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

Malloc help!!!!!!!!!!

Status
Not open for further replies.

rotovegasBoy

Programmer
Sep 16, 1999
88
0
0
NZ
I'm a second year student at university in New Zealand. That will be a sophmore in college to most of you (i think).<br>
Any way I'm having real big problems with malloc. My assignment is to compare the effiency of various sorting algorithms. Most of this involves splitting arrays. This in turn mainly involves using dynamically allocated arrays. This is the problem. When I run the algorithms on a small array the program runs fine but when the size gets up there I get seg-faults. Has any one got tips on using malloc and freeing up memory in recursive functions. Before any one suggests c++ the program has to be in C. The actual algorithms themselves aren't much of the problem they've been supplied in pseudo code by Prof. Tadoka, the problem is translating the specifically into C.
 
Hi.<br>
<br>
Couple of questions:<br>
<br>
1) How small is small and how big is big?<br>
<br>
2) What environment, versions of the operating system and compiler, are you running in? ("Seg faults" sounds like Windows - but which version?)<br>
<br>
Regards<br>
<br>
Mike<br>
---<br>
Mike_Lacey@Cargill.Com<br>

 
Yes well I suppose I should have provided details. I'm running Solaris 5.7(uni) and Win95(home). At home I'm using the cygwin package to compile and do general boring things. For those who don't know cygwin uses the Borne Again Shell, a borne shell clone for windows/dos.<br>
<br>
Big is about 100. I haven't done enough testing to fin out the threshold but small lists 10 or so items runs fine.
 
100 doesn't sound very big - to be honest. I would guess you have omitted to return memory you have malloced - does the same code run ok at uni?<br>
<br>
Mike<br>
---<br>
Mike_Lacey@Cargill.Com<br>

 
I have to agree with Mike.<br>
<br>
Could you post the program so that we can take a look at it?<br>
<br>
Thanks!<br>
<br>
-Mart311
 
Yes I agree that my problem is with using malloc.<br>
Heres one of the programs. The code does not run on the Solaris machines either. I'm not sure whether or not it is the same problem.<br>
<br>
int *insertion(int insert, int arr[], int length){<br>
int *p, *q, *result, i;<br>
result=malloc(sizeof(arr)+sizeof(int));<br>
i=0;<br>
p=arr;<br>
q=result;<br>
while(arr&lt;=insert && i&lt;length){<br>
*q=*p;<br>
q++;<br>
p++;<br>
i++;<br>
}<br>
*q=insert;<br>
q++;<br>
while(i&lt;length){<br>
*q=*p;<br>
q++;<br>
p++;<br>
i++; <br>
}<br>
return result;<br>
}<br>
<br>
int main(void){<br>
int list[1], *newlist, i, a;<br>
i=0;<br>
list[0]=67;<br>
srand(2534);<br>
newlist=insertion(34, list, 1);<br>
for(i=2; i&lt;100; i++){<br>
a=rand()%100;<br>
newlist=insertion(a, newlist, i);<br>
}<br>
/* code for printing/returning newlist*/<br>
return 0;<br>
}<br>
<br>
thanks
 
I think I just found my own error. sizeof(arr) is four bytes. What I should have had was malloc((length+l)*sizeof(int));<br>
<br>
Thanks to those who helped.<br>
<br>
If any one has some constructive criticism of my programin general can they please post it (I'm being marked on programming style and it would be nice to here from thsoe who have been programming for a while).
 
Constructive crit.<br>
<br>
Comments - it may be that you removed the comments when you posted it here, something I often do. But it is something to bear in mind as lecturers are obsessed with comments (I used to be a lecturer - does it show?)<br>
<br>
Sizeof stuff is easy to miss and is a pain, I always seem to end looking at the size of a pointer rather than what it's pointing -- D'oh!<br>
<br>
Mike<br>
---<br>
Mike_Lacey@Cargill.Com<br>

 
Thanks for the comment. Yes I cut out a bit of commenting for this post but the actual comments are a bit sparse. Also as ain interesting sideline I do get a segmentation fault (in windows/Dos prompt) when i use the corrected program to sort 1000000 integers. I gather this is a combination of an inefficient algorithm and the operating system is there any way to guard against this error. i.e can I stop the program before this happens?
 
<br>
I would recommend the following:<br>
<br>
result = (int*)malloc((length+l)*sizeof(int));<br>
<br>
if ( 0 == result) {<br>
exit(1); // Or whatever action you want to take.<br>
}<br>
<br>
Remember that malloc returns a void*, which you need to<br>
cast to the appropriate type.<br>
<br>
Also, if malloc fails when it tries to allocate more memory it returns a NULL (0) pointer, so you should check for it.<br>
<br>
-Mart311
 
Some points of using malloc:<br>
1) Always check the return value when you malloc memory. <br>
<br>
2) If you are using recursive function. Try to free the allocated memory. when you come out of the loop. <br>
<br>
This way you can get rid of the problem.<br>
Does it answer your question ?<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top