Freeing pointers in structures

This is a discussion on Freeing pointers in structures within the C Programming forums, part of the General Programming Boards category; Hi, I've written a program that uses a large amount of memory. This involves many linked lists made up of ...

  1. #1
    jim50498
    Guest

    Freeing pointers in structures

    Hi,

    I've written a program that uses a large amount of memory. This involves many linked lists made up of structures that contain pointers such as

    Struct apple{
    int *shell;
    int *core;
    int pips;
    int *next;
    }
    These pointers are used during the execution with allocation calls to malloc.

    My question is that when I free a structure like this can I just free the pointer to the structure (e.g. to free the next record of the linked list could I just call free on apple->next) or do I have to free all the pointers in the structure first.

    thanks for any help

    Jim

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    You should free all the pointers in the head node (except for next), then free the head node itself and move on to the next node in the list.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Unregistered
    Guest

    Talking

    first clear all the pointers in the structures and then clear the pointer to the structure

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    28
    just curious...but any reason why u're using pointers to ints instead of just having straight out integers.

    if u're gonna be using pointers anyways...why not making void *, u could use them then to point to different data types.

    just a thought.
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >but any reason why u're using pointers to ints instead of just
    >having straight out integers.

    If shell and core are dynamically created lists of ints.

    >if u're gonna be using pointers anyways...why not making void
    >*, u could use them then to point to different data types.

    Could be, but why do that in case you already know you're always going to use ints?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic Pointers to structures
    By dunxton in forum C Programming
    Replies: 8
    Last Post: 02-20-2009, 09:23 AM
  2. pointers in structures
    By esi in forum C Programming
    Replies: 2
    Last Post: 04-13-2007, 12:41 PM
  3. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM

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