Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试从字节数组读取浮点值时出现ESP8266异常_C++_Arrays_Pointers_Exception_Esp8266 - Fatal编程技术网

C++ 尝试从字节数组读取浮点值时出现ESP8266异常

C++ 尝试从字节数组读取浮点值时出现ESP8266异常,c++,arrays,pointers,exception,esp8266,C++,Arrays,Pointers,Exception,Esp8266,我的ESP8266(Wemos D1 mini)有一个奇怪的问题。 我正在编写一个用nRF24L01发射机构建网络的库 我定义了一个gloabl uint8_t arry,它代表我的接收缓冲区。 当接收到来自其他设备的传输时,阵列将被填满。填充数组后,我开始解析缓冲区,以确定是否存在有效的头/帧。标题包含一些典型的数据,如发件人、校验和。。。如果报头有效,将解析帧的数据。 数据的结构总是相同的。可以有x个所谓的数据三元组。数据三元组是键(cstring)、值类型(uint8_t)和值本身的组合。

我的ESP8266(Wemos D1 mini)有一个奇怪的问题。 我正在编写一个用nRF24L01发射机构建网络的库

我定义了一个gloabl uint8_t arry,它代表我的接收缓冲区。 当接收到来自其他设备的传输时,阵列将被填满。填充数组后,我开始解析缓冲区,以确定是否存在有效的头/帧。标题包含一些典型的数据,如发件人、校验和。。。如果报头有效,将解析帧的数据。 数据的结构总是相同的。可以有x个所谓的数据三元组。数据三元组是键(cstring)、值类型(uint8_t)和值本身的组合。值的类型可以是基本类型,如INT8、UINT8或FLOAT或STRING

现在是stange事件。

为了节省内存,我不会将缓冲区中的值复制到专用变量中。我总是指向缓冲区中的位置。当我想读取一个值时,我做一个简单的指针转换并读取值。 如果值是一个字符串或一个简单的1字节长的uint8\u t,那么这是非常有效的。但如果是浮点或uint32,我会得到一个LoadStoreAlignmentCause exception,它告诉我,我想访问非关联内存。 这怎么可能?指针明确指向正确的缓冲区位置。我检查了这个,因为如果我创建一个新的float变量并从缓冲区到变量地址执行memcpy,那么一切都很好。但是我创建了一个浮点指针并指向缓冲区中的位置,我得到了一个异常

    float* fTmp = (float*)iCurrentPos; // does not copy; takes a pointer to the orignal location

    float f;
    memcpy(&f, fTmp, sizeof(float)); // copies data to aligned variable

    Serial.println(f); // prints value from aligned variable

    Serial.println(*fTmp); // prints value for original, non-aligned location
谁能告诉我原因或我做错了什么

下面是一些代码片段,以便更好地理解

bool SBSmartHomeBasicDevice::parseDataTriples() {
if (_IncommingTransmission) {
    uint8_t* iCurrentPos;
    uint8_t iDataTripleCount = 0;
    // _LastReceivedFrame.Payload points to the receive buffer
    for (iCurrentPos = _LastReceivedFrame.Payload; iCurrentPos < (_LastReceivedFrame.Payload + _LastReceivedFrame.Header.PayloadSize); iCurrentPos) {
        // Catch the type of the triple
        uint8_t type = *((uint8_t*)iCurrentPos);
        // increase the pointer about uint8_t size
        iCurrentPos += sizeof(uint8_t);
        // Catch the key of the triple
        char* key;
        key = (char*)(iCurrentPos);
        // increase the pointer about strlen of key + 1 
        iCurrentPos += strlen((char*)iCurrentPos) + 1;
        // catch the value
        void* value = (void*)(iCurrentPos);
        _LastReceivedTriples[iDataTripleCount].setType(type);
        _LastReceivedTriples[iDataTripleCount].setKey(key);


        // ***
        // Here starts the interesting part 
        // ***
        float* fTmp = (float*)iCurrentPos;
        // The following works perfect
        // ****
        float f;
        memcpy(&f, fTmp, sizeof(float));
        Serial.println(f);
        // ****
        // The following causes an exception
        Serial.println(*fTmp);
        // *** EXCEPTION ***


        _LastReceivedTriples[iDataTripleCount].setValue(iCurrentPos);
        // now increase the pointer
        switch (type) {
        case SMART_HOME_VALUE_TYPE_STRING:
            iCurrentPos += strlen((char*)iCurrentPos) + 1;
            break;
        case SMART_HOME_VALUE_TYPE_INT8:
        case SMART_HOME_VALUE_TYPE_UINT8:
            iCurrentPos += sizeof(int8_t);
            break;
        case SMART_HOME_VALUE_TYPE_INT16:
        case SMART_HOME_VALUE_TYPE_UINT16:
            iCurrentPos += sizeof(int16_t);
            break;
        case SMART_HOME_VALUE_TYPE_FLOAT:
            iCurrentPos += sizeof(float);
            break;
        case SMART_HOME_VALUE_TYPE_INT32:
        case SMART_HOME_VALUE_TYPE_UINT32:
            iCurrentPos += sizeof(int32_t);
            break;
        default:
            Serial.println("parseDataTriples(): Unknown ValueType");
        }
        _LastReceivedTriples[iDataTripleCount].print();
        iDataTripleCount++;
        _LastReceivedTriplesCount = iDataTripleCount;
    } 
    return true;
}
return false;
bool SBSmartHomeBasicDevice::parseDataTriples(){
如果(_输入传输){
uint8_t*iCurrentPos;
uint8_t iDataTripleCount=0;
//\u LastReceivedFrame.Payload指向接收缓冲区
对于(iCurrentPos=\u LastReceivedFrame.Payload;iCurrentPos<(\u LastReceivedFrame.Payload+\u LastReceivedFrame.Header.PayloadSize);iCurrentPos){
//接住三重奏的类型
uint8_t type=*((uint8_t*)iCurrentPos);
//将指针增大到uint8\t大小
iCurrentPos+=sizeof(uint8_t);
//抓住三重奏的关键
字符*键;
键=(字符*)(iCurrentPos);
//增加关于键+1的strlen的指针
iCurrentPos+=strlen((char*)iCurrentPos)+1;
//抓住价值
无效*值=(无效*)(iCurrentPos);
_LastReceivedTriples[iDataTripleCount].setType(类型);
_LastReceivedTriples[iDataTripleCount].setKey(key);
// ***
//这里开始有趣的部分
// ***
浮动*fTmp=(浮动*)iCurrentPos;
//下面的工作非常完美
// ****
浮动f;
memcpy(&f、fTmp、sizeof(浮动));
序列号println(f);
// ****
//以下情况会导致异常
Serial.println(*fTmp);
//***例外情况***
_LastReceivedTriples[iDataTripleCount].setValue(iCurrentPos);
//现在增加指针
开关(类型){
案例智能\u主值\u类型\u字符串:
iCurrentPos+=strlen((char*)iCurrentPos)+1;
打破
案例智能家庭价值类型INT8:
案例智能家庭价值类型单元8:
iCurrentPos+=sizeof(int8_t);
打破
案例智能\家庭\价值\类型\ INT16:
案例智能家庭价值类型UINT16:
iCurrentPos+=sizeof(int16_t);
打破
案例智能\家庭\价值\类型\浮动:
iCurrentPos+=sizeof(浮动);
打破
案例智能\家庭\价值\类型\ INT32:
案例智能家庭价值类型UINT32:
iCurrentPos+=sizeof(int32_t);
打破
违约:
println(“parseDataTriples():未知值类型”);
}
_LastReceivedTriples[iDataTripleCount].print();
iDataTripleCount++;
_LastReceivedTripleCount=iDataTripleCount;
} 
返回true;
}
返回false;
}

a非常感谢你抽出时间帮助我


欢迎Schullebernd

ESP8266只能从正确对齐的地址(即多个数据类型的地址)读取
float
s(以及16位
int
s、32位
int
s、
double
s)。由于
float
的长度为4字节,因此只能从4的倍数地址读取。否则,您将得到提到的异常

    float* fTmp = (float*)iCurrentPos; // does not copy; takes a pointer to the orignal location

    float f;
    memcpy(&f, fTmp, sizeof(float)); // copies data to aligned variable

    Serial.println(f); // prints value from aligned variable

    Serial.println(*fTmp); // prints value for original, non-aligned location
您正在使用
uint8\t
数组
uint8_t
的长度为1字节,因此不需要也可能没有以任何方式对齐。阵列可能从地址170001开始。如果您现在在数组中的位置40处有一个
float
值,那么您的代码将尝试从地址170041读取一个
float
值,该值未正确对齐并导致异常

    float* fTmp = (float*)iCurrentPos; // does not copy; takes a pointer to the orignal location

    float f;
    memcpy(&f, fTmp, sizeof(float)); // copies data to aligned variable

    Serial.println(f); // prints value from aligned variable

    Serial.println(*fTmp); // prints value for original, non-aligned location
解决方案是首先将
float
值的字节复制到本地正确对齐的变量。这就是您要做的,但是您在非对齐位置再次访问它,并得到一个异常

    float* fTmp = (float*)iCurrentPos; // does not copy; takes a pointer to the orignal location

    float f;
    memcpy(&f, fTmp, sizeof(float)); // copies data to aligned variable

    Serial.println(f); // prints value from aligned variable

    Serial.println(*fTmp); // prints value for original, non-aligned location

去掉最后一行。

你知道对齐是什么,以及它如何应用于内存访问吗?谢谢,在阅读了Codo的答案后,我现在知道了更多细节。我从来没有遇到过这样的问题,也许我被C#和Java这样的高级语言宠坏了。哇,你完全正确。我真的不知道。我只是尝试了一下,然后一步一步地将数据中的键的长度增加了三倍,最后增加了长度,其中浮点值从缓冲区中的一个地址开始,该地址是4的多表,因此不会发生异常。老实说,这是一个有点有线。你有没有链接,我可以在那里读到更多关于这个的信息?非常感谢,这花费了我4个小时的生命。。。