在c ++中可以动态分配和在编译时分配的最大内存 [英] Maximum memory that can be allocated dynamically and at compile time in c++

查看:134
本文介绍了在c ++中可以动态分配和在编译时分配的最大内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩,以了解可以分配多少内存。最初我认为可分配的最大内存等于物理内存(RAM)。我在Ubuntu 12.04上检查我的RAM,通过运行命令如下所示:

I am playing around to understand how much memory can be allocated. Initially I thought that the maximum memory which can be allocated is equal to Physical memory (RAM). I checked my RAM on Ubuntu 12.04 by running the command as shown below:

~$ free -b
             total       used       free     shared    buffers     cached
Mem:    3170848768 2526740480  644108288          0  265547776 1360060416
-/+ buffers/cache:  901132288 2269716480
Swap:   2428497920          0 2428497920

如上所示,总物理内存是3Gig(3170848768字节),其中只有644108288字节是空闲的,所以我假设我可以在最大分配这么多记忆。我通过编写下面只有两行的小程序来测试它:

As shown above,total physical memory is 3Gig (3170848768 bytes) out of which only 644108288 bytes is free, so I assumed I can at max allocate only this much memory. I tested it by writing the small program with only two lines below:

char * p1 = new char[644108290] ;
delete p1;

由于代码运行完美,这意味着它成功分配内存。也试图分配内存大于可用的物理可用内存仍然没有抛出任何错误。然后按照问题

Since code ran perfectly , it means it allocated memory successfully. Also I tried to allocate the memory greater than the available physical free memory still it did not throw any error. Then per question

最大内存其中malloc可以分配!

我认为它必须使用虚拟内存。我测试了代码的免费交换内存,它也工作。 p>

I thought it must be using the virtual memory.So I tested the code for free swap memory and it also worked.

char * p1 = new char[2428497920] ;
delete p1;

我试图分配空闲swap加上可用的RAM内存字节

The I tried to allocate the free swap plus free RAM bytes of memory

char * p1 = new char[3072606208] ;
delete p1;

但是这次代码失败了抛出 bad_alloc

But this time code failed throwing the bad_alloc exception.Why the code didn't work this time.

现在我在编译时在新程序中分配内存,如下所示:

Now I allocated the memory at compile time in a new program as shown below:

char p[3072606208] ;
char p2[4072606208] ;
char p3[5072606208];
cout<<"Size of array p = " <<sizeof p <<endl;
cout<<"Size of array p2 = " <<sizeof p2<<endl;
cout<<"Size of array p2 = " <<sizeof p3;

结果显示

Size of array p = 3072606208
Size of array p1 = 4072606208
Size of array p2 = 777638912

请帮助我理解这里发生了什么。为什么它允许内存在编译时分配,而不是动态分配。
当分配的编译时间如何 p p1 能够分配大于swap的内存加上可用内存记忆。其中 p2 失败。
这是怎么工作的。这是一些未定义的行为或os特定的行为。谢谢你的帮助。我使用Ubuntu 12.04和gcc 4.6.3。

Could you please help me understand what is happening here. Why did it allowed the memory to be allocated at the compile time but not at dynamically. When allocated compile time how come p and p1 were able to allocate memory greater than swap plus free RAM memory. Where as p2 failed. How exactly is this working. Is this some undefined behaviour or os specific behaviour. Thanks for your help. I am using Ubuntu 12.04 and gcc 4.6.3.

推荐答案

内存页面实际上并没有映射到你的程序, 。所有 malloc 都保留了虚拟地址空间的范围。

Memory pages aren't actually mapped to your program until you use them. All malloc does is reserve a range of the virtual address space. No physical RAM is mapped to those virtual pages until you try to read or write them.

即使分配全局或堆栈(自动)内存,也没有映射

Even when you allocate global or stack ("automatic") memory, there's no mapping of physical pages until you touch them.

最后,在编译时评估 sizeof()不知道操作系统以后会做什么。

Finally, sizeof() is evaluated at compile time, when the compiler has no idea what the OS will do later. So it will just tell you the expected size of the object.

如果你尝试 memset <>,你会发现事情会有很大的不同。 / code>内存为0在每个情况下。此外,您可能需要尝试 calloc ,这会将其内存设置为零。

You'll find that things will behave very differently if you try to memset the memory to 0 in each of your cases. Also, you might want to try calloc, which zeroes its memory.

这篇关于在c ++中可以动态分配和在编译时分配的最大内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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