C语言中的动态字符串矩阵
我有下一个问题。我试图创建一个字符串的动态矩阵,其维度由用户定义。我编写的下一个代码返回一个分段错误:C语言中的动态字符串矩阵,c,memory,matrix,dynamic,C,Memory,Matrix,Dynamic,我有下一个问题。我试图创建一个字符串的动态矩阵,其维度由用户定义。我编写的下一个代码返回一个分段错误: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char ***pa = NULL; int rows, columns , max, i, j = 0; //Ask dimension puts("Type number of
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char ***pa = NULL;
int rows, columns , max, i, j = 0;
//Ask dimension
puts("Type number of columns: ");
scanf("%d",&columns);
puts("Type number of rows: ");
scanf("%d",&rows);
puts("Type max string length: ");
scanf("%d",&max);
//Allocate in memory
***pa = (char ***)malloc(rows * sizeof(char**));
for(i=0;i<rows; i++){
pa[i] = (char **)malloc(columns * sizeof(char*));
for(j=0; i<columns;j++){
pa[i][j] = (char)malloc((max+1)*sizeof(char));
}
}
puts("Memory OK");
//Fill the matrix
i = 0;
j = 0;
for(i=0;i<rows;i++){
strncpy(pa[i][j] , "Hello world", max);
for(j=0;j<columns;j++){
strncpy(pa[i][j] , "Hello world", max);
}
}
//Shows the matrix
for(i=0;i<rows;i++){
puts(pa[i][j]);
for(j=0;j<columns;j++){
puts(pa[i][j]);
}
}
//Cleans the matrix
free(pa);
return 0;
}
我如何调试它以便知道发生了什么?我在编码时出错了吗?更改
pa[i][j] = (char)malloc((max+1)*sizeof(char));
进入:
另外,不需要在主中使用外部(第一个)strncpy
[编辑]
另外,第一个分配行***pa=…
应该是pa=…
。您希望为指针pa
分配一个地址,而不是取消引用它(***pa
取消引用尚未初始化的指针pa
) 我建议您充分利用c99,并将整个矩阵分配为单个块。您将减少程序内存的碎片,并使矩阵上的迭代更便于缓存:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int rows, columns , max;
puts("Type number of columns: ");
scanf("%d",&columns);
puts("Type number of rows: ");
scanf("%d",&rows);
puts("Type max string length: ");
scanf("%d",&max);
char (*mat)[columns][max+1] = malloc(rows * columns * (max + 1));
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j) {
strncpy(mat[i][j] , "Hello world", max);
}
}
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
puts(mat[i][j]);
}
}
free(mat);
return 0;
}
#包括
#包括
#包括
内部主(空){
int行、列、最大值;
puts(“输入列数:”);
scanf(“%d”列和列);
puts(“键入行数:”);
scanf(“%d”,行和行);
puts(“键入最大字符串长度:”);
扫描频率(“%d”和最大值);
char(*mat)[columns][max+1]=malloc(行*列*(max+1));
对于(int i=0;i
其他添加的bonii:
消除了分配内存的复杂性。不再使用malloc
s嵌套循环
无需循环使用free
ing整个过程,只需free
一个指针即可。你已经这样做了,但在你的情况下,它会泄漏内存
缺点:
可能无法为大型矩阵分配整个块。在这方面,分散的方法可能更好
谢谢大家帮我解决了这个问题。我和你一起学到了新东西。
首先,我改变了这个
pa = malloc(rows * sizeof(char**));
for(i=0;i<rows; i++){
pa[i] = malloc(columns * sizeof(char*));
for(j=0; j<columns;j++){//Here there was an i. That was totally wrong.
pa[i][j] = malloc((max+1)*sizeof(char));
}
}
pa=malloc(行*sizeof(char**));
对于(i=0;i***pa=(char***)malloc(rows*sizeof(char**));
应该是pa=malloc(rows*sizeof*pa);
。请原谅我的标签,我没有注意到。我认为每个变量都应该初始化,即使是指针。最好不要将其设置为NULL?在启用警告的情况下编译,并将警告视为错误。1)没有矩阵(又名2D数组)在您的代码中,没有任何东西可以指向一个或表示一个。2)您显然想要一个3D数组。你为什么不用呢?3) 作为一名三星级的C程序员并不是一种恭维。我知道这实际上是一个3D阵列。作为一名私人执行官,我试图以这种方式来做这件事。当然还有其他方法,但我想学习如何做到这一点。@squeamish ossifrage为什么?这是我发布的一个链接。@squeamish ossifrage不会引用低水平程序员的错误答案。@Vladfrommoskow,考虑到对C
标签的巨大贡献,称他为“低水平”这不仅是错误的,而且是彻头彻尾的荒谬。你可能不同意他的观点,但你的理由是一种非理性的论据,只不过是对人的攻击。事实上,这个问题确切地说明了为什么在你不需要的时候施放是一种射中你自己脚的好方法。所以在我做的方式中,元素在记忆中并不彼此接近。是吗?@CarlosManrique他们可能很接近。但是不是连续的。所以,你按照我在回答中的建议做了,但是没有施法(即使你可能保留了它)。在这种情况下,这通常被转化为一种向上投票,作为对那些花时间帮助你的人的感谢。我告诉你这些仅仅是因为你是新来的,不需要你投赞成票。正如你说的,我刚刚在页面上注册,我仍然在学习如何工作。对不起,我弄错了。没问题,我知道。我只是想提醒你注意这件事,这样其他人以后就不会觉得它不好了。祝你好运
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int rows, columns , max;
puts("Type number of columns: ");
scanf("%d",&columns);
puts("Type number of rows: ");
scanf("%d",&rows);
puts("Type max string length: ");
scanf("%d",&max);
char (*mat)[columns][max+1] = malloc(rows * columns * (max + 1));
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j) {
strncpy(mat[i][j] , "Hello world", max);
}
}
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
puts(mat[i][j]);
}
}
free(mat);
return 0;
}
pa = malloc(rows * sizeof(char**));
for(i=0;i<rows; i++){
pa[i] = malloc(columns * sizeof(char*));
for(j=0; j<columns;j++){//Here there was an i. That was totally wrong.
pa[i][j] = malloc((max+1)*sizeof(char));
}
}