Jump to content
  • Advertisement
Sign in to follow this  
Triad_prague

how does 'delete []' know how much to delete?

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

Hello, it's me again. I've always wondered how does the delete operator know how much to delete? I'll explain it in code


void main()
{
//suppose buffer is a char* pointer allocated somewhere
delete [] buffer; //how does it know how much to delete??
}


and secondly, what happen when I allocate memory using 'new'? does my program "mark" some region in RAM and tell the OS "hey, this region is mine!!" or how? and what happen when I deallocate using 'delete'? will the released memory marked "free" by the OS? and can it be used by other application afterward? .

third, does the OS keep a list of 'allocated' RAM somewhere? and free them afterwards? cause I've been told that unreleased memory is automatically 'grabbed back' by the OS when the application quit.

I'm trying to know more about what happen in the back of the curtain.
Oh and thanks beforehand :lol:

Share this post


Link to post
Share on other sites
Advertisement
I believe that, when new is used to create a buffer, an entry is made in the memory manager describing the size of the buffer, which is then checked by delete [] (note the braces) before releasing the block of memory.

This is why one cannot use delete (w/out braces) to delete an array, cause it doesn't know to look for that extra little piece of info.

I'm not totally sure, but this may be why one cannot use free to delete a block of memory that was created by new (tho I suspect that has more to do with calling constructors and destructors)

To answer your other questions, search for something like 'memory management in OSes' or something like that

Share this post


Link to post
Share on other sites

Hello, it's me again. I've always wondered how does the delete operator know how much to delete? I'll explain it in code


void main()
{
//suppose buffer is a char* pointer allocated somewhere
delete [] buffer; //how does it know how much to delete??
}


It depends on the compiler and implementation, but one common method is to allocate an extra few bytes of memory when you allocate memory, and store the size of the array in those few extra bytes. These few extra bytes are put just before the beginning of the array.


and secondly, what happen when I allocate memory using 'new'? does my program "mark" some region in RAM and tell the OS "hey, this region is mine!!" or how? and what happen when I deallocate using 'delete'? will the released memory marked "free" by the OS? and can it be used by other application afterward? .


Actually, the program requests memory from the OS. The OS manages all the memory. When calling new, a request for memory is sent to the OS, the OS sets aside a chunk of memory for the program, and returns this memory. When calling delete, your program informs the OS that you no longer need this memory and the OS can free it. After the memory has been freed, it can be used by another application.


third, does the OS keep a list of 'allocated' RAM somewhere? and free them afterwards? cause I've been told that unreleased memory is automatically 'grabbed back' by the OS when the application quit.


Yes. This can be done through page files and the like. Memory is broken up into chunks called pages, each page often being a few kilobytes (4KB is the traditional page size). The OS knows which programs have what memory allocated, and when that program is no longer in use it's memory is freed as well. It's all dependent on the OS, so there isn't really one answer, as two OSs may handle the dirty details differently.

Useful links: What's a page, C++ FAQ Lite Freestore Management (see 16.4 especially, as he talks about your exact question, but the whole section is a good read)

Share this post


Link to post
Share on other sites


third, does the OS keep a list of 'allocated' RAM somewhere? and free them afterwards? cause I've been told that unreleased memory is automatically 'grabbed back' by the OS when the application quit.



The operating system creates an address space for every application. The OS is responsible for mapping the address spaces to its own and the program usually shouldn't know where a pointer in its address space points to in the big picture. You would think that a pointer to 123456 or whatever would always point at the same place in your hardware but it doesn't. When the OS destroys the address space because the process ended, it won't matter what is or isn't allocated by the process because everything the OS gave to it is free again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!