C 2D数组损坏
我遇到一个问题,我的2D阵列正在损坏。考虑这个代码:C 2D数组损坏,c,arrays,C,Arrays,我遇到一个问题,我的2D阵列正在损坏。考虑这个代码: int **generate_clique_adjacency_matrix(const formula f) { int num_terms = f.num_clauses * kVarsPerClause; printf("\nnum_terms: %d\n", num_terms); // construct a 2D array of appropriate size int **compatible
int **generate_clique_adjacency_matrix(const formula f) {
int num_terms = f.num_clauses * kVarsPerClause;
printf("\nnum_terms: %d\n", num_terms);
// construct a 2D array of appropriate size
int **compatible = malloc(sizeof(int) * num_terms);
for (int i = 0; i < num_terms; i++) {
compatible[i] = malloc(sizeof(int) * num_terms);
// initialize every cell to 0
for (int j = 0; j < num_terms; j++) {
printf("writing 0 to (%d, %d)\n", i, j);
compatible[i][j] = 0;
}
}
printf("num_terms: %d\n", num_terms);
printf("matrix:\n");
for (int i = 0; i < num_terms; i++) {
printf("row %d: ", i);
for (int j = 0; j < num_terms; j++) {
printf("%d ", compatible[i][j]);
}
printf("\n");
}
exit(0);
}
我的阵列是如何损坏的?显然,第0行中的数据应该都是零。我甚至尝试添加断言以确保malloc
成功。这让我快发疯了 这是错误的:
int **compatible = malloc(sizeof(int) * num_terms);
您需要为指向int
的n个指针保留空间(不适用于nint
s)
改为
int **compatible = malloc(sizeof(int *) * num_terms);
或者更好
int **compatible = malloc(sizeof(*compatible) * num_terms);
这是错误的:
int **compatible = malloc(sizeof(int) * num_terms);
您需要为指向int
的n个指针保留空间(不适用于nint
s)
改为
int **compatible = malloc(sizeof(int *) * num_terms);
或者更好
int **compatible = malloc(sizeof(*compatible) * num_terms);
更改此项:
int **compatible = malloc(sizeof(int) * num_terms);
为此:
int **compatible = malloc(sizeof(int *) * num_terms);
因为第一个维度是指针数组
不要忘记在最后释放内存:
for (int i = 0; i < num_terms; i++)
free(compatible[i]);
free(compatible);
for(int i=0;i
更改此选项:
int **compatible = malloc(sizeof(int) * num_terms);
为此:
int **compatible = malloc(sizeof(int *) * num_terms);
因为第一个维度是指针数组
不要忘记在最后释放内存:
for (int i = 0; i < num_terms; i++)
free(compatible[i]);
free(compatible);
for(int i=0;i
正如其他答案中指出的,第一次分配需要为指向int
的指针分配空间,而不是int
s。最好在sizeof
操作数中使用标识符而不是显式类型,以避免在此处出错。此外,您还需要检查malloc()
的返回值是否存在分配错误:
int **compatible = malloc(sizeof(*compatible) * num_terms);
if (compatible == NULL) {
fprintf(stderr, "Row allocation error\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < num_terms; i++) {
compatible[i] = malloc(sizeof(**compatible) * num_terms);
if (compatible[i] == NULL) {
fprintf(stderr, "Column allocation error\n");
exit(EXIT_FAILURE);
}
// initialize every cell to 0
for (int j = 0; j < num_terms; j++) {
printf("writing 0 to (%d, %d)\n", i, j);
compatible[i][j] = 0;
}
}
如果您的阵列不是太大(这取决于系统),您可以简单地使用VLA。请注意,VLA的分配错误是无法检测到的
int compatible[num_terms][num_terms];
// initialize every cell to 0
for (size_t i = 0; i < num_terms; i++) {
for (size_t j = 0; j < num_terms; j++) {
printf("writing 0 to (%zu, %zu)\n", i, j);
compatible[i][j] = 0;
}
}
int兼容[num_terms][num_terms];
//将每个单元格初始化为0
对于(size\u t i=0;i
正如其他答案中指出的,第一次分配需要为指向int
的指针分配空间,而不是int
s。最好在sizeof
操作数中使用标识符而不是显式类型,以避免在此处出错。此外,您还需要检查malloc()
的返回值是否存在分配错误:
int **compatible = malloc(sizeof(*compatible) * num_terms);
if (compatible == NULL) {
fprintf(stderr, "Row allocation error\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < num_terms; i++) {
compatible[i] = malloc(sizeof(**compatible) * num_terms);
if (compatible[i] == NULL) {
fprintf(stderr, "Column allocation error\n");
exit(EXIT_FAILURE);
}
// initialize every cell to 0
for (int j = 0; j < num_terms; j++) {
printf("writing 0 to (%d, %d)\n", i, j);
compatible[i][j] = 0;
}
}
如果您的阵列不是太大(这取决于系统),您可以简单地使用VLA。请注意,VLA的分配错误是无法检测到的
int compatible[num_terms][num_terms];
// initialize every cell to 0
for (size_t i = 0; i < num_terms; i++) {
for (size_t j = 0; j < num_terms; j++) {
printf("writing 0 to (%zu, %zu)\n", i, j);
compatible[i][j] = 0;
}
}
int兼容[num_terms][num_terms];
//将每个单元格初始化为0
对于(size\u t i=0;i
始终喜欢指向VLA的指针:int(*兼容)[num_terms]
,不要鼓励使用int兼容[num_terms][num_terms]
(当使用2D数组时,这会很快使堆栈溢出)@KeineLust——我想这取决于所需数组的大小(以及堆栈限制)。我可以在我的系统上轻松获得1000 X 1000int
s的2d VLA。当然,如果阵列需要调整大小,那么2d VLA就不好了。是的,我也可以在我的计算机上创建它(1000*1000),但现在有很多嵌入式系统(堆栈限制降低了),效果要好得多;)始终喜欢指向VLA的指针:int(*兼容)[num_terms]
,不要鼓励使用int兼容[num_terms][num_terms]
(当使用2D数组时,这会很快使堆栈溢出)@KeineLust——我想这取决于所需数组的大小(以及堆栈限制)。我可以在我的系统上轻松获得1000 X 1000int
s的2d VLA。当然,如果阵列需要调整大小,那么2d VLA就不好了。是的,我也可以在我的计算机上创建它(1000*1000),但现在有很多嵌入式系统(堆栈限制降低了),效果要好得多;)