how to free memory
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: how to free memory

Hybrid View

  1. #1
    Join Date
    Mar 2004
    Posts
    216

    how to free memory

    I've got a char array

    Code:
    char *vec;
    and i allocate memory for a number of elemets

    Code:
    vec = (char*) malloc ((sizeof(char))*(number_of_elemets));
    i do that in main.
    then i call a free function

    Code:
    free_mem (vec);
    the function code is:

    Code:
    void free_mem (char *vec,number_of _elements)
    {   
            for (i=0;i<(number_of _elements);i++)
    	{
                     	free (vec[i]);
    	}
    }
    it gives me errors
    what's wrong?
    thanks!!

  2. #2
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    free() takes in a address as argument

    you can just do this,

    free(vec);

  3. #3
    Join Date
    Mar 2004
    Posts
    216
    i tried but it gave me a runtime error when it gets to free (vec)

    NOTE: i can only use ansi c, that's why i use malloc instead of new

  4. #4
    Join Date
    Apr 1999
    Posts
    27,426

    Re: how to free memory

    Originally posted by kfaday
    Code:
    void free_mem (char *vec,number_of _elements)
    {   
            for (i=0;i<(number_of _elements);i++)
           {
                   	free (vec[i]); // Wrong
          }
    }
    This is totally wrong.

    You call free() on the value returned by malloc(). You are not doing this in the code you're showing -- you're calling free() on each of the characters, which is invalid and leads to undefined behavior.
    Code:
    free(vec);
    That's it.
    i tried but it gave me a runtime error when it gets to free (vec)
    Then you are corrupting the heap somewhere else in your program. You will see that there is nothing wrong when you do this:
    Code:
    #include <stdlib.h>
    
    int main()
    {
       char *vec;
       int number_of_elements = 100;
       vec = (char*) malloc ((sizeof(char))*(number_of_elemets));
       free(vec);
    }
    Do you get a runtime error when you run this program? If not, it is what you're doing in your program between the calls to malloc and free that is causing the problem.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729
    You don't need the loop, just
    Code:
    free(vec)
    .
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well..just out of curiosity...is there any special need that you are using a C character array (like limited to C programming)? Otherwise, I would suggest using one of the C++ standard classes 'vector' or 'string' (depending on what your array will contain) which contains their own memory management...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  7. #7
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    296
    just do:
    Code:
    void free_mem (char *vec)
    {   	
      free (vec);
    }
    then free_mem is not necessary..
    You´re not allocating an array. You´re allocating a continuos block of memory so you have to deallocating it once.

    Rabelo

  8. #8
    Join Date
    Mar 2004
    Posts
    216

    i got it

    i was writing one position over what i reserved
    i reserved x places and i wrote the x+1 place
    now i can do free (vec) and it works
    thanks

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: i got it

    Originally posted by kfaday
    i was writing one position over what i reserved
    i reserved x places and i wrote the x+1 place
    now i can do free (vec) and it works
    thanks
    And that is one of the best reasons for using the standard 'vector' class instead...as long as you are programming in C++...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center