C ++内存分配 [英] C++ memory allocation

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

问题描述

大家好,


AFAIU,''new''返回未初始化的内存。这是正确的吗?


我怀疑这是因为一个实现(我使用的那个)

坚持要求返回零内存。


#include< cstdlib>

#define PACKET_SIZE 1000

#define PACKET_NUM 30

typedef char Packet [PACKET_SIZE];

int main()

{

/ *分配30个1000字节数据包的空间* /

Packet * arr = new Packet [PACKET_NUM];


for(int i = 0; i< PACKET_NUM; ++ i)

for (int j = 0; j< PACKET_SIZE; ++ j)

if(arr [i] [j]!= 0)abort();


返回0;

}


最小程序不会在我的平台上中止。


这是只是我平台的一个功能? (我想是的。)


问候。

Hello everyone,

AFAIU, ''new'' returns un-initialized memory. Is this correct?

The reason I doubt this is because one implementation (the one I use)
insists on returning zero-ed memory.

#include <cstdlib>
#define PACKET_SIZE 1000
#define PACKET_NUM 30
typedef char Packet[PACKET_SIZE];
int main()
{
/* Allocate space for 30 1000-byte packets */
Packet *arr = new Packet[PACKET_NUM];

for (int i=0; i < PACKET_NUM; ++i)
for (int j=0; j < PACKET_SIZE; ++j)
if ( arr[i][j] != 0 ) abort();

return 0;
}

The minimal program does not abort on my platform.

Is this just a feature of my platform? (I suppose so.)

Regards.

推荐答案

Spoon写道:
Spoon wrote:
AFAIU,''new''返回未初始化的内存。它是否正确?


是的。基本规则是你不为你不使用的东西买单。因为

你应该用构造函数新的东西,或者应该用''new''原始的

数据变量然后写下来,''new''本身并没有浪费时间

填补它的记忆。

我怀疑这是因为一个实现(我使用的那个)


下次(但不是这次)承认实施的品种。 (这是

/姓名/不在这里偏离主题!)

包* arr =新包[PACKET_NUM];

for(int i = 0; i< PACKET_NUM; ++ i)
for(int j = 0; j< PACKET_SIZE; ++ j)
if(arr [i] [j]! = 0)abort();
AFAIU, ''new'' returns un-initialized memory. Is this correct?
Yup. The underlying rule is "you don''t pay for what you don''t use". Because
you should either ''new'' things with constructors, or should ''new'' primitive
data variables that you then write on, ''new'' itself doesn''t waste time
filling its memory.
The reason I doubt this is because one implementation (the one I use)
Next time (but not this time) confess the breed of implementation. (It''s
/name/ is not off-topic here!)
Packet *arr = new Packet[PACKET_NUM];

for (int i=0; i < PACKET_NUM; ++i)
for (int j=0; j < PACKET_SIZE; ++j)
if ( arr[i][j] != 0 ) abort();




此程序生成未定义的行为,因为Packet不是''unsigned

char''。这是唯一保证不会因所有潜在位模式而抛出硬件

异常的数据变量。其他变量,包括

''char'',如果它们包含垃圾,如果垃圾不是你的CPU的有效表示,可能会给你的CPU造成错误。那个变量。


你不用付你不用的东西,包括支付额外的操作码

防止虚假情况下的CPU故障。


"未定义的行为"意味着任何事情都可能发生,包括程序

似乎正常工作;包括最近的厕所爆炸。


在你的具体情况下,''new''从堆中拉出来,这本身就是零填充的b $ b。可能是你的实现,也可能是你的操作系统。


尝试将数字写入你的数组,然后删除它,然后''new''ing它

再次。这个实验也将是未定义的行为(意思是不要在生产中使用它/ b $ b),但你可能会发现你的数字仍在那里。


- -

Phlip
http:// c2。 com / cgi / wiki?ZeekLand < - 不是博客!!!



This program generates undefined behavior because Packet is not an ''unsigned
char''. That''s the only data variable guaranteed to not throw a hardware
exception for all of its potential bit patterns. Other variables, including
''char'', if they contain garbage, could fault your CPU if the garbage is not
one of your CPU''s valid representations for that variable.

You don''t pay for what you don''t use, including paying for extra opcodes to
prevent CPU faults over bogus situations.

"Undefined behavior" means anything could happen, including the program
appears to work correctly; including the nearest toilet explodes.

In your specific case, ''new'' is pulling from a heap which itself has been
zero-filled. Possibly by your implementation, and possibly by your OS.

Try writing numbers into your array, then deleting it, then ''new''ing it
again. This experiment will also be undefined behavior (meaning don''t use it
in production), but you might find your numbers are still there.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


2006年4月27日星期四17:36:44 +0200, Spoon< ro ** @ 127.0.0.1>写道:
On Thu, 27 Apr 2006 17:36:44 +0200, Spoon <ro**@127.0.0.1> wrote:
大家好,

AFAIU,''new''返回未初始化的内存。它是否正确?


对于POD类型,答案是肯定的。对于非POD类型,它取决于

新类型的默认构造函数是什么(至少

for array new ...你也可以传递构造函数参数for new ing

单个对象)。这是由C +标准,第5.3.4节,

项目15定义的。

我怀疑这是因为一个实现(我使用的那个)
坚持返回零记忆。

#include< cstdlib>
#define PACKET_SIZE 1000
#define PACKET_NUM 30
typedef char Packet [PACKET_SIZE];


不应该这样:

typedef char [PACKET_SIZE]包;

??

int main()
{* / *为30个1000字节数据包分配空间* /
数据包* arr =新数据包[PACKET_NUM];

(int i = 0; i< PACKET_NUM; ++ i)
for(int j = 0; j< PACKET_SIZE; ++ j)
if(arr [i] [j]!= 0)abort();

返回0;
}

最小程序不会在我的平台上中止。

这是只是我平台的一个功能? (我想是的。)
Hello everyone,

AFAIU, ''new'' returns un-initialized memory. Is this correct?
For POD types, the answer is yes. For non-POD types, it depends on
what the default constructor of the type being new''d does (at least
for array new ... you can also pass constructor arguments for new-ing
single objects). This is defined by the C+ standard, section 5.3.4,
item 15.
The reason I doubt this is because one implementation (the one I use)
insists on returning zero-ed memory.

#include <cstdlib>
#define PACKET_SIZE 1000
#define PACKET_NUM 30
typedef char Packet[PACKET_SIZE];
Shouldn''t this be:
typedef char[PACKET_SIZE] Packet;
??
int main()
{
/* Allocate space for 30 1000-byte packets */
Packet *arr = new Packet[PACKET_NUM];

for (int i=0; i < PACKET_NUM; ++i)
for (int j=0; j < PACKET_SIZE; ++j)
if ( arr[i][j] != 0 ) abort();

return 0;
}

The minimal program does not abort on my platform.

Is this just a feature of my platform? (I suppose so.)




可能。


-

Bob Hairgrove
No**********@Home.com


Bob Hairgrove写道:
Bob Hairgrove wrote:
On Thu,2006年4月27日17:36:44 +0200,Spoon写道:
On Thu, 27 Apr 2006 17:36:44 +0200, Spoon wrote:
#include< cstdlib>
#define PACKET_SIZE 1000
#define PACKET_NUM 30
typedef char数据包[PACKET_SIZE];
#include <cstdlib>
#define PACKET_SIZE 1000
#define PACKET_NUM 30
typedef char Packet[PACKET_SIZE];



不应该这样:
typedef char [PACKET_SIZE]数据包;
??



Shouldn''t this be:
typedef char[PACKET_SIZE] Packet;
??




绝对不是:-)



Definitely not :-)


这篇关于C ++内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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