C - 无法访问地址的内存 [英] C - Cannot access memory at address

查看:213
本文介绍了C - 无法访问地址的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调试GDB告诉我以下错误:

When debugging GDB tells me the following error:

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

如果在调试时调用ConvertByteArrayToFloat时放置了断点,则会产生错误。

The error is produced if I put a breakpoint when I call ConvertByteArrayToFloat while debugging .

但程序退出没有问题,给我一个确定结果?

But the program exits without a problem and gives me an Ok result ?

我的主文件:

#include "Local.h"

int main(void) {

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

Local.h

#ifndef LOCAL_H_
#define LOCAL_H_

#include "Common.h"

T_OP_STATUS HandleReceivedMessages(void);

#endif

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_ */

处理程序。 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

#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_ */

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;
}

这是最小版本,(它做了很多事情,如检查消息的格式,CRC等),但是从头到尾通过所有这些文件。

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.

我正在一个嵌入式平台上工作,当我的微控制器调试并调用函数ConvertByteArrayToFloat时,我的程序跳转到我的代码的其他部分,然后它崩溃了微控制器。

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.

我尝试在没有微控制器的情况下重新创建计算机中的错误,我发现错误在顶部。

I try to recreate the error in my computer without the microcontroller and I found the error at the top.

推荐答案

这个:

TempFloat = ConvertByteArrayToFloat(&rawDataPtr[3]);

(其中 rawDataPtr 是一个 uint8_t * 参数)看起来很可疑。你转过头到 rawDataPtr (与 rawDataPtr + 3 相同)的第四个字节到转换函数,然后读取从该位置开始的四个字节。

(where rawDataPtr is a uint8_t * argument) looks very suspicious. 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.

在消息消息的初始字节之后,这看起来像某种混乱。

This looks like some kind of confusion after consuming the initial bytes of the message.

这篇关于C - 无法访问地址的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆