How does free() know how many bytes to free?
Answers were Sorted based on User's Feedback
Answer / sekhar
free takes the pointer variable as argument. The intial
data at that location holds a value of number of bytes that
pointer is pointing to. Based on that information, it frees
the memory.
| Is This Answer Correct ? | 9 Yes | 5 No |
Heap memory menager is a person, who is responsible for
dynamic memory allocation. He will maintains a internal
data structure(table) to update the information on free and
allocated memory pools. when memory is allocated using
malloc or calloc, the heap memory manager will update the
table(which tells, no of bytes allocated to a particular
pointer) and returns the base address to the application.
when application calls free() with the pointer to be freed
as a argument, free will look into the table which holds no
of bytes allocated for that pointer. And it will free those
many bytes from the base address. Again the freed memory
shall be added to a free memory pool so that other fellows
who needs memory can utilise it.
| Is This Answer Correct ? | 6 Yes | 2 No |
Answer / guest
The malloc/free implementation remembers the size of each
block allocated and returned.
| Is This Answer Correct ? | 6 Yes | 4 No |
Answer / vrushali
Whenever we call malloc() for allocating memory. The value
of the starting address is stored in a virtual memory tables.
So whenever free() is called then using the starting memory
location free () will have an entry of the bytes allocated
and to be freed. It can do it using this entry then entry is
deleted also.
This is pure OS concept.
Please reply here if you are not able to understand my answer.
| Is This Answer Correct ? | 4 Yes | 3 No |
Answer / sourisengupta
free(ptr);
free takes the pointer variable as argument. The intial
data at that location holds a value of number of bytes that
pointer is pointing to. According to the information, it
frees
the memory.
| Is This Answer Correct ? | 4 Yes | 4 No |
Answer / rpjanaka
Then, is there a way to get out the number of bytes that
heap manager going to free, when I call free() function.
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / pearl
This is possible if your OS providing the system calls to access those memory manager data structure.
| Is This Answer Correct ? | 0 Yes | 0 No |
Explain bit masking in c?
in any language the sound structure of that language depends on its a) character set, input/output function, its control structures b) character set, library functions, input/output functions its control structures c) character set, library functions, control sturctures d) character set, operators, its control structures
please send me papers for Dy. manager IT , PNB. it would be a great help for me.
How can you increase the size of a statically allocated array?
Does free set pointer to null?
how to determine the complexity of an algorithm as log(n)
what is the purpose of the following code, and is there any problem with the code? void fn(long* p1, long* p2) { register int x = *p1; register int y = *p2; x ^= y; y ^= x; x ^= y; *p1 = x; *p2 = y; }
When c language was developed?
#define d 10+10 main() { printf("%d",d*d); }
Explain how do you search data in a data file using random access method?
If the static variable is declared as global, will it be same as extern?
Write any data structure program (stack implementation)