Archived

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

hammerstein_02

Arrays..sizeof

Recommended Posts

Can I pass a dynamic array to a function, and determine the size of that array? Or do I have to pass the size of the array to the function? I am creating a function whereby my objects will need to know where in a map they are...the map will be stored as a dynamic array and the objects will update their positions. I don''t mind being corrected if my reasoning is completley pants here so opinions are welcome. This is so I can check for collisions with other objects without reading through my object list twice. Ok.. thanks for any replies..

Share this post


Link to post
Share on other sites
If by "dynamic array" you mean a bunch of objects allocated with malloc() or new[] then no, you''ll need to keep track of the size separately. std::vector will package this up for you nicely (provided you are using C++).

Share this post


Link to post
Share on other sites
The answer you are looking for is NO

My suggestion is since it''s for a map, you should make a struct/class for the map and pass a pointer to it. The class should have the size information inside it, so you don''t need to pass extra variables to your function.

Share this post


Link to post
Share on other sites
Am I the only one who is puzzled by the inability to determine the size of a dynamically-allocated array at run-time? After all, it has to be stored somewhere, for delete[] to work...

Cédric

Share this post


Link to post
Share on other sites
A general rule of thumb is that the one who allocates it should deallocate it.
If you follow that there is no problems storing how much memory you have allocated. If you have a function that accepts a pointer to an array you should have an extra parameter describing how big it is

Share this post


Link to post
Share on other sites
I suppose if you think of how an array works in the memory, it does really make sense that you can''t know how many elements it has. I mean there is nothing there to say, ''hey look.. I got 10 elements.''
I didn''t think to use a vector, simply for the reason I haven''t used them much before and basically forgot I could access them as I do an array. My aim to keep collision detection down on my map is only check the tile to which my vehicle is going to move and if the tile is valid update the array with a pointer to my object.
Will go home and try work out how I am going to exactly implement this then.. thanks for the replies guys... sorry for not switching my brain on this morning.

Share this post


Link to post
Share on other sites
It could work for static arrays, but certainly not for dynamic ones. sizeof is resolved at compile-time.

Cédric

[edited by - cedricl on August 14, 2002 1:16:15 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
Am I the only one who is puzzled by the inability to determine the size of a dynamically-allocated array at run-time? After all, it has to be stored somewhere, for delete[] to work...


If the block was allocated using a general-purpose memory allocator, there's a fair chance that the block size is indeed stored somewhere. You might even be able to extract the size using some highly platform-specific code. But there's no guarantee that this is even possible, and even if it is you would be getting uncomfortably cosy with your runtime library - as in "ouch, my code will crash and burn when compiled with the new compiler version".

delete[] does not necessarily need to know how many bytes you originally requested. The language could provide a standard mechanism with which you could retrieve this information, but that would burden the allocator with extra data it might not otherwise need to keep track of. So it's a matter of efficiency.

[edited by - spock on August 14, 2002 4:10:07 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
Am I the only one who is puzzled by the inability to determine the size of a dynamically-allocated array at run-time? After all, it has to be stored somewhere, for delete[] to work...



Sure it could be stored somewhere , but it is
implementation-defined. The standard does not say anything
about how to store this information. So the conclusion is
that there is no portable way to retrieve this information.
Just use std::vector.




Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites