gpt4 book ai didi

c - 为什么在我有 16 GB RAM 的计算机上只能分配 2 GB?

转载 作者:IT王子 更新时间:2023-10-29 01:16:43 29 4
gpt4 key购买 nike

<分区>

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

我正在我的电脑上运行以下程序:

#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$

这是怎么回事?

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com