Thread: Other programming questions: segfault in free

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Question Other programming questions: segfault in free

    Hi all,

    I need help in solving a segmentation fault in free. I don't understand why it is giving a segfault even if the address is valid. When I tried to print the pointer passed to free. i is printing a valid address and also the correct content at that address. Then why is it giving a segfault. I have tried it in gdb the result is as follows :-
    Here pResp is the pointer i'm tring to free.

    [GDB]
    817 printf("%s - Resp for operation\n",pResp);
    (gdb) n
    Added - Resp for operation
    819 pResp = pTemp;
    (gdb) print pResp
    $4 = 0x80506b5 "Added"
    (gdb) n
    820 free(pResp);
    (gdb) print pResp
    $5 = 0x8050698 "\001"
    (gdb) print pTemp
    $6 = 0x8050698 "\001"
    (gdb) s

    Program received signal SIGSEGV, Segmentation fault.
    0x420744fe in _int_free () from /lib/tls/libc.so.6
    (gdb) print presp
    No symbol "presp" in current context.
    (gdb) print pResp
    No symbol "pResp" in current context.
    (gdb) up
    #1 0x420734d6 in free () from /lib/tls/libc.so.6

    [/GDB]

    Please do tell me where i'm going wrong

    Thanks,
    Neeharika.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    void free ( void * x );

    u have to remember that u r working with a pointer,a pointer is just a variable where a memory address is stored.

    so
    Code:
    char *x=(char*)malloc(20);
    free(x);
    this will work;

    but
    Code:
     
    char *x=(char*)malloc(20);
    x="wwwww"; //this reassigns memory address stored at x.
    free(x);
    this will generate an error;

    because free can only be used on a previously allocated memory block,but here x is pointing to the start address of the array "wwww" .


    so if u want to use the free ,your code should look like this:

    Code:
    #include<stdlib.h>
    
    main(){
    char *pResp;
    char *pTemp;
    pResp=(char*) malloc(20);
    pTemp ="wwwww";
    strcpy(pResp,pTemp);
    printf("%s - Resp for operation\n",pResp);
    free(pResp);
    }
    Last edited by qqqqxxxx; 02-21-2006 at 05:41 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Please do tell me where i'm going wrong
    Well not posting your source code is a big hindrance to giving you a definitive answer.

    Any single malloc / free call can seem perfectly valid, because your mistake can be caused by any prior use of malloc / free which was actually wrong, but did not cause a crash.

    Likewise, any prior use of memory you got from malloc, which you trashed with a buffer overflow can cause seemingly unrelated free calls to fail badly.

    Not to mention prior calls to free of memory you already freed, or memory which wasn't allocated by malloc in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM