指向指针分配的指针然后迭代导致分段错误

Pointer to pointer allocation then iteration causes segmentation fault

我在使用以下代码时遇到问题。我很确定这是正确的。我正在尝试将内存分配给函数中的指针,然后使用 for 循环将数据写入同一函数中分配的内存。但是当for循环中计数器达到2时,会导致段错误,或者我尝试在循环中设置一堆值或从中读取。

是的,我已经在 Whosebug 上对此进行了研究,但我没有发现以下答案对我有太大帮助。 * How can I allocate memory and return it (via a pointer-parameter) to the calling function?

#include <cassert>
#include <cstdlib>
#include <cstdio>

typedef unsigned char uint8_t;

void allocMem(uint8_t **i){
    *i = new uint8_t[10];
    for(int j = 0; j < 10; j++){
        printf("%d\n", *i[j]);
        //*i[j] = j * 2;
    }
}

int main(){
    uint8_t *i = 0;
    allocMem(&i);
    assert(i != NULL);
    delete[] i;
}

优先顺序错误:

printf("%d\n", *i[j]);

应该是

printf("%d\n", (*i)[j]);