Jump to content
  • Advertisement

Archived

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

hammerstein_02

Arrays..sizeof

This topic is 5786 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

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
Advertisement
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
doesnt this work :
   
template <class type>
int getSize(type &array)
{
return(sizeof array /sizeof array [0] )
}
or something to that effect?

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!