Thread: Free linked list in 2D list

  1. #16
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by christop View Post
    I'm pretty sure you're referring to me.
    No. What I wrote was not intended as passive-aggressive sniping, but as the literal description of my observable behaviour on this board.

    Your post #9 required no comment; it is just fine as it is.

    In other words, being able to modify some data stored in memory is separate from telling the OS or C library that that dynamically allocated memory is no longer needed. So, I see no problem with freeing pointers to const data.

    (There is one minor wrinkle: string constants. If the interfaces will be used by other programmers, then catching errors analogous to free("foobar"); might be useful. It is not easy to catch at run time in any portable fashion -- unlike, say, NULL pointers --, but if your dynamically allocated pointers are non-const, you either have code that does casts (and thus need a careful review anyway), or you get a compile-time warning. But this basically only applies to char pointers, and it really is a minor detail. I'm not even sure it was worth pointing it out.)

    Quote Originally Posted by christop View Post
    How would you implement free_person()?
    Because I like to poison the fields when freeing, I wouldn't use const. I also prefer to keep associated data local, so my structure would more likely be
    Code:
    typedef struct {
        size_t offset;
        size_t length;
    } part;
    
    struct person {
        part   fname;
        part   lname;
        size_t length;
        char   name[];
    };
    where the idea is to store the person's name in preferred representation in ->name, and slice it if needed to extract the first and last name parts. At minimum, I'd have the offset of the last name for sorting,
    Code:
    struct person {
        size_t last;
        char  name[];
    };
    That requires C99 (or at least flexible array member) support.

    For an existing codebase, I'd be happy to use your code, especially so if I saw a
    Code:
    /* Macro to avoid sprinkling ugly casts everywhere */
    #define free_const(p) free((void *)(p))
    macro somewhere near the top.

  2. #17
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by Nominal Animal View Post
    No. What I wrote was not intended as passive-aggressive sniping, but as the literal description of my observable behaviour on this board.
    I misjudged you, and I apologize for that.

    Quote Originally Posted by Nominal Animal View Post
    For an existing codebase, I'd be happy to use your code, especially so if I saw a
    Code:
    /* Macro to avoid sprinkling ugly casts everywhere */
    #define free_const(p) free((void *)(p))
    macro somewhere near the top.
    That's nice to hear, though I would prefer to define free_const as an inline function as it makes a call like free_const(7) a compiler error (the macro happily casts the int to a pointer).

    Other than that, I agree with the rest of your post. You've got a good point about poisoning your objects, which I haven't done but looks like a nice technique.

  3. #18
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by christop View Post
    I would prefer to define free_const as an inline function
    Yes, that would be even better; catching bad usage at compile time is useful.

    Using GCC, static inline functions are as fast as macros, so there are no downsides in having it be a static inline function, compared to a macro, that I can see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in free() of linked list
    By iHateSicsic in forum C Programming
    Replies: 7
    Last Post: 11-14-2010, 02:36 PM
  2. free the memory of linked list.
    By xinwu in forum C Programming
    Replies: 2
    Last Post: 11-02-2010, 01:48 PM
  3. Free a linked list
    By thescratchy in forum C Programming
    Replies: 6
    Last Post: 08-01-2010, 03:03 PM
  4. free linked list
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-18-2002, 06:42 PM
  5. How do I free a singlely linked list?
    By mlupo in forum C Programming
    Replies: 6
    Last Post: 12-05-2001, 04:27 AM

Tags for this Thread