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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on calloc and malloc

Status
Not open for further replies.

haivan

Programmer
Jun 7, 2000
2
US
I am new programmer. Could some any explain me the difference between calloc() and malloc(). How to use these functions ?<br><br>Say I have the structures like these<br><br>typedef struct paths <br>{&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;float flow;<br>&nbsp;&nbsp;&nbsp;struct paths *next;<br>} paths;<br><br>typedef struct k_paths<br>{<br>&nbsp;&nbsp;&nbsp;paths *p;<br>&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;n_arc;<br>} k_paths;<br><br>and I want to allocate memory for 8 k_paths and enter the data. What I have to do?<br><br>Thank<br>Haivan<br><br><br>
 
hi haivan,<br><br>you will get problems handling linked lists if you don't even know exactly how to use malloc().<br><br>malloc() and calloc() are basic c-functions. you will find them explained in any c-book for beginners.
 
hi haivan,<br>basic difference between calloc and malloc is that calloc initializes the memory it allocates to zero,while malloc does not.so it is better to call calloc.<br><br>to initialize......<br><br>typedef k_paths * pstr;<br>pstr arr = (pstr)calloc(8*sizeof(k_paths));<br><br>to assign.....<br>arr[0].n_arc = 3;<br>arr[0].path-&gt;flow=4.0;<br><br>/*also refer to realloc(),assert()*/<br>to deallocate....<br>free(pstr);<br><br>/* refer to K&R for further details*/<br><br>
 
I think it's a small mistake in topinc's answer. calloc is intend to use when you need memory for an array. Using calloc, the memory is filled with 0.<br>I think the right example is<br>pstr arr = (pstr)calloc( 8, sizeof( k_paths ) );<br><br>or (the same effect)<br><br>pstr arr = (pstr)malloc( 8 * sizeof( k_paths ) );<br>memset( pstr, 0, 8 * sizeof( k_paths );<br><br>Of course, arr's validity must be tested (arr != NULL)
 
Dear All,<br><br>As Vali pointed out there are some minor (hehe) problems with topinc's code. ehm...<br><br>&gt;arr[0].path-&gt;flow=4.0;&nbsp;&nbsp;//will not work anywhere in the known universe! There is no such entity 'path' in the code posted in this thread.<br><br>Even worse if it was intended to do this:<br>arr[0].p-&gt;flow=4.0;<br>as that would crash anywhere in the known universe! Since k_paths.p is declared as a pointer to a struct of type paths you have to actually allocate that memory!<br><br>Also the following:<br>&gt;free(pstr);&nbsp;&nbsp;//will not work either since pstr is a type and not a variable.<br><br>Now the original question was:<br><br>&gt;and I want to allocate memory for 8 k_paths and enter the data. What I have to do?<br><br>So you must allocate the Eight k_paths structures then for each one of them you must allocate it's k_path.p member, well at least if you want to use it for something other than crashing your program. <br><br>Then better yet you need a mechanism to set the k_path.p-&gt;next member to the next 'paths' structure... which is where? One of the Eight - k_path.p members that you allocated, or not? I don't know but this was a great example in structures and pointers and should make everyone want to say:<br><br>C++ RULES BABY!<br><br>Good luck<br>-pete<br><br><br><br><br><br>
 
Dear palbano,vali,haivan,<br>i answered in a hurry ,and i myself have not used calloc in my code ,i.e the reason for too many errors. i will work out and post my deatailed answer soon, palbano ur reply is indeed very funny,i can't stop laughing<br>thanks a lot
 
Thanks topinc, I try to add some levity whenever I can. Lord knows, this profession can sure use it!<br><br>You should have seen the one I posted to a USNET group years back when someone asked 'what are cookies?', and it was during the Christmas season!<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete<br>
 
Thank all of you. Your sugestions are very<br>useful to me. In fact there is a minor difference between calloc and malloc but some ones prefer to use calloc the others - malloc and I want to know why? My example is not simple as pointed out in palbano's answer&nbsp;&nbsp;because I want to creat an array of k_paths<br>structrue whose member p is a nested list so<br>I think each time I must allocate memory for<br>this member.<br><br>Haivan
 
/*hi haivan here is the solution */<br># include &lt;stdio.h&gt;<br># include &lt;stdlib.h&gt;<br># include &lt;conio.h&gt;<br># include &lt;assert.h&gt;<br><br>typedef struct paths<br>{<br>&nbsp;&nbsp;&nbsp;float flow;<br>&nbsp;&nbsp;&nbsp;struct paths *next;<br>}paths;<br><br>typedef struct k_paths<br>{<br>&nbsp;&nbsp;&nbsp;paths *p;<br>&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;n_arc;<br>} k_paths;<br><br>void main()<br>{<br>int i;<br>k_paths * point;<br>clrscr();<br>point = (k_paths *)malloc(8*sizeof(k_paths));<br><br>assert(point);<br><br>for(i=0;i&lt;8;i++)<br>{<br>point<i>.n_arc=i;<br>point<i>.p=(paths *)malloc(sizeof(paths));<br>assert(point<i>.p);<br>point<i>.p-&gt;flow=2.0*i;<br>point<i>.p-&gt;next=NULL;<br>printf(&quot;\n point[%d].n_arc = %d, point[%d].p-&gt;flow= %f&quot;,i,point<i>.n_arc,i,point<i>.p-&gt;flow);<br>}<br><br>for(i=0;i&lt;8;i++)<br>free(point<i>.p);<br><br>free(point);<br>}<br><br>&nbsp;/*program output in Turbo C compiler version 3*/<br>&nbsp;/*if you are not compiling in Turbo C, comment lines 4 and 23*/<br>&nbsp;/*<br>&nbsp;point[0].n_arc = 0, point[0].p-&gt;flow= 0.000000<br>&nbsp;point[1].n_arc = 1, point[1].p-&gt;flow= 2.000000<br>&nbsp;point[2].n_arc = 2, point[2].p-&gt;flow= 4.000000<br>&nbsp;point[3].n_arc = 3, point[3].p-&gt;flow= 6.000000<br>&nbsp;point[4].n_arc = 4, point[4].p-&gt;flow= 8.000000<br>&nbsp;point[5].n_arc = 5, point[5].p-&gt;flow= 10.000000<br>&nbsp;point[6].n_arc = 6, point[6].p-&gt;flow= 12.000000<br>&nbsp;point[7].n_arc = 7, point[7].p-&gt;flow= 14.000000<br>&nbsp;*/<br>&nbsp;/* for bugs contact <A HREF="mailto:babusats@rediffmail.com">babusats@rediffmail.com</A>*/<br> <p>sathish babu<br><a href=mailto:babusats@rediffmail.com>babusats@rediffmail.com</a><br><a href= > </a><br>
 
calloc does exactly the same thing than malloc but calloc <br>sets all elements to 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top