Thread: How to handle mem alloc errors?

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    How to handle mem alloc errors?

    The following code is part of a linked list implementation.

    Code:
    // sllist.c
    
    #define T SLList_T
    typedef struct T *T;
    
    struct T {
        T     next;
        void *head;
    };
    
    T sllist_copy(T list) {
        T head, *p = &head;
        
        while (list) {
            *p = malloc(sizeof **p);
            if (!(*p)) {
                sllist_free(p);
                return NULL;
            }
            
            (*p)->head = list->head;
            p = &(*p)->next;
    
            list = list->next;
        }
      
        return head;
    }
    
    void sllist_free(T *list) {
        assert(list);
    
        T next;
        while (*list) {
            next = (*list)->next;
            free(*list);
            *list = next;
        }
    
        *list = NULL;
    }
    a) I would like your confirmation that the for loop in list_copy() will properly protect against a memory leak in case of a mem allocation error anytime during the loop execution.

    b) I'd like to know if there is a way (GCC or GDB options would be fine too) for me to simulate an allocation error that could trigger during loops like the above. Some of those loops can be more complex and I'd like to test any checked and unchecked conditions on them.
    Last edited by Mario F.; 10-22-2016 at 02:14 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would pass &head to sllist_free(). What you're actually passing in is the list, starting at the point of failure, and I don't think that's what you want. There's no way to travel back to the head from there.

    I'm not sure there is one. I think the best thing to do would be to use a VM and simulate low memory conditions and induce failure.

    HTH

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Good catch whiteflags. Thanks.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which is a better practice? (Global handle or finding handle)
    By C_Sparky in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2010, 07:32 PM
  2. dyn mem alloc
    By jimmianlin in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 02:46 AM
  3. how much should i handle errors ?
    By jabka in forum C Programming
    Replies: 4
    Last Post: 09-07-2007, 05:43 PM
  4. alloc.h
    By Drako in forum C Programming
    Replies: 2
    Last Post: 04-08-2002, 10:22 AM

Tags for this Thread