I am a newbee in C
I wanted to know the max memory allowed by an application.
So I wrote a little program like the following.
I have a machine with 16GB total memory and 2GB is used and 14GB is free.
I expected this program to stop around 14GB, but it runs forever.
Want am I doing wrong here?
#include <stdlib.h>
#include <stdio.h>
int main(){
long total = 0;
void* v = malloc(1024768);
while(1) {
total += 1024768;
printf ( "Total Memory allocated : %5.1f GB\n", (float)total/(1024*1024768) );
v = realloc(v, total);
if (v == NULL) break;
}
}
Edit: running this program on CentOS 5.4 64 bit.
On most modern operating systems, memory is allocated for each page which is used, not for each page which is “reserved”. Your code doesn’t use any pages, so no memory is really allocated.
Try clearing the memory you allocate with
memset; eventually the program will crash because it can no longer allocate a page.I tried to find a citation for this, but I was unsuccessful. Help with this is appreciated!