Thread: i have a problem with free();

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    i have a problem with free();

    i have this simple program

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *a, int n){
         
         char *d,*c;
         int i;
         d = (char *)malloc(n*sizeof(char));
         c=d;
         n=strlen(a)-n;
         for (i=n;i<strlen(a);i++){
             *(d+i-n) = *(a+i);
             }
             *(d+i-n) = '\0';
        
             return c;
         
         }
         
    int main(void){
        
        char b[]="test";
        
        printf("%s\n",foo(b,2));
    
        getchar();
        
        return 0;
        
    }
    if i put free(d) before return c; it will erase the contents of the array and so no string will be returned, i cant use free(d) in main since we have no d in main, what should i do? how can i free the allocated memory without losing the data that is stored?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nik2
    i cant use free(d) in main since we have no d in main, what should i do? how can i free the allocated memory without losing the data that is stored?
    Have a pointer in the main function to store the return value of foo, then use free on it after printing.
    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
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Have a pointer in the main function to store the return value of foo, then use free on it after printing.
    thanks i made this

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *a, int n){
         
         char *d,*c;
         int i;
         d = (char *)malloc(n*sizeof(char));
         c=d;
         n=strlen(a)-n;
         for (i=n;i<strlen(a);i++){
             *(d+i-n) = *(a+i);
             }
             *(d+i-n) = '\0';
        
             return c;
         
         }
         
    int main(void){
        
        char b[]="test";
        char *res;
        res = foo(b,3);
        
        printf("%s\n",res);
        
        free(res);
        printf("%s\n",res);
    
        getchar();
        
        return 0;
        
    }
    why does it print the same thing after using free()
    i thought it is supposed to print nothing since we free the allocated memory

    thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nik2
    why does it print the same thing after using free()
    It so happens that the data is still in memory. What you did actually results in undefined behaviour.

    Quote Originally Posted by nik2
    i thought it is supposed to print nothing since we free the allocated memory
    Freeing the allocated memory does not make its contents nothing.
    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

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Freeing the allocated memory does not generally change the memory. It just marks it for reuse. Accessing that memory is undefined behavior, and can result in an access violation.

    Some compilers like MVS will mark freed memory in a debug build, but generally the behavior cannot be relied upon.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    so should I free the allocated memory or not?

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You should free it and not try to access it after it has been freed.
    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).

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    ok thanks everyone

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Basically, you're just telling the OS: we're done with this memory.
    Now, what the OS does with it after that is up to the OS. We don't care nor should care. We know the OS does its job, and as far as we are concerned, it is free memory--memory that no longer exists.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need to include <stdlib.h> and get rid of the cast on the return value of malloc.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    I have the same problem so i didnt make a new thread.

    On the other hand one thing is still not clear.

    Quote Originally Posted by laserlight View Post
    Have a pointer in the main function to store the return value of foo, then use free on it after printing.
    But wouldnt it still leave "char* d" in foo() unfreed?

    Thank you!
    Using Windows 10 with Code Blocks and MingW.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    But wouldnt it still leave "char* d" in foo() unfreed?
    It is not the pointer that is being freed, but rather the memory for what the pointer points to. d points to what c points to, and since the value of c is returned, the pointer in the caller would point to the same thing.
    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

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    It makes perfect sense.

    Thank you Laserlight!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM
  5. How can I free what strtok returns?
    By registering in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 04:56 PM