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
Write a C++ program that reads from keyboard 3 integers, with proper input prompt, and then displays the maximum sum of any pair of numbers from these three. If the 3 numbers are 5

Program of Binary tree: Btree::Btree(int O) : itemsInContainer(0) {     finishInit(O); }   Btree::~Btree(void) {     if( root != 0 )         delete roo

write a program to find the area under the curve y=f(x) between x=a and x=b integrate y=f(x) between the limits of a and b #include float start_point,

Write a program that keeps record of football results. Program stores following information about each match: Description Type and/or size name of home team max. 25 characters name

evolutio of object oriented programing

When you are designing a generic class to have or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

C program to define power of a function: Write a program to use power of a function. void main() { int x,y,po=1,i,j; clrscr();   printf("Give x,y\n"); sca

HOW CAN WE EASLY LEARN C++

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing tha