Thread: Trouble freeing a pointer

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Trouble freeing a pointer

    I have no idea why, but when I try to free a pointer that I allocated memory for, the free fails. I test to see if the pointer is NULL after the free, and it is not. I can still print all the values stored within the pointer.

    What can be the reasoning behind this? Has anyone ever had this problem before?

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    free() does not change the pointer passed in.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    So how is that freeing memory if the values stored within the pointer are not changed?
    Doesn't it take memory to keep those values there?

    In another area of my program, when I free a pointer, the values are not the same.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The memory is released, meaning you shouldn't access this memory through the pointer again. It may retain the same memory contents until this memory is reused for some other allocation.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I suppose the values are still stored there, but to the OS that area is considered free. It's like formatting your hard drive. The same values are still there but it's all considered free space that may be replaced later on. Theres no point in wasting CPU by nulling the bytes.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    I see.

    I now also found out I'm getting segmentation violation when I try to free some of my other pointers, even though I no longer use them. I actually allocated them within in a switch statement. I would assume that it does not make much of a difference from allocating the memory before the switch. Is this a bad idea to allocate the memory in a switch statement?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by magda3227 View Post
    I see.

    I now also found out I'm getting segmentation violation when I try to free some of my other pointers, even though I no longer use them. I actually allocated them within in a switch statement. I would assume that it does not make much of a difference from allocating the memory before the switch. Is this a bad idea to allocate the memory in a switch statement?
    Seg faults when freeing usually means that you overwrote the end of the memory you allocated, or you have already freed it (or your are freeing something incorrect). It may also be caused by trying to free something that isn't actually from malloc().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    I recently came across something that has been giving me seg. viol.

    If I want to create a 2-D array of size 2x6, when I allocate memory for my pointer, I would assume that it needs to be done as such...

    Code:
    int row=2;
    int col=6;
    
    int **pointer=malloc(row*sizeof(int*));
    
    for(i=0;i<col; i++)
    pointer[i] = malloc(col*sizeof(int));
    Correct?

    But for some reason, the pointer will work, but I can't free that pointer...it won't work. I get seg. faults.

    In order to be able to free the pointer I created, it makes me allocate a 6x6 block of memory.

    Why is this so? Am I missing some basic concept about pointers and memory allocation?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are perhaps confusing your colums and rows?

    Based on the above code you would have an array corresponding to int array[2][6].

    If you ever go pointer[2] .. = then you are outside the memory you allocated. If you can fix the problem by using array[6][6], then you are almost certainly likely accessing outside your array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for(i=0;i<col; i++)
    This should be row, not col
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Quote Originally Posted by Salem View Post
    > for(i=0;i<col; i++)
    This should be row, not col
    Thanks. That makes sense.

    I was too stupid to figure that out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 4
    Last Post: 06-15-2005, 08:30 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. ascii and pointer trouble!!!
    By algi in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2004, 03:00 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

Tags for this Thread