这个问题是Malloc调用崩溃的延续,但在其他地方有效
我尝试了以下程序,我发现它工作(即没有崩溃 - 这也在上面提到的链接中提到).我很幸运能让它工作但是我正在寻找SO专家对于它为什么有效的合理解释?!
这里有一些关于memory使用malloc()wrt structures和.的分配的基本理解pointers
malloc(sizeof(struct a) * n)分配n类型struct a元素的数量.并且,可以使用a来存储和访问该存储器位置pointer-to-type-"struct a".基本上是一个struct a *.malloc(sizeof(struct a *) * n)分配n类型struct a *元素的数量.然后,每个元素都可以指向类型的元素struct a.基本上malloc(sizeof(struct a *) * n)分配一个array(n-elements)-of-pointers-to-type-"struct a".并且,可以使用a来存储和访问分配的存储器位置pointer-to-(pointer-to-"struct a").基本上是一个struct a **.所以当我们创造一个时array(n-elements)-of-pointers-to-type-"struct a",就是它
struct a *而不是struct a **?array(n-elements)-of-pointers-to-type-"struct a"使用 …