• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: flexible array member not initializing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: flexible array member not initializing


  • Subject: Re: flexible array member not initializing
  • From: Douglas Norton <email@hidden>
  • Date: Mon, 5 Apr 2010 07:20:59 -0700

1. Your allocation is incorrectly sized, 'sizeof(Vertex)*nverts' should be 'sizeof(Vertex*) * nverts'

2. Malloc does not initialize any of the memory it allocates. Your newobject method is setting the first entry in the array to NULL, but the remaining pointers from (1.. nverts - 1) are all uninitialized. Without allocating a Vertex object and putting that into the pointers, your access will always fail.

try:

Object* newobject(char* filename, long nverts, long nface, p_ply ply)
{
	Object* newo=(Object*)malloc(sizeof(Object)+sizeof(Vertex*)*nverts);
	if (newo==NULL) {
		return NULL;
	}
	newo->i = 0;
	newo->filename = filename;
	newo->nvertices = nverts;
	newo->nfaces = nface;
	newo->ply = ply;

	for ( long index = 0; index < nverts; ++index ) {
		newo->vertices[ index ] = new Vertex();
	}

	return newo;
}

3. When you destroy this object, be sure to walk the vertices array and dispose of each Vertex, or you'll introduce a leak.



On Apr 5, 2010, at 6:21 , Juan Vesa wrote:

> hello,
> sorry if this is not ok topic for this list.
> i'm writing a program in xcode 2.4.1, os x 10.4.11, ndi have a struct Object like:
>
> typedef struct Object
> {
> 	char* filename;
> 	//internal index for reading vertex position
> 	int i;
> 	p_ply ply;
> 	long nvertices;
> 	long nfaces;
> 	Vertex* vertices[];
> } Object;
>
> when i initialize it in a newobject() function, all the member get initialized except for the array vertices, i'm using dynamic allocation in the newobject() method like:
>
> Object* newo=(Object*)malloc(sizeof(Object)+sizeof(Vertex)*nverts);
> 	if (newo==NULL) {
> 		return NULL;
> 	}
>
> where nverts is the size of an array (read in from a file at run time), and vertex is another structure with 3 double member values. so looking to the debugger all the data members are there but vertices doesn't exist, so later when trying to use an Object struct allocated by this method in another function i can't add anything to the array vertices, the debugger comes up and i get the error "Program received signal:  "EXC_BAD_ACCESS".".
>
> o->vertices[o->i]->x = value;
>
> could any one please help? the entire newobject() function is below.
> thank you very much,
> juan
>
> Object* newobject(char* filename, long nverts, long nface, p_ply ply)
> {
> 	Object* newo=(Object*)malloc(sizeof(Object)+sizeof(Vertex)*nverts);
> 	if (newo==NULL) {
> 		return NULL;
> 	}
> 	newo->i = 0;
> 	newo->filename = filename;
> 	newo->nvertices = nverts;
> 	newo->nfaces = nface;
> 	newo->ply = ply;
> 	newo->vertices[0] = NULL;
> 	return newo;
> }
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >flexible array member not initializing (From: Juan Vesa <email@hidden>)

  • Prev by Date: Re: flexible array member not initializing
  • Next by Date: Re: flexible array member not initializing
  • Previous by thread: Re: flexible array member not initializing
  • Next by thread: Xcode 3.2.2 standalone download?
  • Index(es):
    • Date
    • Thread