0

What is the ownership of the dynamic allocated memory?

For example, in one program dynamic allocate memory like this:

int main()
{
   int * p = malloc(10 * sizeof(int));

   return 0;
} 

1) if this program exit, then will the memory pointed by p be freed()? I think the answer will be, it will not be freed automatically.

2) if this program crash, then will the memory pointed by p be freed()?

ratzip
  • 1,571
  • 7
  • 28
  • 53

3 Answers3

1

Memory Deallocation And Allocation

This link will help you to a great extent to solve your problem

The text below is from the above mentioned link:-

Just about every modern operating system will recover all the allocated memory space after a program exits. The only exception I can think of might be something like Palm OS where the program's static storage and runtime memory are pretty much the same thing, so not freeing might cause the program to take up more storage. (I'm only speculating here.)

So generally, there's no harm in it, except the runtime cost of having more storage than you need. Certainly in the example you give, you want to keep the memory for a variable that might be used until it's cleared.

However, it's considered good style to free memory as soon as you don't need it any more, and to free anything you still have around on program exit. It's more of an exercise in knowing what memory you're using, and thinking about whether you still need it. If you don't keep track, you might have memory leaks.

On the other hand, the similar admonition to close your files on exit has a much more concrete result - if you don't, the data you wrote to them might not get flushed, or if they're a temp file, they might not get deleted when you're done. Also, database handles should have their transactions committed and then closed when you're done with them. Similarly, if you're using an object oriented language like C++ or Objective C, not freeing an object when you're done with it will mean the destructor will never get called, and any resources the class is responsible might not get cleaned up.

Community
  • 1
  • 1
Kunal Saini
  • 138
  • 10
0

On linux, as long as the process terminated, all its memory will be freed, for reuse.

If your program runs long term(e.g shell or web server daemon), you should free every piece of memory by hand, otherwise, as time goes the limit of memory might be reached, and you can no long allocate memory.

If your program runs very short, you can leave the free job be done automatically when process terminate. Actually, free() itself also takes time, so this would save some machine resource, even though it's strongly not suggested.

free() every piece of memory by hand, also improve your maintainabiliy & readability of your project for long term.

Eric
  • 22,183
  • 20
  • 145
  • 196
0

referred : GNU libc manual page for free()

So, according to libc manual

There is no point in freeing blocks at the end of a program, because all of the program’s space is given back to the system when the process terminates.

So even if your program crashed, memory will be freed and given back to system

Amol Saindane
  • 1,568
  • 10
  • 19