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

Assignment Help:

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.


Related Discussions:- How does free know the size of memory to be deleted.?

Recursive function, Binomial coefficients are the numeric factors of the pr...

Binomial coefficients are the numeric factors of the products in a power of a binomial such as (x + y)n. For example, (x + y)2 = x2 + 2 x y + y2 has the coefficients 1 2 1. Binomia

If statement, Write a program to input two integers. If they are equal disp...

Write a program to input two integers. If they are equal display "The numbers are equal". Otherwise display a message if the first is a multiple of the second or if it is not a mul

Inverted Index Implementation, Need help finishing my homework. It''s been ...

Need help finishing my homework. It''s been 5 years since I''ve use C++. I am a VN.Net programmer. -Todd

Explain the pointer types, Pointer Types Pointer holds the address of a...

Pointer Types Pointer holds the address of an object, permitting for the indirect manipulation of that object. They are used in creating linked data structures like lists, tree

C program to check prime numbers , C Program to check PRIME NUMBERS   ...

C Program to check PRIME NUMBERS   main() {           int i,k,r,flag;           clrscr();           printf("ENTER THE NO. TO CHECK IT IS PRIME OR NOT: ");

What is token in programming languages, T o k e n :  Tokens  are  s...

T o k e n :  Tokens  are  small  entities  in  a  program.    Example: identifiers,  keywords,  constants, operators, strings, etc.  These tokens are used almost in same wa

Big-m method, big-m method operation reseach in progrmme c++..

big-m method operation reseach in progrmme c++..

Define storage classes of c program - computer programming, Define Storage ...

Define Storage Classes of c program - computer programming? Each variable and function in C language has two attributes that are type and storage class. If storage class of a v

Minimum shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd