地址的价值。给出例外

时间:2014-12-10 17:36:43

标签: c printf

我正在尝试在微控制器上打印一块内存。

它抱怨这条线:

stringLength += sprintf(buffer + stringLength, "0x%X, ", *(startAddress + i));

由于:

*(startAddress + i)

为什么这不起作用?它不应该只是给我地址的价值吗?

使用ARM微控制器并跳转到HardFault_Handler()(通常表示非法内存访问)。

如何在内存位置获取值呢?

整个功能:

void printMemory(uint32_t *startAddress, uint32_t lengthInBytes)
{
    uint32_t stringLength = 0;
    uint32_t i;
    char buffer[1000];
    uint32_t start = (uint32_t) startAddress;

    for (i = start; i < (start + lengthInBytes); i++)
    {
        if ((i % 10) == 0)
        {
            stringLength += sprintf(buffer + stringLength, "\r\n");
        }

        stringLength += sprintf(buffer + stringLength, "0x%X, ", *(startAddress + i));
    }

    //Add line termination.
    stringLength += sprintf(buffer + strlen(buffer), "%c", '\0');

    //Printf buffer.
    printf("%s", &buffer[0]);
}

修改

工作示例:

void printMemory(uint32_t startAddress, uint32_t lengthInBytes)
{
    uint32_t i;
    char buffer[1000];
    uint32_t stringLength = 0;
    uint8_t *start;

    //Create a uint8_t pointer to address.
    start = (uint8_t *) startAddress;

    for (i = 0; i < lengthInBytes; i++)
    {
        if ((i % 10) == 0)
        {
            stringLength += sprintf(buffer + stringLength, "\r\n");
        }

        stringLength += sprintf(buffer + stringLength, "0x%X, ", *(start + i));
    }

    //Add line termination.
    stringLength += sprintf(buffer + strlen(buffer), "%c", '\0');

    //Printf buffer.
    printf("%s", &buffer[0]);
}

1 个答案:

答案 0 :(得分:3)

你犯了一个错误:

istart开始,即startAddress,因此在第一次迭代中您将获取:

*(startAddress + startAddress)