Thread: Decallocate heap memory in different function.

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    24

    Decallocate heap memory in different function.

    Code:
    void *foo(){
        int *x = malloc(56);
        
        return x;
    }
    
    
    int main(){
        int *y = foo();
        free(y);
    
        return 0;
    }

    By freeing 'y' in main function, am I decallocating 'x' in function foo?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    699
    Quote Originally Posted by yj1214 View Post
    Code:
    void *foo(){
        int *x = malloc(56);
        
        return x;
    }
    
    
    int main(){
        int *y = foo();
        free(y);
    
        return 0;
    }

    By freeing 'y' in main function, am I deallocating 'x' in function foo?
    x in foo() is just a local variable that temporarily holds the return address from malloc(). When you return to main(), y now holds that same address returned from foo(). x does not exist at this point. Yes, you are correctly freeing up the memory allocated earlier by malloc() in foo().

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You can check these things yourself using valgrind. Valgrind can help you check for any memory leaks in your program instantly. What you all have to do is compile your code with debugg option enabled (-g).

    Code:
    gcc -Wall -g <youprogram.c> -o <outout>
    valgrind --leak-check=yes <output>
    Those command should reveal that if your program indeed has a memory or leak free. Good tool to get your hands dirty. It helped me a great deal at times.

    ~H
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    699
    Quote Originally Posted by ssharish2005 View Post
    You can check these things yourself using valgrind. Valgrind can help you check for any memory leaks in your program instantly. What you all have to do is compile your code with debugg option enabled (-g).

    Code:
    gcc -Wall -g <youprogram.c> -o <outout>
    valgrind --leak-check=yes <output>
    Those command should reveal that if your program indeed has a memory or leak free. Good tool to get your hands dirty. It helped me a great deal at times.

    ~H
    True, but the OP's code was correct. The OP was just trying to understand what was happening, not how to debug the code. If the OP was taking a course from an instructor, the instructor did not teach the subject very well. I see this too many times.

    You have to understand basic C first, before you can understand the results from an excellent tool like valgrind.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by rstanley View Post
    True, but the OP's code was correct. The OP was just trying to understand what was happening, not how to debug the code. If the OP was taking a course from an instructor, the instructor did not teach the subject very well. I see this too many times.

    You have to understand basic C first, before you can understand the results from an excellent tool like valgrind.
    Indeed - i didn’t say your reply was wrong! I’m giving him more information and pointers for his better understanding and possible explorations. Its upto OP whether to take it or leave it. To that extent I believe my reply is valid.

    ~H
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reallocation of heap memory
    By sukhdeep in forum C++ Programming
    Replies: 17
    Last Post: 12-20-2011, 02:16 AM
  2. Heap Memory
    By strokebow in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2008, 11:10 AM
  3. C++ memory heap questions.
    By lord mazdak in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2005, 10:38 AM
  4. heap memory
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-15-2003, 06:29 PM
  5. heap memory management
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2002, 10:23 PM