用C语言创建动态矩阵
我试图用c理解malloc和动态分配,但当我编译程序时一切都正常,但如果我运行它,终端会告诉我分段错误(内核转储),然后退出用C语言创建动态矩阵,c,memory,dynamic,malloc,C,Memory,Dynamic,Malloc,我试图用c理解malloc和动态分配,但当我编译程序时一切都正常,但如果我运行它,终端会告诉我分段错误(内核转储),然后退出 #include <stdio.h> #include <stdlib.h> int main(){ int **matrice; int righe, colonne; int r, c; printf("Quante RIGHE deve avere la matrice? "); scanf("%d
#include <stdio.h>
#include <stdlib.h>
int main(){
int **matrice;
int righe, colonne;
int r, c;
printf("Quante RIGHE deve avere la matrice? ");
scanf("%d", &righe);
printf("Quante COLONNE deve avere la matrice? ");
scanf("%d", &colonne);
matrice = (int**) malloc(righe*colonne*sizeof(int));
for(r=0; r<righe; r++){
matrice[r] = (int*) malloc(colonne*sizeof(int));
for(r=0; r<righe; r++){
for(c=0; c<colonne; c++){
printf("Elemento[%d][%d]: ",r, c);
scanf("%d", &matrice[r][c]);
}
// print out
for(r=0; r<righe; r++){
for(c=0; c<colonne; c++){
printf ("%d\n", matrice[r][c]);
}
}
}
}
}
#包括
#包括
int main(){
整数**矩阵;
科隆里格内;
int r,c;
printf(“是否有权开发矩阵?”);
scanf(“%d”&righe);
printf(“矩阵的平均值是多少?”);
scanf(“%d”和科隆);
矩阵=(int**)malloc(righe*colonne*sizeof(int));
对于(r=0;r您文章中的确切问题已经在评论中得到了回答(现在在接受的答案中),但另一个建议是将2D矩阵的分配合并到一个函数中,例如:
int ** Create2D(int c, int r)
{
int **arr;
int y;
arr = calloc(c, sizeof(int *));
for(y=0;y<c;y++)
{
arr[y] = calloc(r, sizeof(int));
}
return arr;
}
您错过了一个“*”并嵌套了错误的最后两个循环。我相信这就是您想要的:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **matrice;
int righe, colonne;
int r, c;
printf("Quante RIGHE deve avere la matrice? ");
scanf("%d", &righe);
printf("Quante COLONNE deve avere la matrice? ");
scanf("%d", &colonne);
matrice = (int **) malloc(righe * sizeof(int *)); //<<< here
for(r=0; r<righe; r++)
{
matrice[r] = (int *) malloc(colonne * sizeof(int));
for(c = 0; c < colonne; ++c) //<<< here
{
printf("Elemento[%d][%d]: ", r, c);
scanf("%d", &matrice[r][c]);
}
}
for(r = 0; r < righe; ++r) //<<< here
for(c = 0; c < colonne; ++c)
printf ("%d\n", matrice[r][c]);
}
#包括
#包括
int main()
{
整数**矩阵;
科隆里格内;
int r,c;
printf(“是否有权开发矩阵?”);
scanf(“%d”&righe);
printf(“矩阵的平均值是多少?”);
scanf(“%d”和科隆);
matrice=(int**)malloc(righe*sizeof(int*);//您有许多步骤不符合顺序,您为循环对您的matrice
进行的排序不正确。您还缺少对所有输入和所有分配的验证。要纠正问题,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
int main(){
int **matrice;
int righe, colonne;
int r, c;
printf("Quante RIGHE deve avere la matrice? ");
if (scanf("%d", &righe) != 1) {
fprintf (stderr, "error: invalid input - righe.\n");
return 1;
}
printf("Quante COLONNE deve avere la matrice? ");
if (scanf("%d", &colonne) != 1) {
fprintf (stderr, "error: invalid input - colonne.\n");
return 1;
}
matrice = malloc (righe * sizeof *matrice);
if (!matrice) {
perror ("matrice");
return 1;
}
for (r = 0; r < righe; r++){
matrice[r] = malloc (colonne * sizeof *matrice[r]);
if (!matrice[r]) {
perror ("matrice[r]");
return 1;
}
for (c = 0; c < colonne; c++){
printf ("Elemento[%d][%d]: ",r, c);
if (scanf ("%d", &matrice[r][c]) != 1) {
fprintf (stderr, "error: matrice[r][c].\n");
return 1;
}
}
}
// print out
for (r = 0; r < righe; r++){
for (c = 0; c < colonne; c++)
printf (" %3d", matrice[r][c]);
putchar ('\n');
free (matrice[r]);
}
free (matrice);
}
请仔细检查,如果您还有其他问题,请告诉我。问题在于malloc
呼叫,您分配的电话号码错误
字节
矩阵是由一个指向数组的指针创建的,该数组包含指向的指针
int
的数组
int **matrix = calloc(rows, sizeof(*matrix));
if(matrix == NULL)
// handle the error
现在必须初始化列:
for(int i = 0; i < rows; ++i)
matrix[i] = calloc(cols, sizeof *matrix[i]);
if(matrix[i] == NULL)
// handle the error
请注意,我使用了calloc
而不是malloc
使用两个参数(而不是一个)是指calloc
设置分配的内存
到0。当您正在执行错误处理并希望释放时,这是一个巨大的帮助
内存。空闲(空)
不被禁止
创建专用函数(如create_matrix
和
free\u matrix
。将所有内容设置为0非常方便
最后一件事:我建议在调用malloc
和calloc
。很容易错过正确的类型、错过*
或出错
通常情况下,sizeof*var
但是总是返回正确的
字节。您不应该强制转换malloc
-这是一种糟糕的做法。为什么在使用相同名称的循环中使用同名变量matrice=malloc(nrows*sizeof*matrice)
(即sizeof(int*)
)。然后是matrice[r]=malloc(ncols*sizeof*matrice[r]);
(即sizeof(int)
)。注意:您必须验证每个分配是否成功,例如,如果(!matrice[r]){/*handle error*/}
您走上了正确的道路,找到根本原因并解决以下问题是学习、理解和使用动态内存分配的一部分:“当我编译程序时,一切正常,但如果我运行它,终端会告诉我分段错误(内核转储)并退出“。您还需要对二维矩阵在内存中的布局有清晰的理解和脑海中的图像。在您的情况下,二维矩阵由一个块a内存组成,其中包含一个指针数组,一个指针对应一列。此类指针的数量与行数相同(因此,第一个malloc应该是size=行数乘以(int*)的大小),这是您需要在代码中修复的第一件事,@David C RankinSure已经指出了这一点,很高兴提供帮助!在free2D
函数中执行arr=NULL
有什么意义?仅释放变量并不会将变量设置为NULL
,而且在大型项目中使用动态内存时,例如在拓扑包括几个可能需要或可能不需要使用结构数组的位置,以测试其当前状态是否为NULL
,即是否已分配或需要分配。如果为NULL,则分配,否则不分配。但在这种情况下,它仅在函数末尾将局部变量arr
设置为NULL
,它对free2D
的调用者没有任何意义。如果它类似于*arr=NULL
(我知道它不适合签名)我可以理解。“巴勃罗,我认为这是一个很好的实践。”Ryyk说,“对于一个程序来说,这一点在 Fieldtypedef
创建一个具有extern
作用域的指针,使其在多个模块中可见。当动态地将内存分配给righe
xcolonne
2D数组时,您认为这是正确的->matrice=(int**)malloc(righe*colonne*sizeof(int*);
?malloc(里格*科隆*尺寸(国际*)
会给你一个比你想要的更多的int*
s维度。OP需要选择righe
或colonne
和malloc
那么多int*
s,对其进行迭代,并malloc
为每次迭代选择其他数量的int
s。此外,还可以对malloc
i的结果进行铸造nc
是的,你们是对的,伙计们!因为每行有一个分配,所以初始分配应该只基于行数。我在答案中修正了它。@yano ups,你们是对的。我修正了我的答案。谢谢你们指出这一点。
$ ./bin/arrmatrice
Quante RIGHE deve avere la matrice? 3
Quante COLONNE deve avere la matrice? 3
Elemento[0][0]: 1
Elemento[0][1]: 2
Elemento[0][2]: 3
Elemento[1][0]: 4
Elemento[1][1]: 5
Elemento[1][2]: 6
Elemento[2][0]: 7
Elemento[2][1]: 8
Elemento[2][2]: 9
1 2 3
4 5 6
7 8 9
int **matrix = calloc(rows, sizeof(*matrix));
if(matrix == NULL)
// handle the error
for(int i = 0; i < rows; ++i)
matrix[i] = calloc(cols, sizeof *matrix[i]);
if(matrix[i] == NULL)
// handle the error
for(int i = 0; i < rows; ++i)
free(matrix[i]);
free(matrix);