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
1. Using Visual C++ and your OpenGL configured environment, write an application that displays a “unique” graphical scene that you designed and coded for this course. What yo

how to find minimum number if shelves required

if one won't explicitly call the destructor of a local; then how does he handle the above situation?

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Define the Recursion Function in C? In C, it is potential for the function to call themselves a function is describing 'recursive' if a statement within the body of a function

A: It is implementation dependent. Verify with your compiler's documentation. For instance, assume you wish to do binary I/O using std::cin & std::cout. Unluckily there is no st

(a) Write a procedure (count-even n) that counts the number of even digits in the decimal representation of the number n. For example, (count-even 234) should return 2. (b) Writ

In this problem you are given a board in which some of the elements are placed as shown in diagram below. Each element represents a color. Fill the other elements in the board, suc

Assume we wish to partition the square roots of the integers from 1 to 100 in to two piles of fifty numbers every, such that the sum of the numbers in the first pile is as close as

string S convert it to a palindrome by doing chara, C/C++ Programming