i have a problem with free();

This is a discussion on i have a problem with free(); within the C Programming forums, part of the General Programming Boards category; i have this simple program Code: #include <stdio.h> #include <string.h> char *foo(char *a, int n){ char *d,*c; int i; d ...

  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
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    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
    Posts
    23,013
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    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
    812
    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!
    Compiler MSVC++ 2013 with Code::Blocks.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    Thank you Laserlight!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 06:20 AM
  2. Replies: 12
    Last Post: 06-24-2005, 05: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, 02:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 08:52 AM
  5. How can I free what strtok returns?
    By registering in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 05:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21