-
April 12th, 2004, 07:48 AM
#1
how to free memory
I've got a char array
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
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!!
-
April 12th, 2004, 07:56 AM
#2
free() takes in a address as argument
you can just do this,
free(vec);
-
April 12th, 2004, 08:01 AM
#3
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
-
April 12th, 2004, 08:25 AM
#4
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.
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
-
April 12th, 2004, 08:26 AM
#5
You don't need the loop, just
.
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html
-
April 12th, 2004, 08:30 AM
#6
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...
-
April 12th, 2004, 08:31 AM
#7
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
-
April 12th, 2004, 07:06 PM
#8
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
-
April 13th, 2004, 04:13 AM
#9
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++...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|