Archived

This topic is now archived and is closed to further replies.

johnnyBravo

If you have a pointer is it possible to find sizeof thru pointer?

Recommended Posts

this is my code.
//the function

void save(Vertex3 value[])
{
	//i get the size of "value" and create a new Vertex3 

	//of the same size of "value"

	Vertex3 myVertex[sizeof(value)/sizeof(Vertex3)]
}


//using the function

Vertex3 vertex[3];
//fill vertex 0,1,2 with data

//.

//..

//...

//then send it to the function to work with

save(vertex);
i want to find the size of the array from the save function only. But because its a pointer it just returns 4. So is there any way around this, thanks.

Share this post


Link to post
Share on other sites
No, you can''t know the size of a dynamically-allocated array, unless you store it somewhere and pass it along.

You can either create a "vertex array" object that also holds the size of the vertex, or use stl vectors.

Share this post


Link to post
Share on other sites
quote:
Original post by ToohrVyk
No, you can''t know the size of a dynamically-allocated array, unless you store it somewhere and pass it along.

You can either create a "vertex array" object that also holds the size of the vertex, or use stl vectors.




It isn''t dynamical. But I''m not sure if it is possible.
You can always try..

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
MSDN description of sizeof:

When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

Share this post


Link to post
Share on other sites
ah its just that i can''t create an array from an integer or something.
eg
//doesnt work
int size =5;
int numbers[size];

and i can''t use pointers and ''new'' as i would use ''numbers'' for something else that can''t use a pointer.



Share this post


Link to post
Share on other sites
If the compiler knows how big your array is, then it''s fine, ie:

void functions(void)
{
int Array[5];

printf("sizeof(Array) = %d\n", sizeof(Array));

Function2(Array);

}

void Function2(int Array[])
{
printf("sizeof(Array)2 = %d\n", sizeof(Array));
}



would output:
sizeof(Array) = 20
sizeof(Array)2 = 4

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
ah its just that i can''t create an array from an integer or something.
eg
//doesnt work
int size =5;
int numbers[size];



That''s true, but you can do:


int numbers[5];
int size = sizeof(numbers);


That way, if you change the size of the array, size will automatically be updated, saving you from updating one and forgetting to update the other. With this you can now pass size to any function that needs to know the size of the array.

A better solution might be an stl vector, but only if you''re comfortable with stl.

Share this post


Link to post
Share on other sites