strange stack limit behavior when allocating more than 2GB mem on 32bit machine
Joe <[email protected]>
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
Hi,
Here I encounter something which I can't understand.
What I want to do is to allocate ~2.5GB mem, it fails when stack limit
is unlimited, but succeeded when stack limit is 10240.
Here is the code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *p;
long i;
size_t n;
if (argc != 2 || atol(argv[1]) <= 0) {
fprintf(stderr, "usage: malloc value (MB)\n");
return 1;
}
n = 1024 * 1024 * atol(argv[1]);
if (!(p = malloc(n))) {
perror("malloc failed");
return 2;
}
printf("Malloc succeeded\n");
free(p);
return 0;
}
and here is what confused me:
$ uname -a
Linux stone 2.6.9-11.ELsmp #1 SMP Fri May 20 18:26:27 EDT 2005 i686
i686 i386 GNU/Linux <==== 32bit system
$ free -m
total used free shared buffers cached
Mem: 2024 1996 28 0 30 1401
-/+ buffers/cache: 563 1461
Swap: 10236 0 10236
$ ulimit -s
10240
$ ./malloc 2500
Malloc succeeded <======= succeeds when stack limit is 10240
$ ulimit -s unlimited
$ ./malloc 2500
malloc failed: Cannot allocate memory <======== fails when stack
limit is unlimited???
BTW, there is no such problem on 64bit machine
Could you please give some insight on this?
Regards