动态内存分配问题 [英] Dynamic memory allocation question

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

问题描述

当您使用指针在堆上分配动态内存时,

when you allocate dynamic memory on the heap using a pointer,

char *buffer_heap = new char[15];

它将在内存中表示为:

 ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þþþ

为什么在末尾没有NULL结束符,而不是ýýýý«««««««þþþ?

why doesn't there be a NULL terminating character at the end instead of ýýýý««««««««þþþ?

推荐答案

Í是字节0xCD,Windows调试分配器写入到15个字节的内存中,以指示它是未初始化的堆内存。未初始化的堆栈将为0xCC。这个想法是,如果你曾经读过内存并意外地得到这个值,你可以自己想,嗯,我可能忘了初始化这个。此外,如果你读它作为指针和解引用,那么Windows会崩溃你的进程,而如果一个未初始化的缓冲区填充随机或任意值,有时通过fluke你会得到一个有效的指针,你的代码可能会导致所有种类麻烦。 C ++不说什么值未初始化的内存保持,非调试分配器不会浪费时间填充内存与每个分配的特殊值,所以你不能依赖那里的值。

Í is byte 0xCD, which the Windows debug allocator writes into your 15 bytes of memory to indicate that it is uninitialised heap memory. Uninitialized stack would be 0xCC. The idea is that if you ever read memory and unexpectedly get this value, you can think to yourself, "hmm, I've probably forgotten to initialise this". Also, if you read it as a pointer and dereference it, then Windows will crash your process, whereas if an uninitialised buffer were filled with random or arbitrary values then sometimes by fluke you'd get a valid pointer, and your code might cause all kinds of trouble. C++ doesn't say what values uninitialized memory holds, and non-debug allocators won't waste time filling memory with special values for every allocation, so you must never rely on that value being there.

其后是4个字节的ý(字节0xFD),Windows调试分配器用来指示缓冲区末尾的超出范围区域。这个想法是,如果你发现自己在调试器写一个像这样的区域,你可以认为嗯,我可能超支我的缓冲区在这里。此外,如果缓冲区释放时值改变,内存分配器可以警告你的代码是错误的。

This is followed by 4 bytes of ý (byte 0xFD), which the Windows debug allocator uses to indicate an out-of-bounds region at the end of a buffer. The idea is that if you ever find yourself in the debugger writing to a region that looks like this, you can think "hmm, I've probably overrun my buffer here". Also, if the value has changed when the buffer is freed, the memory allocator can warn you that your code is wrong.

«是字节0xAB,而þ是0xFE。据推测,这些也被用作捕捉器(它们不是合理的指针或偏移,因此它们不构成堆结构的一部分)。我不知道他们的意思,可能更多的保护数据像0xFD。

« is byte 0xAB, and þ is 0xFE. Presumably these are also intended as eye-catchers (they aren't plausible pointers or offsets, so they don't form part of the heap structure). I don't know what they signify, possibly more guard data like the 0xFD.

最后,我想,你发现一个0字节,第16个字节超出你的15字节缓冲区的结束(即从它的开始计数的第31个字节)。

Finally, I guess, you've found a 0 byte, the 16th byte beyond the end of your 15 byte buffer (i.e. the 31st byte counting from the start of it).

将问题作为C ++而不提到您在Windows上表明这是C ++的行为。它不是,它是一个C ++的实现行为,与特定的编译器选项和/或链接的dll。 C ++不允许你读缓冲区的结尾,微软只是对你很好,让你摆脱它不崩溃或更糟。

Asking the question as "C++" without mentioning that you're on Windows suggests that this is how C++ behaves. It isn't, it's how one implementation of C++ behaves, with particular compiler options and/or linked dlls. C++ does not permit you to read past the end of the buffer, Microsoft is just being nice to you and letting you get away with it not crashing or worse.

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

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