malloc crashing program

This is a discussion on malloc crashing program within the C Programming forums, part of the General Programming Boards category; Hello, I am using a series of nested for loops in my program. Within the third nested loop I have ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Unhappy malloc crashing program

    Hello,

    I am using a series of nested for loops in my program. Within the third nested loop I have memory being allocated and deallocated.

    The problem is that the third iteration in the allocation caused my program to crash and I cannot determine why.

    Could someone please help?

    Code:
    #include <stdio.h>	/* for printf and scanf */
    #include <stdlib.h>	/* for the system function */
    
    #define BUBBLE 0	/* define the sorts */
    #define SHELL 1
    #define QUICK 2
    
    #define BEST 0	/* define the cases */
    #define AVG 1
    #define WORST 2
    
    #define TRIALS 100	/* for the trials */
    
    /* function prototypes */
    void generate(long **, int);		/* to create the data */
    void arrange(int, long [], long, long);	/* to arrange the data */
    
    main()
    {
    	/* define the sorts, cases and list sizes */
    	int sorts[] = {BUBBLE, SHELL, QUICK};
    	int cases[] = {BEST, AVG, WORST};
    	int length[] = {10, 100, 1000, 10000, 25000, 50000, 75000, 100000};
    
    	/* create a record structure */
    	struct results { int sort, caseType, length, avgTime; } * recPoint;
    
    	/* iterators */
    	int s, c, l, i;
    
    	/* accumulator and avg for the runtime*/
    	long runTime[TRIALS];
    	
    	/* create a pointer to the random generated list */
    	long * patient;
    
    	/* for each sort */
    	for(s = 0; s < 3; s++)
    	{
    		/* for each case */
    		for(c = 0; c < 3; c++)
    		{
    			/* for each length */
    			for(l = 0; l < 8; l++)
    			{
    				/* generate the data */
    				generate(&patient, length[l]);
    				
    				/* arrange the data */
    				arrange(cases[c], patient, 0, length[l] - 1);
    				
    				/* for 100 trials for the current sort, case length */
    				for(i = 0; i < TRIALS; i++)
    				{
    					/* call the requested sort and return the time */
    					/* runTime[TRIALS] = sort(); */
    
    				}	/* end trial iterations */
    				
    				/* test the output */
    				printf("The sort is %d, the case is %d, the length is %d\n", sorts[s], cases[c], length[l]);
    
    				/* avg the times and add to the record */
    	
    				/* create the record object and add the values */
    				
    				/* remove the dynamic memory */
    				free(patient);
    
    			}	/* end for each length */
    
    		}	/* end for each case */
    
    	}	/* end for each sort */
    
    }	/* end main */
    
    /* begin the generate function */
    void generate(long ** pArg, int reqSize)
    {
    	/* generate storage for the list */
    	long temp = (sizeof(long) * reqSize);
    	printf("temp is %d\n", temp);
    	*pArg = (long *)malloc(temp);
    	
    	/* generate the random data for the list */
    	
    				
    }	/* end the generate function */
    
    /* begin the arrange (quicksort) function */
    void arrange(int cArg, long pArg[], long left, long right)
    {
    	/* function prototype */
    	void swapper(long [], long, long);
    	
    	long i, last;	/* variables for iterating through the list */
    		
    	/* if the case is avg or less than two elements then return */
    	if((cArg == AVG) || (left >= right)) return;
    
    	/* swap the first and middle elements */
    	swapper(pArg, left, (left + right) / 2);
    	
    	last = left;	/* assign the last moved element to left */
    	
    	/* test each element */
    	for(i = 0; i <= right; i++)
    	{
    		/* check which case is required */ 
    		/* if best, ascend, if worst then descend */
    		switch(cArg)
    		{
    			case BEST: if(pArg[i] < pArg[left]) swapper(pArg, i, ++last); break;
    			case WORST: if(pArg[i] > pArg[left]) swapper(pArg, i, ++last); break;
    	
    		}	/* end switch */
    	
    	}	/* end iterating through the elements */
    	
    	/* swap the last moved with the left element */
    	swapper(pArg, last, left);
    	
    	/* recursively call quicksort for each side of the last position */
    	arrange(cArg, pArg, left, last - 1);
    	arrange(cArg, pArg, last + 1, right);	
    		
    }	/* end the arrange function */
    
    /* begin the swapper function */
    void swapper(long theList[], long swap1, long swap2)
    {
    	/* switch the two elements using a temp storage */
    	long hold;
    	
    	hold = theList[swap1];
    	theList[swap1] = theList[swap2];
    	theList[swap2] = hold;
    	
    	return;
    
    }	/* end the swapper function */

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,255
    I don't get any problems with malloc, I get a stack overflow with your code. arrange seems to be called 10000 times, so perhaps you could use a method other than recurstion with your larger lengths?

    What compiler are you using?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    I'm using a recent version of Workshop on Unix.

    I'll try a different sort. Thanks.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 08:42 AM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21