Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory allocation in C

Status
Not open for further replies.

hecker

Programmer
May 15, 2000
8
0
0
US
Does Malloc allocate contigous memory locatio ? .In general wot is the criterion followed by this function to allocate memory ?
 
As far as I know. Malloc allocates a continuous chunk of memory. But if you are making a linked list kind of thing. Then it is not necessary that all the nodes are contiguously allocated. <br><br>For eg, <br>int *p;<br>p = (int*)malloc(20* sizeof(int));<br>will allocate contigous memory of 20 integer bytes.<br>So that when you do p+n where n is &gt;=0 and &lt;20 . then you can traverse the next element (just like array).<br><br>Please let me know if I am wrong. <p>Siddhartha Singh<br><a href=mailto:siddhu_singh@hotmail.com>siddhu_singh@hotmail.com</a><br><a href=siddhu.freehosting.net> </a><br>
 
As Sigghartha Singh stated:<br><br>&nbsp;&nbsp;<FONT FACE=monospace>p = (int*)malloc(20* sizeof(int));</font><br><br>will allocate a contiguous block of memory, as stated in the malloc(3C) manual page: <b>malloc</b>() and <b>free</b>() provide a simple general-purpose memory allocation&nbsp;&nbsp;package. <b>malloc</b>() returns a pointer to a block of at least size bytes suitably aligned for any use.<br><br>The one addition I wanted to make is that multiple blocks of memory allocated by malloc() are not necessarily contiguous. I've spent too many hours trying to debug a program because a programmer made this assumption.<br><br>-- Derek Ludwig [ <A HREF="mailto:derek@ludwig.com">derek@ludwig.com</A> ]
 
As Derek said the memory allocated by one call to malloc returns a contiguous chunk of memory. multiple calls means multiple chunks and to complicate things realloc generally will move the chunk around (actually new chunk with old data :) all these calls can cause memory fragmentation resulting in lots of spare memory but split into small unusable chunks. when you're repeatedly calling the malloc family it is iwse to allocate memory in large steps if possible (ie don't expand an array to hold one more integer when you know more will follow) this will make your programs more efficient.
 
thats right but in case if we&nbsp;&nbsp;try to allocate a big chunk of memory using malloc which is larger than the available biggest contiguous free block in memory. What will happen in that case ? So can we be always sure that memory block returned by malloc is contiguous ?
 
Dear hecker,<br><br>You asked: <br><br>&gt;So can we be always sure that memory block returned by malloc is contiguous ? <br><br>My understanding is, Yes. You can actually be denied a memory allocation (out of memory) if there is not a large enough contiguious block available.<br><br>Memory fragmentation is a somewhat complicated issue since compilers and operating systems handle this differently. For instance on some platforms you can use third party heap management systems that will specifically address memory fragmentation.<br><br>Also the nature of your application certainly may increase your exposure to this issue, for instance server processes that are expected to run unattended for long periods of time without being shut down.<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top