对于学校的作业,我们必须使用结构来制作可以为无限数量的矩阵存储无限数量的点的矩阵。 (理论上无穷大)
对于作业,我决定使用 calloc 和 realloc。矩阵的大小是如何变化的:每次达到其点的限制时,它的大小都会加倍(因此它从 1 开始,然后变为 2,然后变为 4,依此类推)。每次添加矩阵时,它的大小也会加倍。
这就是我的问题所在。添加初始矩阵后,它会添加第二个矩阵名称和点,它给了我以下内容:
B?????????????????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ??????
B 是我想要的部分(因为我稍后会使用 strcmp),但是 ?标记不应该在那里。 (显然)
我不确定它为什么要这样做。由于代码是模块化的,因此很难获取其中的一部分来准确显示它是如何处理的。
注意:我可以通过以下方法访问矩阵的点:MyMatrix[1].points[0].x_cord;
(这只是一个示例)
产生问题的示例代码:
结构:
struct matrice {
char M_name[256];
int num_points[128];
int set_points[128];
int hasValues[1];
struct matrice_points * points;
} * MyMatrix;
struct matrice_points {
int set[1];
double cord_x;
double cord_y;
};
设置矩阵函数:
void setupMatrix(){
MyMatrix = calloc(1, sizeof(*MyMatrix));
numMatrix = 1;
}
增长矩阵函数:
void growMatrix(){
MyMatrix = realloc(MyMatrix, numMatrix * 2 * sizeof(*MyMatrix));
numMatrix = numMatrix * 2;
}
添加矩阵函数,在矩阵增长一次后输出此问题。
void addMatrix(char Name, int Location){
int exists = 0;
int existsLocation = 0;
for (int i = 0; i < numMatrix; i++){
if (strcmp(MyMatrix[i].M_name, &Name) == 0){
exists = 1;
existsLocation = i;
}
}
*MyMatrix[Location].M_name = Name;
printf("Stored Name: %s\n", MyMatrix[Location].M_name);
*MyMatrix[Location].num_points = 1;
*MyMatrix[Location].set_points = 0;
*MyMatrix[Location].hasValues = 1;
MyMatrix[Location].points = calloc(1, sizeof(*MyMatrix[Location].points));
}
最佳答案
void addMatrix(char Name, int Location)
char Name
表示单个char
,即整数类型的数量。 char
只是一个数字,根本不是字符串。
当你这样做时:
strcmp(..., &Name)
您假设存储该字符的位置代表一个有效的 C 字符串。这是错误的,没有理由会这样。如果你想将 C 字符串传递给这个函数,你需要像这样声明它:
void addMatrix(char *Name, int Location)
然后您需要将该 C 字符串复制到矩阵结构中的适当位置。它应该看起来像:
strncpy(... .M_name, Name, max_number_of_chars_you_can_store_in_M_Name);
此外,这些字段定义在您的结构中很奇怪:
int num_points[128];
int set_points[128];
int hasValues[1];
这意味着您的结构将包含一个包含 128 个整数的数组,称为 num_points,另一个包含 128 个整数的数组,称为 set_points,以及一个包含一个整数(奇怪)的数组,称为 hasValues。如果只需要存储总点数和设定点数,以及是否存储值的标志,定义应该是:
int num_points;
int set_points;
int hasValues;
并更正您的 addMatrix
函数中的分配。
如果您确实需要这些数组,那么您的赋值也是错误的。
请打开编译器中的所有警告。
关于c - 重新分配后的动态内存存储问题 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5361295/