从 'BYTE *' 到 'ULONG' 的指针截断
Pointer truncation from 'BYTE *' to 'ULONG'
我试图为Windows编写一个简单的过滤器驱动程序,当我想构建项目时,Visual Studio会给我以下警告:
警告C4311'类型Cast':从"字节 *"到'ulong''
的指针截断和错误: C2220警告被视为错误 - 无"对象"文件生成
这是我的代码:
BOOLEAN GetAllBufferFromChunkedFormat(STREAM_EDIT_PARAMETERS* params, BYTE**
dataBuffer)
{
if (!CheckPointer(params) || !CheckPointer(dataBuffer))
{
return FALSE;
}
BYTE* iterator = params->dataStart + params->contentStart;
params->currentContentLength = 0;
UINT currentChunkLength = 0;
BOOLEAN isAllData = FALSE;
while ((ULONG)iterator - (ULONG)params->dataStart < params->streamEditor->dataLength) //calculate total length
{
currentChunkLength = strtol(iterator, &iterator, 16);
iterator += s_chunksSeparatorLength + currentChunkLength + s_chunksSeparatorLength;
..
..
..
}
警告在线显示:
while ((ULONG)iterator - (ULONG)params->dataStart < params->streamEditor->dataLength)
有什么问题?为什么?
a long 类型是4个字节宽,而诸如 byte*之类的指针类型具有体系结构的宽度,即。x86平台上的32位或4个字节,x64体系结构上的64位(8byte)。因此,这将在X86平台上予以罚款,但在64位平台上发出警告(被视为错误)。
对于X64架构,正确的转换将是数值类型" 长"或 uint64 和类似类型,或者如果您真的不在乎,您可以 static_cast(x)值。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- std::byte 指针应该用于指针算术吗?
- 从 'BYTE *' 到 'ULONG' 的指针截断
- 将YUV的单个BYTE*拆分成每个平面的3个BYTE*指针