Thread: Issues with calling free() on a malloced double pointer

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    Issues with calling free() on a malloced double pointer

    Hey everyone,

    I was messing around with pointers trying to get a better understanding of them, and I came across something that I haven't been able to solve in regards to freeing a double pointer I malloced.

    Code:
    int main(int argc, char** argv) {
    
        
        char** temp = (char**)malloc(argc * sizeof(char*));
        //free(temp);                  <-------------------------------------------- free's fine here
        char** temp2;
        int i;
        for (i = 0; *argv != NULL; i++)
            *(temp + i) = *argv++;
    
        *(temp + argc) = NULL;
    
        temp2 = temp;
        while (*temp != NULL)
            printf("%s", *temp++);
       
        free(temp2);                 <--------------------------------------------- doesn't free fine here (SIGABRT)
    
        return (EXIT_SUCCESS);
    }
    I don't understand why it free's fine right after I malloc and then when I add strings to the double pointer, print them, and attempt to assign the char** temp to char** temp2 (equivalently storing the value of temp, which was malloced, into temp2 because I increment temp in the while) I run into an error when I free what I malloced...

    This might be because I'm ignorant on the operation of malloc, but if someone could set me straight, I'd appreciate it. Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The line *(temp + argc) = NULL falls off the end of the array you allocated. The result is undefined behaviour.

    It's got nothing to do with a "double pointer". Undefined behaviour is undefined behaviour.

    One common symptom of such undefined behaviour is overwriting some area of memory used later by something else. The internal workings of malloc() and free() are an example of such a "something else".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Free the row pointers (in a loop), first.

    Then free the array pointer.

    Whatever you assign to a malloc'ed array, is not somehow removed when you free the pointer with that address. That value or values, will remain as they are, until the OS (perhaps through your program), see's fit to assign some other data, to that memory.

    The pointer address will not automatically be returned to NULL or something like that. This is like dating - just because you broke up with your ex- that doesn't mean you have a date for Saturday night. It just means you're "available". Same with the RAM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    Free the row pointers (in a loop), first.
    That's actually wrong in this case, as the "row pointers" have not been malloc()ed so should not be freed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Quote Originally Posted by grumpy View Post
    The line *(temp + argc) = NULL falls off the end of the array you allocated. The result is undefined behaviour.

    It's got nothing to do with a "double pointer". Undefined behaviour is undefined behaviour.

    One common symptom of such undefined behaviour is overwriting some area of memory used later by something else. The internal workings of malloc() and free() are an example of such a "something else".
    Well, what you said didn't really help me out at all, but when you mentioned that my pointer fell off the end of the array, it reminded me that I forgot to add room for a NULL character in my malloc call. I went back and did that and it ended up working.

    Next time maybe you can try to be a little less demeaning in your answer. I'm very aware of the consequences involved with undefined behavior, I'm just not use to the mechanics of C.

    Quote Originally Posted by Adak View Post

    Free the row pointers (in a loop), first.

    Then free the array pointer.

    Whatever you assign to a malloc'ed array, is not somehow removed when you free the pointer with that address. That value or values, will remain as they are, until the OS (perhaps through your program), see's fit to assign some other data, to that memory.

    The pointer address will not automatically be returned to NULL or something like that. This is like dating - just because you broke up with your ex- that doesn't mean you have a date for Saturday night. It just means you're "available". Same with the RAM.
    Thanks for the suggestion Adak, however you only free what you malloc and since I only malloced one time, I only free one time. If I attempted to free the individual strings, it would have thrown a SIGABRT because I didn't allocate memory for them since they're arguments to main.

    I just compiled it by attempting to free the strings like you said and it did exactly as I described above.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Of course. I misunderstood your original post.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Next time maybe you can try to be a little less demeaning in your answer.
    Most of the posts on this board are written in a window of like ten minutes. If anyone comes off as demeaning I think you could find it in your heart to let it go without comment. If we took a long time people would complain about that too, so we just can't win, ever.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jshowa View Post
    Well, what you said didn't really help me out at all, but when you mentioned that my pointer fell off the end of the array, it reminded me that I forgot to add room for a NULL character in my malloc call. I went back and did that and it ended up working.

    Next time maybe you can try to be a little less demeaning in your answer. I'm very aware of the consequences involved with undefined behavior, I'm just not use to the mechanics of C.
    .
    My answer pointed you in the right direction, and described the mechanics of what your code was doing wrong. That allowed you to solve the actual problem without being subjected to the indignity of being spoonfed. It was also a fact that your problem had noting to do with your subject line - the problem was because you were falling off the allocated array, and your usage of double pointers was incidental.

    And I'm accused of being demeaning. Welcome to my ignore list - you get no more help from me.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in printf line,,,what's wrong?
    By mft_ika in forum C Programming
    Replies: 9
    Last Post: 03-19-2010, 08:46 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM