【发布时间】:2021-06-09 09:47:27
【问题描述】:
我有这个问题。 每当我尝试调用 StorageStore 时,它都会在运行时崩溃。 我不知道如何解决它。 我试过谷歌搜索,但我对指针有点缺乏经验。 提前致谢。
编辑:我用 gcc -Ofast 编译
uint8_t Storage[256];
typedef struct _QCPU {
uint8_t pc; // 1
uint8_t *regs; // 7
uint8_t *dCache; // 8 (32)
uint8_t *iCache; // 8 (32)
uint8_t **port_table; // 8 (8)
void *str_load; // 8 (1)
void *str_store; // 8 (1)
struct Flags flags;
} QCPU;
void StorageStore(QCPU *CPU, uint8_t Addr)
{
memcpy(Storage+(Addr & 0xE0), CPU->dCache, 32);
}
QCPU* init()
{
return (QCPU*) malloc(sizeof(QCPU)); // Return Allocated Pointer To QCPU
}
int main()
{
QCPU *cpu = init();
cpu->dCache[3] = 5;
StorageStore(cpu, 5);
free(cpu);
}
【问题讨论】:
-
指针不是数组。
-
行
cpu->dCache[3] = 5;取消引用未初始化的指针cpu->dCache,然后写入找到5的随机地址。 -
CPU->dCache是一个未初始化的指针。 -
您可能会发现clang.llvm.org/docs/MemorySanitizer.html 很有帮助。
标签: c pointers memory struct memcpy