用C堆大小限制 [英] Heap size limitation in C

查看:301
本文介绍了用C堆大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在C程序的程序执行布局图堆一个疑问。

I have a doubt regarding heap in program execution layout diagram of a C program.

我知道,所有的动态分配的内存在堆中动态地分配增长。但我想知道什么是C程序??

I know that all the dynamically allocated memory is allotted in heap which grows dynamically. But I would like to know what is the max heap size for a C program ??

我只是附加一个C程序示例...我在这里试图分配1GB内存串,甚至做的memset ...

I am just attaching a sample C program ... here I am trying to allocate 1GB memory to string and even doing the memset ...

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

    int main(int argc, char *argv[])
    {
       char *temp;
       mybuffer=malloc(1024*1024*1024*1);

       temp = memset(mybuffer,0,(1024*1024*1024*1));

       if( (mybuffer == temp) && (mybuffer != NULL))
       printf("%x - %x\n", mybuffer, &mybuffer[((1024*1024*1024*1)-1)]]);
       else
       printf("Wrong\n");

       sleep(20);
       free(mybuffer);
       return 0;
    }

如果我上面的程序在3个实例一次那么的malloc应该一个实例ATLEAST故障运行[我觉得好] ...但仍是malloc的全成。

If I run above program in 3 instances at once then malloc should fail atleast in one instance [I feel so] ... but still malloc is successfull.

如果它是成功的,我可以知道如何OS需要动态分配的内存3GB的照顾。

If it is successful can I know how the OS takes care of 3GB of dynamically allocated memory.

推荐答案

您的机器是很可能的 overcomitting 的RAM,而不是使用内存,直到你真正写。尝试分配后写入每个块,从而迫使操作系统,以确保有映射到的malloc()返回的地址是否真实RAM。

Your machine is very probably overcomitting on RAM, and not using the memory until you actually write it. Try writing to each block after allocating it, thus forcing the operating system to ensure there's real RAM mapped to the address malloc() returned.

这篇关于用C堆大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆