Jump to content
  • Advertisement

Archived

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

bzroom

how does delete[] do it?

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

in vb there is ubound(myarray) to see how big its dimension is is there an equivelent in c++?, how does delete[] know when to stop?

Share this post


Link to post
Share on other sites
Advertisement
no equivilent, the os ''knows when to stop'' by saving it deep in the memory manager IIRC.

you can use std::vector, which is like a ''managed'' array, that resizes for you, and you can get the size and things like that.

Share this post


Link to post
Share on other sites
if you are desperate to know, you can place a try/catch block around an infinite loop that tests each place in the array. Although many people would probably kill me for suggesting it.

Share this post


Link to post
Share on other sites
quote:
Original post by Exorcist
if you are desperate to know, you can place a try/catch block around an infinite loop that tests each place in the array. Although many people would probably kill me for suggesting it.

Assuming that an exception is thrown when you access invalid memory, that technique won''t tell you how long your arrays is, only how far from the starting point you have invalid memory.

If you are desperate to know, don''t forget it in the first place. You have to know how many items to allocate when calling new [], so just save that value if you need to know it later.

Share this post


Link to post
Share on other sites
The block is probably overallocated and the size of it is stored at the beginning. Then it returns you a pointer 4 bytes in (or whatever the max allocation size is). delete[] then just backs up 4 bytes, gets the size, and unallocates the entire block.

Thats one way to do it. Come on people, you can step into the runtime library yourself and see how its done.

Share this post


Link to post
Share on other sites
quote:
Original post by honayboyz
so will


int x;
int *ptr = &x;
ptr--;


will ptr* = 1?

x is not dynamically allocated, which is what we''re talking about.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i think me means

type *ptr = new type[size]

temp = sizeof((*ptr));

thats not guranteed to work i dont think...

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
i think me means

type *ptr = new type[size]

temp = sizeof((*ptr));

thats not guranteed to work i dont think...

This simply cannot work because sizeof() is evaluated at compile time while "size" is evaluated at runtime.

Share this post


Link to post
Share on other sites
A header is created with each usage of the new operator which specifies how much memory was reserved. I''m not sure whether it''s at the front of the block as antareus suggested or not, but, that''s what happens.

- Jason Citron
- ZeroInfinity Studios
- www.zeroinfinity.net

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!