Thread: Malloc - Free giving double free or corruption error

  1. #1
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007

    Question Malloc - Free giving double free or corruption error

    Hey, having problems with what i thought was a simple piece of code, but when inputting anything more than 3 names, it ........s itself, both code and dumps are shown.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_LENGTH 255
    //declare pointer and other vars
    	char **pt_bottom,**pt_counter,**pt_top;
    	int list_length;
    	char name[MAX_LENGTH];
    //read in first int as length of the array
    	printf("Enter the number of names to be entered into the list:");
    //malloc pt_array to bottom and set the counter to bottom and top to the top of the list
    	//	printf("Allocating %d to pt_bottom\n",list_length*sizeof(char));
    //for loop to get, trim, and malloc names
    		printf("Enter a name:");
    		//	printf("Allocating %d to pt_counter\n",sizeof(char)*(strlen(name)+1));
    //for loop to print names reversed
    	printf(":Results List:\n");	
    		printf("%s\n",*pt_counter);		//on the last loop this is printing incorrectly (always "X?"
    		//	printf("Printed\n");		
    		free(*pt_counter);			//this is where its failing on the last loop....
    		//	printf("Freed\n");
    }//not working on anything more than 3 names, malloc error
    *** glibc detected *** ./ex_13: double free or corruption (out): 0x0804a018 ***
    ======= Backtrace: =========
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 03:02 822669     /home/bolster/School/advanced_c/Coursework/ex_13
    08049000-0804a000 rw-p 00000000 03:02 822669     /home/bolster/School/advanced_c/Coursework/ex_13
    0804a000-0806b000 rw-p 0804a000 00:00 0          [heap]
    b7c00000-b7c21000 rw-p b7c00000 00:00 0 
    b7c21000-b7d00000 ---p b7c21000 00:00 0 
    b7da9000-b7daa000 rw-p b7da9000 00:00 0 
    b7daa000-b7eee000 r-xp 00000000 03:02 574702     /lib/tls/i686/cmov/
    b7eee000-b7eef000 r--p 00143000 03:02 574702     /lib/tls/i686/cmov/
    b7eef000-b7ef1000 rw-p 00144000 03:02 574702     /lib/tls/i686/cmov/
    b7ef1000-b7ef4000 rw-p b7ef1000 00:00 0 
    b7efb000-b7f05000 r-xp 00000000 03:02 540678     /lib/
    b7f05000-b7f06000 rw-p 0000a000 03:02 540678     /lib/
    b7f06000-b7f0a000 rw-p b7f06000 00:00 0 
    b7f0a000-b7f24000 r-xp 00000000 03:02 541509     /lib/
    b7f24000-b7f26000 rw-p 00019000 03:02 541509     /lib/
    bf85b000-bf870000 rw-p bf85b000 00:00 0          [stack]
    ffffe000-fffff000 r-xp 00000000 00:00 0          [vdso]
    Aborted (core dumped)

    Running on ubuntu 7.10

    A fresh pair of eyes would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > pt_bottom=malloc(list_length*sizeof(char));
    You didn't allocate enough memory. You're trying to allocate space for char* pointers, not chars.

    Always use this idiom
    p = malloc ( num * sizeof *p );

    Which in your case would be
    pt_bottom=malloc(list_length*sizeof *pt_bottom );

    Then check your other malloc calls as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007

    Talking *Praises*

    thanks, that solved it. I thought pointers were of size char but i guess that they're dynamically sized by the compiler on the system or some such.

    Anyway, just saying thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM