二维结构 C 数组的未定义行为
Undefined behavior with 2d array of struct C
我有一个二维结构数组,我正在为其分配字符串,这是我的结构。
struct node {
char* value;
};
这是我的分配(我是 C 的新手,所以我不确定它是否正确)但是总会有 35 列,但可能有数百万行。(我现在只有 3测试)
const int rows=3;
struct node ** arrayofnodes[rows][35];
for(int i=0; i<rows; i++) {
array[i] = malloc(test * sizeof array[0]);
for(int j=0; j<35; j++) array[i][j] = malloc(sizeof array[0][0]);
}
然后我从一个 csv 文件中逐个字符地读入一个临时字符串,然后使用下面的方法将临时值分配到我想要的位置。
//int row and count are defined in my while loop I have for counting commas(or what col I am on) then new lines for the rows
arrayofnodes[row][count]->value=strdup(temp);
printf("%s \n", arrayofnodes[row][count]->value);
printf("%d %d \n",row, count );
当我按照上面的方式分配时,它似乎起作用了。我添加了这些打印语句以确保它分配了正确的值。
例如上面的例子会打印出类似
的东西
Red
0 0
这对那个职位来说是正确的。
但是在我完成所有作业之后。我放置了一个打印语句 printf("%s \n", arrayofnodes[0][0]->value);
来测试我是否可以检索如上所示的第一个值,它应该是 "Red"。
在我的终端中输出“@`??”或“@Pz?”或者只是任何随机输出。除了 0,0 之外,我已经尝试了很多不同的位置,但它们都得到了相同的结果。我想我只是很困惑为什么打印语句在我分配它们之后立即工作,但在我稍后调用它们时却没有在我的代码末尾工作。
您可以通过在结构中使用常量大小来避免过于复杂的分配:
struct OneRow
{
char Value[35];
}
const int Rows=3;
OneRow *MyArray=NULL;
MyArray = (OneRow*) malloc (Rows*sizeof(OneRow));
您现在可以访问每个元素(字符)或整个字符串作为
MyArray[rownumber].Value[colnumber] = …
strcpy (MyArray[rownumber].Value, "I'm_shorter_than_35"); //34 chars max + null-term
这就是您正在尝试做的事情。您将需要扫描您的 csv 文件并计算所需的行数,然后根据需要填充值。
#include <stdio.h>
#include <stdlib.h>
struct node {
char* value;
};
int main() {
const int rows = 3; // you will need to compute this beforehand
const int columns = 35;
struct node** arrayofnodes = malloc(rows * sizeof(struct node*));
for (int i = 0; i < rows; ++i) {
arrayofnodes[i] = malloc(columns * sizeof(struct node));
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
arrayofnodes[i][j].value = malloc(...);
strcpy(arrayofnodes[i][j].value, ...); // etc..
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
free(arrayofnodes[i][j].value);
}
}
for (int i = 0; i < rows; ++i) {
free(arrayofnodes[i]);
}
free(arrayofnodes);
}
我有一个二维结构数组,我正在为其分配字符串,这是我的结构。
struct node {
char* value;
};
这是我的分配(我是 C 的新手,所以我不确定它是否正确)但是总会有 35 列,但可能有数百万行。(我现在只有 3测试)
const int rows=3;
struct node ** arrayofnodes[rows][35];
for(int i=0; i<rows; i++) {
array[i] = malloc(test * sizeof array[0]);
for(int j=0; j<35; j++) array[i][j] = malloc(sizeof array[0][0]);
}
然后我从一个 csv 文件中逐个字符地读入一个临时字符串,然后使用下面的方法将临时值分配到我想要的位置。
//int row and count are defined in my while loop I have for counting commas(or what col I am on) then new lines for the rows
arrayofnodes[row][count]->value=strdup(temp);
printf("%s \n", arrayofnodes[row][count]->value);
printf("%d %d \n",row, count );
当我按照上面的方式分配时,它似乎起作用了。我添加了这些打印语句以确保它分配了正确的值。
例如上面的例子会打印出类似
的东西Red 0 0
这对那个职位来说是正确的。
但是在我完成所有作业之后。我放置了一个打印语句 printf("%s \n", arrayofnodes[0][0]->value);
来测试我是否可以检索如上所示的第一个值,它应该是 "Red"。
在我的终端中输出“@`??”或“@Pz?”或者只是任何随机输出。除了 0,0 之外,我已经尝试了很多不同的位置,但它们都得到了相同的结果。我想我只是很困惑为什么打印语句在我分配它们之后立即工作,但在我稍后调用它们时却没有在我的代码末尾工作。
您可以通过在结构中使用常量大小来避免过于复杂的分配:
struct OneRow
{
char Value[35];
}
const int Rows=3;
OneRow *MyArray=NULL;
MyArray = (OneRow*) malloc (Rows*sizeof(OneRow));
您现在可以访问每个元素(字符)或整个字符串作为
MyArray[rownumber].Value[colnumber] = …
strcpy (MyArray[rownumber].Value, "I'm_shorter_than_35"); //34 chars max + null-term
这就是您正在尝试做的事情。您将需要扫描您的 csv 文件并计算所需的行数,然后根据需要填充值。
#include <stdio.h>
#include <stdlib.h>
struct node {
char* value;
};
int main() {
const int rows = 3; // you will need to compute this beforehand
const int columns = 35;
struct node** arrayofnodes = malloc(rows * sizeof(struct node*));
for (int i = 0; i < rows; ++i) {
arrayofnodes[i] = malloc(columns * sizeof(struct node));
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
arrayofnodes[i][j].value = malloc(...);
strcpy(arrayofnodes[i][j].value, ...); // etc..
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
free(arrayofnodes[i][j].value);
}
}
for (int i = 0; i < rows; ++i) {
free(arrayofnodes[i]);
}
free(arrayofnodes);
}