How does free know the size of memory to be deleted.?, C/C++ Programming

How does free know the size of memory to be deleted.?

int *i = (int *)malloc(12); followed by free(i); how did free function call know how much of memory to delete?

A: It based on the implementation, however usually there is a malloc header added to all the memory allocated through malloc. On Linux its four bytes of memory preceding the memory returned to you, which contain the number of bytes allocated + 4(itself). Thus while you say,

int *i = (int *)malloc(12);

it allocates 16 bytes.

-------------------

17 | | | |

-------------------

^

|

i

Since you can see above total of 16 bytes are allocated, first four bytes stores the number of bytes allocated(offset to the next memory from the begin of this memory). Address of the 5th byte is returned to i. now i can access 12 bytes through this byte.

Posted Date: 3/19/2013 8:25:49 AM | Location : United States







Related Discussions:- How does free know the size of memory to be deleted.?, Assignment Help, Ask Question on How does free know the size of memory to be deleted.?, Get Answer, Expert's Help, How does free know the size of memory to be deleted.? Discussions

Write discussion on How does free know the size of memory to be deleted.?
Your posts are moderated
Related Questions
Virtual Base Classes This ambiguity can be resolved if the class derived have only one copy of the class base. This can be done by making the base class a virtual class. This k

I need Asp .net Facebook app project completion Project Description: We are urgently seeking a few remaining items to be completed, and the project to be deployed to live.

Using a Friend Using a friend function is quite easy. The following example explains a friend function to access members of two classes. class Bclass;



a) Implement a binomial tree class with a method that calculates the value of an option passed in to the class. The binomial tree should not rely on specific features of the differ

One person who is specialist at programming and solving problems with a computer Project Description: Dear Potential computer, hardware, programming and software genius, I

What is difference between require_once (), require(), include()? require() includes and evaluates a specific file, if file isn't found then it shows a Fatal Error. require_

Rahul is a newbie to the programming and while learning the programming language he came to know the following rules: ???• Each program must start with ''{'' and end with '

How do you access the values within an array? - Arrays comprise a number of elements, which depends on the size you assigned it during variable declaration. - Every element