首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C++:为什么这段代码会给我内存问题/未定义的行为?

C++:为什么这段代码会给我内存问题/未定义的行为?
EN

Stack Overflow用户
提问于 2019-06-21 03:21:34
回答 2查看 126关注 0票数 1

如果您感兴趣,请介绍一些背景知识...

下一段代码是使用循环冗余校验(CRC-15)实现分组错误码生成器的尝试。这用于检测通信数据损坏。更详细的介绍是不必要的。

代码和问题

init_PEC15_Table函数是一个查找表生成器。

pec15函数接受数据输入,计算解决方案的地址,并在查找表中查找结果。

data是一个字符数组,我给它赋值为1。这将被传递给pec15。

现在,我发现,通过对cout命令重新排序,“ pec”的值(我感兴趣的输出)会更改。通过在线阅读,我了解到这可能是由于内存堆栈以一种影响结果寄存器的方式意外更改,也可能是由于对其他变量的越界操作。我的理解错了吗?

现在,我是一个初学者,这是非常令人畏惧的。我可能犯了一些我不知道的严重错误,所以请随时将代码撕成碎片。另外,如果重要的话,这段代码运行在一个mbed LPC1768上。

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-21 03:28:02

您在此处编写的代码与您编写的注释不同步:

代码语言:javascript
复制
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的数组的指针,但这可能不是您想要做的。

上面的两行代码本身并不会做任何坏事。然而,下一行是一个真正的问题:

代码语言:javascript
复制
unsigned short result = pec15(dataPtr);                    //Pass data pointer to pec calculator

请记住,pec15还有第二个参数,用于表示传入的数据的长度。但是,您的dataPtr指针只指向单个char值,而不是16个char值,因此这会导致未定义的行为。

我不确定如何修复这个问题,因为我对你的代码背后的意图没有很好的感觉。你的意思是做一个16元素的数组吗?您的意思是创建一个值为0x1的数组吗?这里的正确解决方法取决于这个问题的答案。

票数 6
EN

Stack Overflow用户

发布于 2019-06-21 03:25:55

尝试:

代码语言:javascript
复制
unsigned short result = pec15(dataPtr, 1);

否则,lengt为16 (具有默认值)。我还建议删除lengt的默认值,因为它在pec15函数的上下文中意义不大。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56692601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档