Thread: free() not working?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    free() not working?

    i was playing with malloc() and free() and i notice that free() is not deallocating the memory

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        
        double *pInt = (double *) malloc(7*sizeof(double));
        
        free(pInt);
      
        for (int j = 0; j < 7; j++)
        {
            *(pInt + j) = j;
        }
        
        for (int k = 0; k < 7; k++)
        {
            printf("Element is %e at address %p\n",*(pInt+k),pInt+k);
        }
        getchar();
        return 0;
        
    }
    i was expecting the program to terminate without displaying the output but it ran surprisingly and it displayed the output.

    why is this so? maybe i don't know how free() works.

    i ran this under dev-cpp btw.
    Last edited by scout; 10-19-2010 at 11:14 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You observed that free did not overwrite the memory deallocated. You did not observe that free did not deallocate the memory.

    Note that what you did actually results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    To test if your frees and mallocs work I suggest using Valgrind.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    free() puts the memory's "name" back into the singles list. It doesn't guarantee mr. RAM a date on Saturday night.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Yes this is a rather surprising result. I have put entire strings into buffers and then freed them and printf'd them. It's not correct, and sometimes you can crash if the operating system actually collects the memory. But in general, the memory you alloc'd will be marked for freeing at some later point in time. The free() function is not guaranteed to immediately remove it from your address space. However, if you valgrind myprogram, you will find errors of the form "Invalid read/write of size 4... Address [blah] is n bytes passed a block of 28 free'd"

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    thanks for the replies, guys

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by scout View Post
    thanks for the replies, guys
    One other thing they didn't touch on...

    Calling free() does not reset the value in your pointer...

    All it really does is mark the memory as "available" to the system. Your buffer content will remain in place until it's overwritten by something else.
    Last edited by CommonTater; 10-19-2010 at 04:21 PM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Oh right, useful macro
    Code:
    #define FREE(ptr) if(ptr != NULL) free(ptr); ptr = NULL

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Checking for NULL doesn't buy you much since free(NULL) is a no-op.
    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.

  10. #10
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    ln that spa
    Quote Originally Posted by CommonTater View Post
    One other thing they didn't touch on...

    Calling free() does not reset the value in your pointer...

    All it really does is mark the memory as "available" to the system. Your buffer content will remain in place until it's overwritten by something else.
    i am agree with you,
    in computer, actually nothing is deleted, what just happens
    is that space is made available for other things and if os chooses that space to store other data, then it sinply overwrites on it.
    The same happens when we use free( ). If we have allocated some space for a variable, no one except that variable can access that. When we free memory allocated for a variable, those memory locations are made available to use by other things. If nothing is overwritten on that deallocated space, you can access the data written on it. If you want data tobe unacessible, make pointer to null.
    It's not missbehaviour of free function, it's doing it's job well, the thing is what we want, deallocation or deallocation with losing data.
    But a nice question

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Adak View Post
    free() puts the memory's "name" back into the singles list. It doesn't guarantee mr. RAM a date on Saturday night.
    Quote Originally Posted by CommonTater View Post
    One other thing they didn't touch on...

    Calling free() does not reset the value in your pointer...

    All it really does is mark the memory as "available" to the system. Your buffer content will remain in place until it's overwritten by something else.
    Or to put it another way (Since I liked Adak's analogy):
    It doesn't mean your phone number magically disappears from your ex's cellphone either.
    Last edited by iMalc; 10-20-2010 at 12:24 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by iMalc View Post
    Or to put it another way (Since I liked Adak's analogy):
    It doesn't mean your phone number magically disappears from your ex's cellphone either.
    Lol. I like this analogy better.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free function
    By rakeshkool27 in forum C Programming
    Replies: 6
    Last Post: 06-06-2010, 03:29 PM
  2. Question about Free() and Ptr-Ptr-Int
    By Kehyn in forum C Programming
    Replies: 11
    Last Post: 04-14-2010, 08:37 PM
  3. Help explaining test questions
    By Sentral in forum General Discussions
    Replies: 26
    Last Post: 11-09-2009, 11:10 PM
  4. Yet another free() question: user breakpoint called
    By ArchitectureinH in forum C Programming
    Replies: 3
    Last Post: 07-20-2005, 10:47 AM
  5. Replies: 12
    Last Post: 06-24-2005, 04:27 PM