I'm taking a course on C (Software/Hardware Interfaces) and we just talked about using malloc in lecture the other day, so i decided to play with it and see how much memory I could allocate before receiving a failure. I made a program which looped a call to malloc that allocated a meg, and counted how many megs it could allocate before failing- but on my 1 gig laptop the final result was that it could allocate 2 gigs before failing o.O
Then I tried the below script to allocate half a gig, and opened Task Manager to see how much memory was actually being taken up, and it was less than a MB
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
void *test;
int megabytes = 512;
if((test = malloc(1024*1024*megabytes))==0)
{
printf("ALLOC FAIL"); fflush(stdout);
}
else
{
printf("ALLOC SUCCESS");
}
while(1)// hangs program so it doesn't close, and can be viewed in task manager
{
}
return 0;
}
Can anyone explain what's going on? TIA