C++:为什么这段代码给我内存问题/未定义的行为?
C++: Why is this code giving me memory issues / undefined behavior?
如果你有兴趣的话,可以介绍一下背景...
下一段代码是尝试使用循环冗余校验 (CRC-15) 实现数据包错误代码生成器。这用于检测通信数据损坏。不需要更详细的介绍。
代码和问题
init_PEC15_Table函数是一个查找表生成器。
PEC15函数接受数据输入,计算解决方案的地址并在查找表中查找结果。
data 是一个字符数组,我为其分配了值 1。这将传递给 pec15。
现在,我发现通过对 cout 命令重新排序,我感兴趣的输出"填充 pec"的值会发生变化。通过在线阅读,我了解到这可能是由于内存堆栈意外更改,从而影响结果寄存器,这可能是由于对其他变量的越界操作。我的理解有误吗?
现在,我是一个初学者,这是非常令人生畏的。我可能犯了一些我不知道的严重错误,所以请随时将代码撕成碎片。 此外,如果重要,此代码在 mbed LPC1768上运行。
#include <iostream>
using namespace std;
unsigned short pec15Table[256];
const unsigned int CRC15_POLY = 0x4599;
void init_PEC15_Table() // Cyclical Redundancy Check lookup table generator function
{
unsigned short rem;
for (int i = 0; i < 256; i++)
{
rem = i << 7;
for (int bit = 8; bit > 0; --bit)
{
if (rem & 0x4000)
{
rem = ((rem << 1));
rem = (rem ^ CRC15_POLY);
}
else
{
rem = ((rem << 1));
}
}
pec15Table[i] = rem & 0xFFFF;
// cout << hex << pec15Table [i] << endl;
}
}
unsigned short pec15(char* data, int lengt = 16) //Takes data as an input,
{
int rem, address;
rem = 16;//PEC seed (intial PEC value)
for (int i = 0; i < lengt; i++)
{
address = ((rem >> 7) ^ data[i]) & 0xff;//calculate PEC table address
rem = (rem << 8) ^ pec15Table[address];
}
return (rem * 2);//The CRC15 has a 0 in the LSB so the final value must be multiplied by 2
}
int main()
{
init_PEC15_Table(); //initialise pec table
char data = (short) 0x1 ; // Write 0x1 to char array containing the data 0x1
char* dataPtr = &data; // Create a pointer to that array
unsigned short result = pec15(dataPtr); //Pass data pointer to pec calculator
cout << "data in: " << (short) *dataPtr << endl; //Print the short representation of the char data array (Outputs 1)
cout << "size of data: " << sizeof(*dataPtr) << endl; //Print the size of the char array (Outputs 1)
cout << "stuffed pec: " << result << endl; //Print the output of the pec calculation
return 0;
}
您在此处编写的代码与您编写的注释不同步:
char data = (short) 0x1 ; // Write 0x1 to char array containing the data 0x1
char* dataPtr = &data; // Create a pointer to that array
第一行不会向字符数组写入任何内容。相反,它会创建一个数值为 1 的char
变量。请注意,这里不需要short
演员阵容,也没有效果——你的意思是写别的东西吗?
第二行不创建指向数组的指针。相反,它会创建一个指向data
变量的指针。您可能会将其视为指向长度为 1 的数组的指针,但这可能不是您的本意。
上面的两行本身并没有做任何坏事。然而,下一行是一个真正的问题:
unsigned short result = pec15(dataPtr); //Pass data pointer to pec calculator
请记住,pec15
有第二个参数,应该表示传入数据的长度。由于您未指定它,因此它默认为 16。但是,dataPtr
指针仅指向单个char
值,而不是 16 个char
值,因此这会导致未定义的行为。
我不确定如何解决这个问题,因为我对您的代码背后的意图没有很好的了解。你的意思是制作一个十六元素的数组吗?你的意思是创建一个填充值0x1
的数组吗?此处的正确修复取决于该问题的答案。
尝试:
unsigned short result = pec15(dataPtr, 1);
否则lengt
为 16(具有默认值)。我还建议删除默认值lengt
,因为它在函数pec15
上下文中几乎没有意义。
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 包含矢量指针的结构的内存释放问题
- 在函数中分配内存时出现问题
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 使用 NTAllocateVirtualMemory 和 GetProcAddress 的内存分配问题不起作用
- 释放内存(主题模板)时出现问题
- C++ 中 std::vector 的内存问题
- C++ DLL 堆内存分配问题
- 在我的以下代码中获取 MLE(内存限制错误).尝试解决 ROUND C 2019(问题 A-摆动行走)启动问题
- 字符 * 未从重载运算符或内存管理问题正确返回
- delete[]有问题,如何部分删除内存
- 内存泄漏问题
- 指向动态内存中结构中的变量时出现问题
- c++中的内存管理问题
- C++堆栈内存管理问题
- 为什么我的C 代码在Linux上运行,而在Windows上不运行,是否有RAM内存的问题
- JNA 参数问题:内存访问无效
- boost的问题::内存映射~500MB,外部USB文件