Saturday, May 22, 2010

How compiler will knows the length of the array using free() function in turbo c?

the memory allocated using malloc function in free() function we will give only starting address

How compiler will knows the length of the array using free() function in turbo c?
When you allocate a memory block using malloc, you pass the amount of memory you want to reserve. If you want to allocate enough memory for a 1000 character array you would use





int *p = malloc(sizeof(char)*1000);





when you later use the free(p) function, it deallocates the entire memory block you originally allocated, regardless of the contents of that 1000 character block. So if your array exceeds 1000 characters, only the first 1000 character blocks are freed. The exceeded portion is NOT deallocated (and your program may crash since it is overwriting other sections of memory). If your array is less than 1000 character blocks, free still deallocates the amount you originally specified with malloc.

local florist

No comments:

Post a Comment