Thread: a noob freeing memory...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    Thumbs down a noob freeing memory...

    supposed we have a this

    Code:
    int func(){
       int *p;
    
       p=malloc(some,sizeof some);
    
       return something_else;
    }
    lets suppose we don't free() the allocated memory. what happens with the allocated space when we call again the function??? is it freed or do we have n-allocations for n-function-calls???

  2. #2
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Hmm, what is the lifetime of local variables?? i think it's just within the function. then, it's "destroyed". im not so sure though... why am i even replying if i aint sure.. LOL
    It is not who I am inside but what I do that defines me.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is it freed or do we have n-allocations for n-function-calls???
    Yes. This is called a memory leak.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by Prelude
    >is it freed or do we have n-allocations for n-function-calls???
    Yes. This is called a memory leak.
    i can guess that there will be problems if i free(p) and try to return p with int *f() ???

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i can guess that there will be problems if i free(p) and try to return p with int *f() ???
    Good guess. That has a special name too, it's a dangling pointer.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by Prelude
    >i can guess that there will be problems if i free(p) and try to return p with int *f() ???
    Good guess. That has a special name too, it's a dangling pointer.
    is there any way to retrieve the pointer value externally while being able to free the memory it allocates? i guess that when the function returns all the allocated space will be unreachable to free().

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there any way to retrieve the pointer value externally while being able to free the memory it allocates?
    Return the pointer. Basically, as long as you have a pointer to the memory, you're capable of freeing it, regardless of whereever in the code you are. Once you lose the last reference to the memory, you've leaked it.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by Prelude
    >is there any way to retrieve the pointer value externally while being able to free the memory it allocates?
    Return the pointer. Basically, as long as you have a pointer to the memory, you're capable of freeing it, regardless of whereever in the code you are. Once you lose the last reference to the memory, you've leaked it.
    thanks alot!!!

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    create,return,free

    i want to initialise,return and free a variable length 2-d array...
    suppose i do this:
    Code:
    int **f(some input){
    
      int **p;
      p=malloc(dim_1*sizeof(int *));
    	
      for(i=0;i<dim_2;i++)	p[i]=malloc(dim_1*sizeof(int));
    
      blah blah blah...
    
      return p;
    }
    first of all, is my initialization right? is there anything illegal in it? it surely works for me but is there a more right way to do it?

    secondly how do i free tis pointer if i return it in main??? i have tried many ways but still i run valgrind mem test on my code and get memory leaks...

    if i dont return i do this inside my function and have no problems:
    Code:
    for(i=0;i<dim_2;i++)	free(p[i]);
    free(p);
    no memory leaks nothing...

    but if i do the same thing outside the function with its reference i still get memory leaks.
    any suggestions?
    thanks

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >first of all, is my initialization right?
    No, it's not.
    > for(i=0;i<dim_2;i++) p[i]=malloc(dim_1*sizeof(int));
    This should be:
    Code:
      for(i=0;i<dim_1;i++)	p[i]=malloc(dim_2*sizeof(int));
    >secondly how do i free tis pointer if i return it in main???
    Like so:
    Code:
       int **array;
    
       array = f(some input);
    .
    .
       for (i=0; i<dim_1; i++)
          free(array[i]);
       free(array);
    Last edited by swoopy; 10-31-2006 at 05:03 PM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And I believe it's recommended to do it like this (Hopefully I have this right. Someone correct me if I have this wrong):
    Code:
      int **p;
      p=malloc(dim_1*sizeof(*p));
    	
      for(i=0;i<dim_1;i++)	p[i]=malloc(dim_2*sizeof(**p));

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    i did as you said. it really works internally but when i free it from main i still get leaks.
    any suggestions? i would post code but its pretty huge. i'll try to cut things out and maybe post it...

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i did as you said. it really works internally but when i free it from main i still get leaks.
    >any suggestions?
    It sounds like the memory for your array is getting corrupted at some point after the malloc and before the free. You could print the array at various points in main to see where the corruption occurs.

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by swoopy
    >i did as you said. it really works internally but when i free it from main i still get leaks.
    >any suggestions?
    It sounds like the memory for your array is getting corrupted at some point after the malloc and before the free. You could print the array at various points in main to see where the corruption occurs.
    if i don't free in main the code seems to leak exactly the double amount of bytes. doesn't this mean that the memory allocated in int **f() is never freed? but i can't free it before i return it.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >doesn't this mean that the memory allocated in int **f() is never freed?
    Sure. But think about it logically. When free() is eventually called in main, free() has information stored about that pointer, which tells it how much memory was allocated and where that memory is located. Well what happens if you somehow overwrite that information between the time you call malloc() and free()? Then free() will never be able to deallocate that memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 02:08 AM
  2. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 01:30 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM