Question:
How can you determine the size of an allocated portion of memory?
Answer:
You can’t, really. free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release of the compiler. Source: CoolInterview.com
The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the bounds of the allocated block are even slightly overstepped Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date:
| Contact Syed Baseer Ahmed
The size of any data structure can be known using the sizeof operator/ function. We generally use it during allocation of memory for a node in a link list or any other dynamic struct. Struct okay ok1 = malloc(struct)sizeof(struct okay); Source: CoolInterview.com
Answered by: Arpan | Date: 6/25/2009
| Contact Arpan
While allocating the memory, it keeps track of the size in the header, which will be usually just before the staring address of allocated memory.
So if anybody want to know the size, he/she can move the pointer to the address before starting location and size can be found. Source: CoolInterview.com
Answered by: Nisheedh | Date: 7/6/2009
| Contact Nisheedh
Programmer no need to bother about how many bytes you need free when you are freeing a pointer using free(ptr), since for each memory allocation there must be a symbol table entry with attributes like starting address, how many number of bytes are allocated and etc... So when you use free on any pointer first it try to loacte that entry and find out how many number of bytes allocated and then de-allocate the meory. Comments are invited. Source: CoolInterview.com
Answered by: Krishna K K | Date: 9/21/2009
| Contact Krishna K K
simply if u want to know how much memory is allocated to character u can use the following printf("%d",size of(3)); this statement tells how much space does 3 takes Source: CoolInterview.com
Answered by: samiullah | Date: 12/23/2009
| Contact samiullah
Suppose we have a variable declared as below: int i=10; Now if we want to know the memory allocated for variable i, use the below : sizeof(i); This return the amount of memory allocated for the variable i of type int. Source: CoolInterview.com
Answered by: Poornima | Date: 1/5/2010
| Contact Poornima
While allocating the memory, it keeps track of the size in the header, which will be usually just before the staring address of allocated memory.
So if anybody want to know the size, he/she can move the pointer to the address before starting location and size can be found Source: CoolInterview.com
Answered by: G.DIVYA | Date: 1/19/2010
| Contact G.DIVYA
#define size(any) (char*)(&any)-(char*)(&any-1)
use the above code to find size
for eg : if u r using an integer variable u can write like this:
size(var_name) it will give u the size Source: CoolInterview.com
Answered by: RAJU SAHANI | Date: 2/10/2010
| Contact RAJU SAHANI
int i=10; Now if we want to know the memory allocated for variable i, use the below : sizeof(i); This return the amount of memory allocated for the variable i of type int. Source: CoolInterview.com
Answered by: Richa Ganjoo | Date: 7/2/2010
| Contact Richa Ganjoo
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|