4

Possible Duplicate:
Can you allocate a very large single chunk of memory ( > 4GB ) in c or c++?

I am running the following program on my computer:

#include <stdio.h>
#include <stdlib.h>

#define ONE_GIGABYTE 1024*1024*1024


int main(void) {
    int ctr=0;

    for (;;) {
        char *ptr = (char*)malloc(ONE_GIGABYTE*sizeof(char));
        if (ptr == 0)
            return -1;

        ctr++;
        printf("%d\n", ctr);
    }
}

flyrev@stargazer:~/weirdstuff$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 128957
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 128957
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
flyrev@stargazer:~/weirdstuff$ free -g
             total       used       free     shared    buffers     cached
Mem:            15          6          8          0          0          4
-/+ buffers/cache:          2         13
Swap:            9          0          9
flyrev@stargazer:~/weirdstuff$ clang malloc-program.c 
flyrev@stargazer:~/weirdstuff$ ./a.out 
1
2
flyrev@stargazer:~/weirdstuff$ 

What is going on here?

Community
  • 1
  • 1
Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93

2 Answers2

4

You're not running out of memory, you're running on a 32 bit system and so are running out of address space, you might reasonably expect to be able to allocate 4Gb on a 32 system since:

2^32 = 4Gb

However, on most operating systems, at least 50% of the available address space is actually reserved for use by the kernel so you can only have half of it.

On Linux, it is possible to use significantly more than 4Gb in 32 bit mode by switching to use a PAE kernel. Many Linux distributions provide a PAE kernel as a package if you need one.

EDIT: As Dietrich notes: PAE allows more memory to be used, but still only gives you 4 GiB of address space. So with 16 GiB you can have 8 programs with 2 GiB each, but you still can't have one program with more than 2-ish GiB

Benj
  • 31,668
  • 17
  • 78
  • 127
  • 3
    This should be clarified: PAE allows more memory to be used, but still only gives you 4 GiB of address space. So with 16 GiB you can have 8 programs with 2 GiB each, but you still can't have one program with more than 2-ish GiB. (You can configure the high memory cutoff, too, when you compile the kernel.) – Dietrich Epp Oct 20 '12 at 15:16
  • @DietrichEpp Good point, edited. – Benj Oct 20 '12 at 15:20
1

@Benj provided a good answer to this question. However, I would like to add that, even if one is using a 64-bit system, he may still be able to use only 2GB of memory if the program is compiled with a compiler that targets 32-bit system.

64-bit system usually supports 32-bit programs. But 32-bit system can't run 64-bit ones. Therefore, programs that didn't expect to take up 2GB memory may be compiled to target 32-bit system if the author chose to. Also, 32-bit target is usually the default, even on 64-bit systems.

Haozhun
  • 6,331
  • 3
  • 29
  • 50