简体   繁体   English

C-无法访问地址处的内存

[英]C - Cannot access memory at address

When debugging GDB tells me the following error: 调试GDB时,出现以下错误:

0x800c99ed00000001 < error: Cannot access memory at address 0x800c99ed00000001>

The error is produced if I put a breakpoint when I call ConvertByteArrayToFloat while debugging . 如果在调试时调用ConvertByteArrayToFloat时设置了断点,则会产生错误。

But the program exits without a problem and gives me an Ok result ? 但是程序退出没有问题,结果还可以吗?

My main file: 我的主文件:

#include "Local.h"

int main(void) {

    if(HandleReceivedMessages() == OP_COMPLETED){
        printf("Main Completed \n" );
    } else {
        printf("Main Failed \n");
    }
    return 0;
}

Local.h Local.h

#ifndef LOCAL_H_
#define LOCAL_H_

#include "Common.h"

T_OP_STATUS HandleReceivedMessages(void);

#endif

Local.c Local.c

#include "Handler.h"
#include "Local.h"

uint8_t message[] = {0x86, 0x9a, 0xa0, 0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x10, 0x4a, 0x00, 0x00, 0x00, 0x00, 0xe1};

uint8_t length = 16;

T_OP_STATUS HandleReceivedMessages(void) {

    if(HandleResponseMessage(message, length) == STATUS_SUCCESS) {
        printf("Completed from Local \n");
        return OP_COMPLETED;
    } else {
        printf("Failed from Local \n");
        return OP_FAILED;
    }

}

Handler.h handler.h中

#ifndef HANDLER_H_
#define HANDLER_H_

#include "Common.h"

T_MESSAGE_STATUS HandleResponseMessage(uint8_t *requestData, uint8_t msgLength);


#endif /* HANDLER_H_ */

Handler.c Handler.c

#include "Handler.h"
#include <string.h>

static uint8_t rawRequestData[BUFFER_WIRED_SIZE];

static float TempFloat = 0;


T_MESSAGE_STATUS HandleCmd(uint16_t cmdNumber, uint8_t rawDataLength,
                    uint8_t *rawDataPtr) {

    switch (cmdNumber) {
        case 1:
            TempFloat = ConvertByteArrayToFloat(&rawDataPtr[3]);
            printf("The value of the float is : %f \n", TempFloat);
            return STATUS_SUCCESS;
        default:
            break;
    }

    return STATUS_NOT_IMPLEMENTED;
}


T_MESSAGE_STATUS HandleResponseMessage(uint8_t *message,
                                uint8_t msgLength) {

    uint8_t cmdNumber, dataLength, startOfData;


    // Check the delimiter.
    if (message[0] & INDICATOR_UNIQUE_ADDRESS) {

        cmdNumber = message[6];
        dataLength = message[7];
        startOfData = 8;

    } else {

        cmdNumber = message[2];
        dataLength = message[3];
        startOfData = 4;
    }

    // we copy only the real data from the command response
    memcpy(&rawRequestData, message + startOfData, dataLength);

    return HandleCmd(cmdNumber, dataLength, rawRequestData);

}

Common.h COMMON.H

#ifndef COMMON_H_
#define COMMON_H_

#include <stdint.h>
#include <stdio.h>

#define BUFFER_WIRED_SIZE               128
#define INDICATOR_UNIQUE_ADDRESS        0x80


typedef enum {

    OP_FAILED,
    OP_COMPLETED,

}T_OP_STATUS;

typedef enum
{
    STATUS_SUCCESS,
    STATUS_NOT_IMPLEMENTED,

} T_MESSAGE_STATUS;

float ConvertByteArrayToFloat(uint8_t *data);


#endif /* COMMON_H_ */

Common.c common.c中

#include "Common.h"

float ConvertByteArrayToFloat(uint8_t *data) {

    union {
        uint8_t tmpArray[4];
        float tmpFloat;
   } value;

    value.tmpArray[0] = data[3];
    value.tmpArray[1] = data[2];
    value.tmpArray[2] = data[1];
    value.tmpArray[3] = data[0];

    return value.tmpFloat;
}

This is the min version, (it does a lot of things like checking the format of the message, CRC, etc..)but goes from start to finish thru all those files. 这是最低版本,(它做了很多事情,例如检查消息的格式,CRC等。)但是从头到尾都是所有这些文件。

I am working on an embedded platform and when debugging in my microcontroller and calling the function ConvertByteArrayToFloat, my program jumps to some other part of my code and then it crashes the microcontroller. 我在嵌入式平台上工作,在微控制器中调试并调用函数ConvertByteArrayToFloat时,程序跳至代码的其他部分,然后使微控制器崩溃。

I try to recreate the error in my computer without the microcontroller and I found the error at the top. 我尝试在没有微控制器的情况下在计算机中重新创建错误,并且在顶部发现了错误。

This: 这个:

TempFloat = ConvertByteArrayToFloat(&rawDataPtr[3]);

(where rawDataPtr is a uint8_t * argument) looks very suspicious. (其中rawDataPtruint8_t *参数)看起来非常可疑。 You're passing a pointer to the fourth byte at rawDataPtr (same as rawDataPtr + 3 ) to the conversion function, which will then read four bytes starting at that location. 您正在将指向rawDataPtr的第四个字节的指针(与rawDataPtr + 3相同)传递给转换函数,该函数随后将从该位置开始读取四个字节。

This looks like some kind of confusion after consuming the initial bytes of the message. 在消耗了消息的初始字节之后,这看起来有些混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM