0

When I allocate memory from the heap and don't free it (by accident most likely!), are those memories freed up automatically by the os when the program quits?

Something like this

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int *m = malloc(100);
  printf("%p", m);
  return 0;
}

What happens to those 100 allocated bytes when the program quits the main?

Student
  • 708
  • 4
  • 11
  • Yes, the OS will clean up all of the process's memory on exit. – 0x5453 Jul 15 '21 at 19:20
  • Yes, all memory segments of a process will be cleaned on exit, including the heap, which is where dynamic memory is allocated. – h0r53 Jul 15 '21 at 19:20
  • 2
    If it were possible for a careless program to "lose" memory -- if the operating system did *not* clean it up, perfectly reliably -- that would be a really horrible operating system! – Steve Summit Jul 15 '21 at 19:47

0 Answers0