C内存在运行时崩溃

C内存在运行时崩溃,c,pointers,memory,struct,memcpy,C,Pointers,Memory,Struct,Memcpy,我有这个问题。 每当我尝试调用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 (3

我有这个问题。 每当我尝试调用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);
}

在谷歌搜索未初始化指针是什么之后 我意识到了我的问题

感谢alk、Paul Hankin和Jiri Volejnik的回答

我加了这些线来修复它

QCPU* init()
{
    QCPU* r = malloc(sizeof(QCPU)); // Allocated Pointer To QCPU
    r->dCache = malloc(32);
    r->iCache = malloc(32);
    r->port_table = malloc(8);
    return r;
}

指针不是数组。行cpu->dCache[3]=5;取消对未初始化指针cpu->dCache的引用,然后写入找到5的随机地址。cpu->dCache是一个未初始化指针。您可能会发现它很有用。如果它总是32,您也可以写入uint8_t dCache[32];在结构中。