Segmentation fault when freeing memory using malloc and Free()

This is a discussion on Segmentation fault when freeing memory using malloc and Free() within the C Programming forums, part of the General Programming Boards category; Hi, I am attaching this code I wrote for linked list of 3 nodes (2 dummies and 1 node).. For ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Segmentation fault when freeing memory using malloc and Free()

    Hi,
    I am attaching this code I wrote for linked list of 3 nodes (2 dummies and 1 node).. For some reason it is giving me a segmention fault when I run the code. it executes till the last and prints what is stored. The problem occurs at the free() stage. It says segmentation fault.Please help..thanks in advance.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You can't free memory you didn't allocate. You malloc some memory and point to it with ptr. Then, you change ptr to point to &newNode, and lose your handle to the memory you malloc'ed, causing a memory leak. You try to free ptr (pointing to newNode), causing a seg fault. Also, you probably don't want to set tail->next to point back at tail. The traditional sentinel value for end of list is NULL.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    You malloc some memory into ptr, then you overwrite ptr with the address of a local variable. Then, when you try to free(ptr), it fails because it points to a local variable which resides on the stack, and not allocated memory. You don't need the local variable newNode at all.

    Also, tail->next shouldn't point to itself, otherwise you end up with a circular list. It should be set to NULL instead.

    EDIT: too slow!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by livin View Post
    Hi,
    I am attaching this code I wrote for linked list of 3 nodes (2 dummies and 1 node).. For some reason it is giving me a segmention fault when I run the code. it executes till the last and prints what is stored. The problem occurs at the free() stage. It says segmentation fault.Please help..thanks in advance.
    Please... post your code HERE in the open...

    A lot of us will (understandably) not want to download files of unknown content.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

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