-3
while(1){    
static int i=1;   
int *p;    
if(p=malloc(1024*1024))  //assigning 1 mb space    
printf("%d.",i++);    
else    
exit(0);    
}

if the above program run in a system having 1GB ram and in another system having 4GB ram what will be difference in time and value of i ???

  • 1
    There are systems where `malloc()` never fails. Arguably they're bad systems ;) – pmg Feb 12 '13 at 18:15

2 Answers2

0

We don't know, since it's an implementation detail. The C standard says nothing about this.

0

We can't answer that, since it doesn't JUST depend on how much RAM the system has, but also "what else is running on the system" and "what is the configuration of the system". For example, if we have set rlimit in a Linux/Unix system to not allow a process to use more than 256MB, then you will not be allowed to allocate more than 256MB, no matter how many gigabytes or terabytes of RAM the system has.

Also, consider a 1GB system with 6GB of swapspace MAY give you a higher value of i than a system with 4GB of RAM and no swapspace?

There are other variables than "how much RAM is there in the system" that dictates how much RAM your application can allocate.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227