Thread: Why is this free corruption the heap?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    Why is this free corruption the heap?

    Hi there,

    Doing something very simple here, not sure why I'm getting heap corruption at run time.
    This function was much longer but I stripped it down to figure out what's wrong::

    Code:
    bool DeleteRecord(void){
    
    	StudentRecord** delTemp; // temporary copy of DB used to resize array when adding
    
    	delTemp = (StudentRecord**)malloc(g_numRecords * sizeof(StudentRecord*));
       for(count=0;count<g_numRecords-1;count++){
    		delTemp[count]=(StudentRecord *)malloc(sizeof(StudentRecord));
    		delTemp[count]->firstname=(char *)malloc(sizeof(char));//malloc fname 
    		delTemp[count]->lastname=(char *)malloc(sizeof(char));//malloc lname
       }
    	printf("malloced\n");
    	delTemp[0]->firstname="........yous";
    	printf("delTemp[0]->firstname=%s\n", delTemp[0]->firstname);
    	delTemp[0]->lastname="youtwo";
    	printf("delTemp[0]->lastnamename=%s\n", delTemp[0]->lastname);
    	delTemp[0]->id=22;
    	delTemp[0]->mark=33;
    	printf("deltemp 0 id, mark: %d, %f\n", delTemp[0]->id,delTemp[0]->mark); 
    
    	free(delTemp[0]->firstname);
    	printf("freed fname\n");
    	free(delTemp[0]->lastname);
    	printf("freed lname\n");
    	free(delTemp);
    	printf("freed deltemp\n");
    
    
    
    	return true;
    }
    Code:
    struct StudentRecord
    {
    	char* firstname;
    	char* lastname;
    	int id;
    	float mark;
    };
    I get heap corruption trying to free the memory I just malloced?
    Any suggestions?
    Thanks!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to have a "-1" in there. Why are you using a pointer to a pointer?
    Code:
    struct foo *bar;
    bar = malloc( N * sizeof *bar );
    for( x = 0; x < N; x++ )
    {
        bar[ x ].a = b;
        bar[ x ].y = z;
    }
    ...
    
    free( bar );
    I'm not sure why you're using a pointer to a pointer here.
    delTemp[count]->firstname=(char *)malloc(sizeof(char));//malloc fname
    delTemp[count]->lastname=(char *)malloc(sizeof(char));//malloc lname
    Why are you allocating 1 char for each of their first and last names? Did you mean to actually have more than one character?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-26-2010, 01:48 PM
  2. Help getting runtime double free or corruption error
    By wiggsfly in forum C Programming
    Replies: 9
    Last Post: 10-20-2008, 10:07 AM
  3. Mysterious heap corruption
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2007, 10:55 AM
  4. Free store vs. heap
    By CondorMan in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2006, 11:41 AM
  5. double free or corruption???
    By hwttdz in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2006, 03:02 PM