Archived

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

[c++] sizeof()...is this legal?

This topic is 6017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I want to be able to see how many elements I have in an array in the code, so I can simply alter array size and work with for loops, etc. So tell me, will this work: sizeof(array)/sizeof(arraytype) as in sizeof(particles)/sizeof(CParticle) Or if it won''t work, is there another way to accomplish the same thing? -- WNDCLASSEX Reality; ... ... Reality.lpfnWndProc=ComputerGames; ... ... RegisterClassEx(&Reality); Unable to register Reality...what''s wrong? --------- Dan Upton Lead Designer WolfHeart Software

Share this post


Link to post
Share on other sites
That will work perfectly as long as the array isn''t created on the heap (ie, with new or malloc). If you DID create your array with new, then the array variable is really just a pointer to another memory location, hence doing a sizeof(array) will always return 4 bytes for 32bit systems.


- Houdini

Share this post


Link to post
Share on other sites
no, sizeof(*array) would (this is just my intuition, BTW, I don''t know for sure) be the same as sizeof(array[0]) which would be the size of array''s type. e.g.:

  
int *array1 = new int[100];
char *array2 = new char[100];

printf( "%d\n", sizeof(*array1) );
printf( "%d\n", sizeof(*array2) );


would give the output:

4
1

There''s no real way to determine the number of elements in a dynamic array. You can use functions like _msize() to give you the size of an allocated block, but you''ve got to be very careful with that, since it will return a different number depending on whether you''re in a release build or a debug build (because the debug build allocates more memory to test for buffer over- and under- runs)


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
quote:
Original post by Dean Harding
There''s no real way to determine the number of elements in a dynamic array.


There''s also little need. If you''ve allocated an array dynamically (via new or malloc), you already know how many elements are in the array.

  
int* p = new int[104]; //okay, there are 104 elements, yes?


Share this post


Link to post
Share on other sites
quote:
Original post by mossmoss

There''s also little need. If you''ve allocated an array dynamically (via new or malloc), you already know how many elements are in the array.



Not always, what if the array was passed to you by a function you have no control over? Although, the compiler can''t keep track of the sizeof() of a static array when you pass it to another function either, so it''s a moot point really.

Anyway, in this case, the solution is to also pass the number of elements, or encapulate the array in a class.


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites