I have a function, foo(), that allocates memory and returns it. Is it standard practice for me to free it at the end of my main function?
char* foo(){
char * p;
p = malloc(sizeof(char) * 4); /* edit - thanks to msg board */
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
p[3] = '/0'; /* edit: thanks to the msg board. */
return p;
}
int main(int a开发者_JAVA技巧rgc, char *argv[])
{
char * p2;
p2 = foo();
printf("%s", p2);
free(p2);
return 0;
}
Freeing at the end of main()
would be the correct thing to do, yes. You might think about null terminating that string, though. A more idiomatic design is do to all the memory management "at the same level", so to speak. Something like:
void foo(char *p)
{
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
p[3] = '\0';
}
int main(int argc, char **argv)
{
char *p2 = malloc(4);
foo(p2);
printf("%s", p2);
free(p2);
return 0;
}
"Standard practice", if there is such a thing, is to free the memory as soon as you're done with it.
My personal belief is that you should free memory even if you know the program is about to end just because it's good practice, and because someday, somehow, you're going to encounter an environment that doesn't automatically release malloced memory when the program exits.
You need to free when you no longer need reference to that variable. However when your program exits all resources allocated to it are released.
And YES its a good practice to free it when you are done using it even if the program exits on the next line.
and why would you malloc(sizeof(int) * 3) when you're allocating for a 3 char's?
I would have thought it would be better practice to allocate and free the memory in the same function (in this case, main()).
For example:
void foo(char *p)
{
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
return ;
}
int main(int argc, char *argv[])
{
char * p2;
p = malloc(sizeof(int) * 3);
foo(p2);
printf("%s", p2);
free(p2);
return 0;
}
I don't know if it's "standard practice", but the free()
is necessary or you will leak memeory.
I would make two functions, new_foo()
and delete_foo()
, like this:
char* new_foo()
{
char * p;
p = malloc(sizeof(int) * 4);
/* ... */
return p;
}
void delete_foo(char* p)
{
free(p);
}
int main()
{
char * p2;
p2 = new_foo();
/* ... */
delete_foo(p2);
return 0;
}
This "makes more sense" in my opinion.
One good reason to do it is so that you have matching malloc's/free's. If code gets moved around, a programmer will naturally look for the matching free to go with the malloc. If it's not there, it might be a source of confusion (hopefully not for long though).
In short, it makes the resource usage more evident.
The general rule is that memory should be freed as soon as you notice that you no longer need it. In the particular case of memory that is needed until the program terminates, there are two valid points of view.
Keep it simple: for every
malloc
there must be a correspondingfree
. The main benefit is that if you ever want to convert your standalone program into a component of a bigger program, yourmain
will turn into a library call, and the calls tofree
will be required.Keep it simple: freeing a program's resources is the OS's job. This is a valid management strategy — the program's memory is a region of memory that is freed in one swoop when the program exits. The main benefit is that you don't need to keep track of each allocated block, which can be hard sometimes (but then you might want to consider a garbage collector).
精彩评论