According to this question and this one, the operating system is freeing wathever you've malloced in your program as long as you close it, moreover in a simple program like mine it's not so important.
Sure in very short running simple programs, failing to free memory won't have a noticeable effect. jaredpar
According to man :
The malloc() function allocates size bytes [...]
So, I have used malloc to allocate an array of integer with 20 bytes of memory (sizeof int
is 4 bytes on my system) :
array = malloc(5 * sizeof(int));
Therefore I should be able to write just 5 integers (5 * 4 bytes = 20 bytes = 5 integers). Now this program here should create a core dump, but this is not the case, for some reason :
#include <stdio.h>
#include <stdlib.h>
int main(void){
int *array = NULL, i = 0;
array = malloc(5 * sizeof(int));
if(array == NULL)
{
printf("\nERROR : Out of memory.\n");
return 1;
}
for(i = 0; i < 10; i++)
scanf("%d", &array[i]);
return 0;
}
This program lets me input 10 integers without the creation of a coredump...Instead using free(array)
at the end of the program will correctly result in the creation of a core dump. Isn't that strange? Shouldn't both programs create a core dump? Why doesn't using free() doesn't create a core dump?
I know what a core dump is and how to avoid it, my question is purely curiosity as, for me, this is a strange behaviour.