Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么会出现这种奇怪的内存分配行为_C_Memory Management_Ambiguous - Fatal编程技术网

C 为什么会出现这种奇怪的内存分配行为

C 为什么会出现这种奇怪的内存分配行为,c,memory-management,ambiguous,C,Memory Management,Ambiguous,这段有趣的代码总是在Linux系统中分配3 GB内存,即使物理RAM小于3 GB 怎么做?(我的系统中有一个2.7GB的RAM,此代码分配了3.054MB内存!) #包括 #包括 #包括 int main(int argc,char*argv[]) { 无效*ptr; int n=0; 而(1){ //分配1 MB块 ptr=malloc(0x100000); //当我们无法再分配时停止 如果(ptr==NULL) 打破 n++; } //我们得到了多少钱? printf(“malloced%d

这段有趣的代码总是在Linux系统中分配3 GB内存,即使物理RAM小于3 GB

怎么做?(我的系统中有一个2.7GB的RAM,此代码分配了3.054MB内存!)

#包括
#包括
#包括
int main(int argc,char*argv[])
{
无效*ptr;
int n=0;
而(1){
//分配1 MB块
ptr=malloc(0x100000);
//当我们无法再分配时停止
如果(ptr==NULL)
打破
n++;
}
//我们得到了多少钱?
printf(“malloced%d MB\n”,n);
暂停();
}

当计算机的物理RAM不足时,它们可以使用硬盘空间“充当RAM”。这要慢得多,但仍然可以做到

通常,机器可以使用几个级别访问信息:

  • 缓存(最快)
  • 公羊
  • 硬盘(最慢)

  • 它将在可用时使用最快的内存,但正如您所指出的,有时它需要使用其他资源。

    在Linux中,默认情况下,在您真正尝试修改它之前,您不会真正获得RAM。您可以尝试按如下方式修改您的程序,并查看它是否会提前终止:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
        char *ptr;
        int n = 0;
        while (1) {
            // Allocate in 4kb chunks
            ptr = malloc(0x1000);
            // Stop when we can't allocate any more
            if (ptr == NULL)
                break;
            *ptr = 1;  // modify one byte on the page
            n++;
        }
        // How much did we get?
        printf("malloced %d MB\n", n / 256);
        pause();
    }
    

    因此,任务似乎没有默认的内存大小限制。

    是否启用了交换文件/分区?@user2422531:我启用了,但我禁用了交换,仍然得到了相同的结果。请在操作系统下的网络上查找术语
    虚拟内存
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
        char *ptr;
        int n = 0;
        while (1) {
            // Allocate in 4kb chunks
            ptr = malloc(0x1000);
            // Stop when we can't allocate any more
            if (ptr == NULL)
                break;
            *ptr = 1;  // modify one byte on the page
            n++;
        }
        // How much did we get?
        printf("malloced %d MB\n", n / 256);
        pause();
    }
    
    $ ulimit -a
    core file size          (blocks, -c) 0
    data seg size           (kbytes, -d) unlimited
    scheduling priority             (-e) 20
    file size               (blocks, -f) unlimited
    pending signals                 (-i) 16382
    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) unlimited
    virtual memory          (kbytes, -v) unlimited
    file locks                      (-x) unlimited