此数组分配的3D版本似乎工作正常,但添加第四个维度显然会导致段错误。编辑:(结果指针的东西很好,只是内循环中的一对拼写错误)
#include <stdio.h>
#include <stdlib.h>
#define M 2
#define N 3
#define O 4
#define P 5
int main()
{
int ****A = malloc(sizeof(int***)*M);
int i, j, k, m;
if(!A)
{
printf (" **** Out of Memory");
}
for(i = 0; i < M; i++)
{
A[i] = malloc(sizeof(int**)*N);
if(!A[i])
{
printf(" *** Out of Memory");
}
for(j = 0; j < N; j++)
{
A[i][j] = malloc(sizeof(int*)*O);
if(!A[i][j])
{
printf(" ** Out of Memory");
}
for(k = 0; k < O; k++)
{
/* TYPO 1 - former site of main problem, a "==" typo */
A[i][j][k] = malloc(sizeof(int)*P);
if(!A[i][j][k])
{
printf(" * Out of Memory\n");
}
/* TYPO 2 - former site of ")A;" typo unrelated to main problem */
printf("Allocation: A[i][j][k] at %p, A[i][j]: %p, A[i]: %p, A: %p\n", A[i][j][k], A[i][j], A[i], A);
}
}
}
return 0;
}
Mingw在18 - 26次分配后出现out of memory
错误,无论M,N,O,P的大小是多少。
在linux gcc上运行的相同代码会立即产生错误和段错误
此代码的三维版本(仅少一个for循环)可以工作,不会产生错误。
mingw输出:
Allocation: A[i][j][k] at 00B326F8, A[i][j]: 00B30D60, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 00B300C0, A[i][j]: 00B30D60, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 69575C3A, A[i][j]: 00B30D60, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 776F646E, A[i][j]: 00B30D60, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 00B33700, A[i][j]: 00B30DF8, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 00B300C0, A[i][j]: 00B30DF8, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 45434F52, A[i][j]: 00B30DF8, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 524F5353, A[i][j]: 00B30DF8, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 00B33700, A[i][j]: 00B30E90, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 00B300C0, A[i][j]: 00B30E90, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 6F646E69, A[i][j]: 00B30E90, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 535C7377, A[i][j]: 00B30E90, A[i]: 00B30D48, A: 76FD74CD
Allocation: A[i][j][k] at 00B33700, A[i][j]: 00B30F40, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 00B300C0, A[i][j]: 00B30F40, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 6D766E44, A[i][j]: 00B30F40, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 5C3A433B, A[i][j]: 00B30F40, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 00B30FD8, A[i][j]: 00B304A0, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 00B300C0, A[i][j]: 00B304A0, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 00000231, A[i][j]: 00B304A0, A[i]: 00B30F28, A: 76FD74CD
* Out of Memory
Allocation: A[i][j][k] at 00000000, A[i][j]: 00B304A0, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 00B304D8, A[i][j]: 00B313E0, A[i]: 00B30F28, A:
76FD74CD
Allocation: A[i][j][k] at 00B300C0, A[i][j]: 00B313E0, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 6F646E69, A[i][j]: 00B313E0, A[i]: 00B30F28, A: 76FD74CD
Allocation: A[i][j][k] at 00007377, A[i][j]: 00B313E0, A[i]: 00B30F28, A: 76FD74CD
Process returned 255 (0xFF) execution time : 3.891 s
在linux上,相同的代码会产生:
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617028, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617028, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617028, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617028, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96170a0, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96170a0, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96170a0, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96170a0, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617118, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617118, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617118, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617118, A[i]: 0x9617018, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96171a0, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96171a0, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96171a0, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x96171a0, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617218, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617218, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617218, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617218, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617290, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617290, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617290, A[i]: 0x9617190, A: 0x24ab80
* Out of Memory
Allocation: A[i][j][k] at (nil), A[i][j]: 0x9617290, A[i]: 0x9617190, A: 0x24ab80
Segmentation fault
编辑:根本不是指针符号问题。在打印声明中有一个随机的“==”而不是“=”和错误的“)”。
答案 0 :(得分:4)
A[i][j][k] == (int*)malloc(sizeof(int)*P);
然后你尝试访问它
if(!A[i][j][k])
给你分段错误。
这适用于3
维度?不。也许是因为你没有这个循环。
更正
A[i][j][k] = malloc(sizeof(int)*P);
你不需要那个演员 - 这是多余的。
如果你这样做,所有这4个mallocs都可以更清洁,更轻松
int ****A = malloc(sizeof *A * M);
和
A[i] = malloc(sizeof *A[i] * N);
还要像这样编译你的代码
gcc -Wall -Werror progname.c
这会发出警告,让你意识到这种平等 - 同等问题。
插图代码: -
#include <stdio.h>
#include <stdlib.h>
#define M 2
#define N 3
#define O 4
#define P 5
int main(void)
{
int ****A = malloc(sizeof*A*M);
if(!A)
{
perror(" *** Out of Memory");
exit(EXIT_FAILURE);
}
for(size_t i = 0; i < M; i++)
{
A[i] = malloc(sizeof *A[i]*N);
if(!A[i])
{
perror(" *** Out of Memory");
exit(EXIT_FAILURE);
}
for(size_t j = 0; j < N; j++)
{
A[i][j] = malloc(sizeof(int*)*O);
if(!A[i][j])
{
perror(" *** Out of Memory");
exit(EXIT_FAILURE);
}
for(size_t k = 0; k < O; k++)
{
A[i][j][k] = malloc(sizeof *A[i][j][k]*P);
if(!A[i][j][k])
{
perror(" *** Out of Memory");
exit(EXIT_FAILURE);
}
printf("Allocation: A[i][j][k] at %p, A[i][j]: %p, A[i]: %p, A: %p\n", (void*)A[i][j][k], (void*)A[i][j], (void*)A[i], (void*)A);
}
}
}
return 0;
}