• Announcements

Archived

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

how does delete[] do it?

Recommended Posts

bzroom    647
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 on other sites
billybob    134
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 on other sites
Digitalfragment    1504
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 on other sites
Brother Bob    10344
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 on other sites
antareus    576
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.

bzroom    647
so will

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

will ptr* = 1?

Share on other sites
Brother Bob    10344
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 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 on other sites
darookie    1441
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 on other sites
Clash    342
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 on other sites
Zipster    2359
Althought if the location of the header isn''t standardized, you might as well forget it exists for your purposes

Share on other sites
drslush    122
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...

temp will then be sizeof(type), since you are dereferencing a pointer to type. Same as:

type *ptr = new type;

temp = sizeof((*ptr));

Share on other sites
C-Junkie    1099
$cat array.cpp int *ary = new int[50000]; cout << ary-- << endl; cout <<*ary-- << endl; cout <<*ary-- << endl;$ strace ./array
old_mmap(NULL, 200704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x401a7000
write(1, "0x401a7008\n", 11) = 11
write(1, "200706\n", 7) = 7
write(1, "0\n", 2) = 2

It would appear that C++ does infact keep some space for itself. 8 bytes, in fact.

furthermore, it _seems_ that is is a 64-bit integer specifying how much space was allocated. and it overestimates by two bytes.

interesting

Share on other sites
Guest Anonymous Poster
C++ in fact doesn''t specify anything about how it knows the array size. The implementation decides how to implement new[] and delete[]. If you try to access this implementation stuff, it means your code could break on a different platform, a different compiler, or even a different version of the same compiler. Or even on the same compiler, since it doesn''t necessarily handle all allocations the same way.

Just use std::vector instead and don''t worry about it. The only good time to use arrays in C++ is for interfacing with other people''s code.

Share on other sites
antareus    576
quote:
Original post by antareus
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.

And sizeof(int) == 4 on x86, besides the example code being incorrect.

Share on other sites
Wohoo! I am not the only one with this question!!

http://www.gamedev.net/community/forums/topic.asp?topic_id=147725

At this point in time I am at the mercy of the
OS to "delete [] myarray", and ignoring how the system knows how big a arbitrary dynamically allocated array is during deletion.

www.cppnow.com