   How does he know ?
  Hi!There is an intrigue that doesn't let me sleep at night. ;) It's about memory management.We know that memory allocated with new operator is placed somewhere on the heap of our program. Let's say that we allocate a one-dimensional array of 1000 integers. Eventually we would like to free the array. And here comes my question -if the only thing we have is a pointer to the first element of the allocated array (my_array_pointer), how does the program (operating system?) know how many bytes it should free after a call to 'delete [] my_array_pointer' ?I would be thankful for clearing me that out!   
  It is no problem if the routine backing the new operator allocates 4 bytes (or whatever is needed) more than specified, and to put in the size of the memory block. Similarly the pointer to C++ vtable is stored. Even similar a GC could link all allocated objects for retrieval purposes.   ;  OK, so you say that each time I allocate memory an additional information about the size of the block is allocated as well? That sounds convincing.Quote:Even similar a GC could link all allocated objects for retrieval purposes.What is GC ? :)Thank you!   ;  GC = garbage collectorInstead of manually freeing the memory a GC will free an object when the application has no reference to the memory anymore. This helps in cases where ownership of an object is not clear or managing the ownership would complicate things too much.;  GC is short for Garbage Collection.  I believe GC is automatic in C#, you could probably read up on it if you wanted to know more.  In short, the objective of GC systems is, well, garbage collection.  They have to know when memory is officially 'dead', ie. they have to be able to guarentee that it will not be used again during the programs lifetime.  Then, whenever that is the case, they recycle that memory, by freeing it, or adding to to a list of blocks that can be reallocated, or whatever.  And it should be able to do this transparently to the end user, he should not have to know how it's going on, only that is is.   ;  Great thanks guys! Well, I guess there is no automatic garbage collection in C++ (surely not for data allocated on heap). I see that I didn't specify what language I use. :)   ;  Correct, C++ does not have a GC. It was just another example of how things may occur behind the scenes.BTW: IMHO isn't it great w/ all these abbreviations? ;-)   ;  But there are free GCs available, which replace new/delete:http://www.hpl.hp.com/personal/Hans_Boehm/gc/http://www.codeproject.com/cpp/gc.asphttp://www.codeproject.com/cpp/garbage_collect.asp   ;Quote:Original post by haegarrBTW: IMHO isn't it great w/ all these abbreviations? ;-)Yeah, it helps a lot. :DGetting back to my primary question, dynamic memory allocation (in C++) simply adds some additional bytes (on heap, stack ... somewhere?) where the information about the size of newly allocated block is stored. Right? ;);  Real GC can't be done in C++ - there is no way to take a object of an arbitrary type and deduce what objects that object holds references to.The best you can do in C++ is reference counted smart pointers (which have flaws - circular references being the main one).Real GC requires language level support.   