PBYTE有16个字节的垃圾值。 [英] PBYTE has 16 Bytes of Junk values.

查看:103
本文介绍了PBYTE有16个字节的垃圾值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PBYTE m_pSectorBitmap = new BYTE[512];
memset(m_pSectorBitmap, 0xFF, 512);



对于上面的代码片段,我是预计将获得一个具有FF高达511个位置的BYTE阵列,但由于某些原因,我得到另一个16字节,即我得到511 BYTES的FF和另外16个字节的JUNK值。有人可以解释它是如此。我应该怎么做才能获得511字节的FF。



我尝试过相同的128字节,仍然遇到同样的问题,它是如此通用,或者是这根本不是什么问题吗?



谢谢,

Sajith


For the above code snippet, i am expected to get a BYTE array with FF upto 511 positions, But for some reasons i am getting another 16 Byte that is I am getting 511 BYTES of FF and another 16 Bytes of JUNK values. Can someone explain y it is so. What should i do to get exactly 511 Bytes of FF.

I have tried the same with 128 Bytes, still getting the same problem, Is it so generic, or is it not at all a problem ?

Thanks,
Sajith

推荐答案

指针的工作方式是指向一个内存位置。分配内存(以有效的非NULL指针返回)时,指针将设置为新分配的内存中的第一个字节。如果分配成功,您将使用您请求的确切字节数,而不是更多。查看超出您请求的数据称为未定义行为,并且可能导致您无法控制的多个行为(可能是填充,实际上也是分配的,它可能是您的分配开销,无效内存,另一个分配,或其他)。



您提供的示例中指向的内存仅对前512个字节有效。如果你超越它,你就违反了规则。内存分配系统使用了似乎是垃圾的东西,你无权访问它。
The way that pointers work is that they point to one memory location. When you allocate memory (returned in a valid, non-NULL pointer), the pointer is set to the first byte in the newly allocated memory. If the allocation is successful, you get to use the exact number of bytes that you requested, and no more. Looking at the data beyond what you requested is called "undefined" behavior, and can result in several behaviors that you can not control (maybe it is "padding" that is really also allocated, it might be overhead for your allocation, invalid memory, another allocation, or something else).

The memory you point to in the example you provide is only valid for the first 512 bytes. If you look beyond that you are breaking the rules. What seems to be junk to you is used by the memory allocation system, and you are not entitled to access it.


这篇关于PBYTE有16个字节的垃圾值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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