Skip to main content
gamedev.net
🔒 Locked

C++ delete question

Started by Mulligan 10 replies 1k views
Original Post
Mulligan
Mulligan
If I declare an array as such: int* array = new int[10]; there is no way to tell how long the variable ''array'' is without using another variable. So how does calling delete[] array; work, if ''array'' can be of any size? How does delete know how much memory to free?
neoztar
neoztar
I think new[] allocates 4 bytes + your memory, stores the count in those 4 bytes and returns the address of the memory. Then delete[] just has to go 4 bytes back from the address you pass it to find the size.

~nz

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
quasar3d
quasar3d
it allocated much more extra bytes. Sometime ago I figuered out that it was 18 bytes if I''m not mistaking before the pointer. But you shouldn''t use that, because you never know if it will work on other systems/windows versions, and you can be quite sure it won''t be cross platform.

My Site
fizban75
fizban75
Because you told the compiler how much to allocate when you said new int[10], so the compiler stores the number with the memory it allocated, and when you call delete [] on that variable, it knows how much to delete.
daerid
daerid
Just trust the compiler. It works, and is part of the standard.

new/delete
new[]/delete[]

always
daerid@gmail.com
C-Junkie
C-Junkie
ladies and gentlemen...

i decree that from now on, this question will be answered with two words, "its magic."

now, move along...
anakin_skywalker
anakin_skywalker
Looks this is what is happening!!

When you declare int *ARRAY = new int[10];, the compiler goes to the heap (a section in where you can allocate memory that lies on RAM) and set memory aside for your data. In this case you are telling to the compiler to set aside 10 int''s or 10 * 4 = 40 bytes. But how do the compiler knows where the memory is?..Well that is why we declare variables...I this case ARRAY is a constant Address,,,or a Pointer to the first byte of the 40 bytes we set aside on memory. Therefore, when you call
delete[] ARRAY;
Your compiler goes to the Address that ARRAY is holding and free the same amount of bytes that you set aside when you reserved that portion of data.
In this case 40 bytes.

ASW
C-Junkie
C-Junkie
Not only are you not entirely right, but this was answered in a post about the exact same thing like 2 days ago.

Thus, "Its magic."
Beer Hunter
Beer Hunter
quote:
Original post by C-Junkie
i decree that from now on, this question will be answered with two words, "its magic."
...but that doesn''t make any sense. You want: "It''s magic.", with the apostrophe.
Shannon Barber
Shannon Barber
quote:
Original post by Anonymous Poster
Now explain the magic behind stack unwinding


First you wind the stack then you unwind it... using Voodoo.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.