C - Cannot access memory at address

时间:2016-07-11 20:24:08

标签: c gdb embedded c11

When debugging GDB tells me the following error:

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

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 ?

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

#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

#ifndef HANDLER_H_
#define HANDLER_H_

#include "Common.h"

T_MESSAGE_STATUS HandleResponseMessage(uint8_t *requestData, uint8_t msgLength);


#endif /* HANDLER_H_ */

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

#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

#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.

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.

1 个答案:

答案 0 :(得分:2)

This:

TempFloat = ConvertByteArrayToFloat(&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.