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

C program to reverse the elements of array: #define rows 3 #define cols 3 void main() {                 int i=0,j=0;                 int arr[rows][cols];

Write a C program to compute the factorial of  5 i.e. 1x2x3x4x5 etc Write a C program to calculate the output Y for a given value of X for the following formula.       Y=X 2

3 . Write a ‘C’ program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

padovan string for a natural number is defined

Temperature Conversions. Problems 28 through30generate temperature-conversion tables. Use the following equations that give relationships between temperatures in degrees Fahrenheit

Question: a) Give two uses of NFS. b) Give one advantage of using NFS. c) Explain the following lines as they would be interpreted in the /etc/exports file i. /usr/local

Develop a biomedical imaging project Project Description: This is a biomedical imaging project. Skills required are C++ Programming, Cocoa, Mac OS, Objective C

create a shopping cart in c++

What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than a