linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* strange stack limit behavior when allocating more than 2GB mem on  32bit machine
@ 2009-08-21  3:47 Joe
  2009-08-21  6:58 ` Glynn Clements
  2009-08-21  7:09 ` Michał Nazarewicz
  0 siblings, 2 replies; 10+ messages in thread
From: Joe @ 2009-08-21  3:47 UTC (permalink / raw)
  To: linux-c-programming

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-08-22  7:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-21  3:47 strange stack limit behavior when allocating more than 2GB mem on 32bit machine Joe
2009-08-21  6:58 ` Glynn Clements
2009-08-21  9:12   ` Joe
2009-08-21  9:37     ` Nicholas Mc Guire
2009-08-21 10:33       ` Michał Nazarewicz
2009-08-21  9:38     ` Michał Nazarewicz
2009-08-21 11:04       ` Joe
2009-08-21 12:21         ` Michał Nazarewicz
2009-08-22  7:44         ` Glynn Clements
2009-08-21  7:09 ` Michał Nazarewicz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).