Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how does delete() work? (http://www.go4expert.com/forums/delete-t3486/)

tom_ge 20Mar2007 10:29

how does delete() work?
 
delete() in C take a void pointer as an argument.How does it know how many bytes of memory to delete ? Can anyone help me out ?

Thanks in advance

shabbir 20Mar2007 13:11

Re: how does delete() work?
 
One word answer is magic and the more detail answer is it stores the amount allocated when allocating. It depends on the compiler used and as far as I remember MS VC stores before the memory chunk. Dont quote me on this as I dont have the VC installed on this PC.

tom_ge 21Mar2007 11:28

Re: how does delete() work?
 
Thanks shabbir,

So am i right in saying
if i allocate a character pointer with 100 bytes of memory..in reality there is 100 bytes + 4 bytes(initial block for pointer to know how many bytes internally allocated to the pointer) allocated, but these 4 bytes are not acccessible by the user and is only for compiler to know how many bytes to delete.

DaWei 21Mar2007 12:10

Re: how does delete() work?
 
There are more than 4 bytes of overhead, but you're getting the picture. You can google for malloc implementations if you want the precise details for your particular implementation. The delete function is actually a C++ thing, not a C thing. New and delete are wrappers for the C-type malloc (and family) and free operations.

tom_ge 21Mar2007 12:38

Re: how does delete() work?
 
Thanks Dawei


All times are GMT +5.5. The time now is 20:13.