From: Joe <longapple@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: strange stack limit behavior when allocating more than 2GB mem on 32bit machine
Date: Thu, 20 Aug 2009 19:47:28 -0800 [thread overview]
Message-ID: <56b13acf0908202047k2bf536f9vf993394d42059b8e@mail.gmail.com> (raw)
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
next reply other threads:[~2009-08-21 3:47 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-21 3:47 Joe [this message]
2009-08-21 6:58 ` strange stack limit behavior when allocating more than 2GB mem on 32bit machine 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56b13acf0908202047k2bf536f9vf993394d42059b8e@mail.gmail.com \
--to=longapple@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).