重新分配后的动态内存存储问题 - C
对于学校的作业,我们必须使用结构体来创建矩阵,该矩阵可以为无限量的矩阵存储无限量的点。 (理论上无限)
对于分配,我决定使用 calloc 和 realloc。矩阵的大小是这样的:每当其点数达到极限时,它的大小就会加倍(因此它从 1 开始,然后变为 2,然后为 4,依此类推)。每次添加矩阵时,它的大小也会加倍。
这就是我的问题所在。添加初始矩阵后,它会添加第二个矩阵名称和点,它给出以下内容:
B????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ??????????????????????????????????????????????????? ????????????????????????
B 是我想要的部分(因为我稍后使用 strcmp),但是 ?标记不应该在那里。 (显然)
我不确定它为什么要这么做。由于代码是模块化的,因此很难获取其中的某些部分来准确显示它是如何进行的。
注意:我可以通过以下方法访问矩阵的点:MyMatrix[1].points[0].x_cord;
(这只是一个示例)
产生问题的示例代码:
STRUCTS:
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;
};
Setup矩阵函数:
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));
}
For an assignment at school, we have to use structs to make matrices that can store a infinite amount of points for an infinite amount of matrices. (theoretical infinite)
For the assignment I decided to use calloc and realloc. How the sizes for the matrix go is: It doubles in size every time its limit is hit for its points (so it starts at 1, then goes to 2, then 4 and so on). It also doubles in size every time a matrix is added as well.
This is where my issue lies. After the initial matrix is added, and it goes to add the second matrix name and points, it gives me the following:
B???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
B is the portion of it that I want (as I use strcmp later on), but the ? marks are not supposed to be there. (obviously)
I am not sure why it is exactly doing this. Since the code is modular it isn't very easy to get portions of it to show exactly how it is going about this.
Note: I can access the points of the matrix via its method of: MyMatrix[1].points[0].x_cord;
(this is just an example)
Sample code that produces problem:
STRUCTS:
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;
};
Setup Matrix Function:
void setupMatrix(){
MyMatrix = calloc(1, sizeof(*MyMatrix));
numMatrix = 1;
}
Grow Matrix Function:
void growMatrix(){
MyMatrix = realloc(MyMatrix, numMatrix * 2 * sizeof(*MyMatrix));
numMatrix = numMatrix * 2;
}
Add Matrix Function which outputs this problem after growing the matrix once.
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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将“\0”添加到数据末尾。
Try adding '\0' to the end of your data.
您在此处复制单个字符,而不是字符串。如果您想要一个字符串,
Name
应定义为char *
,并且您应使用strcpy
。You're copying a single character here, not a string. If you want a string,
Name
should be defined aschar *
, and you should be usingstrcpy
.char Name
代表单个char
,即整数类型的数量。char
只是一个数字,它根本不是一个字符串。当您执行此操作时:
您假设存储该字符的位置代表有效的 C 字符串。这是错误的,没有理由会这样。如果要将 C 字符串传递给此函数,则需要像这样声明它:
然后需要将该 C 字符串复制到矩阵结构中的适当位置。它应该看起来像:
另外,这些字段定义在您的结构中很奇怪:
这意味着您的结构将包含一个名为 num_points 的 128 个整数数组,另一个名为 set_points 的 128 个整数数组,以及一个名为 hasValues 的一个整数(奇怪)数组。如果您只需要存储总点数和设定点的计数,以及指示是否存储值的标志,则定义应该是:
并更正
addMatrix
函数中的分配。如果您确实需要这些数组,那么您的分配也是错误的。
请打开编译器中的所有警告。
char Name
represents a singlechar
, i.e. a integer-type quantity.char
is just a number, it's not a string at all.When you do this:
you're assuming that the location where that one character is stored represents a valid C string. This is wrong, there is no reason why this should be the case. If you want to pass a C string to this function, you will need to declare it like this:
Then you need to copy that C string into the appropriate place in your matrix structure. It should look like:
Also these field definitions are strange in your struct:
This means that your struct will contain an array of 128 ints called num_points, another array of 128 ints calls set_points, and an array of one int (strange) called hasValues. If you only need to store the count of total points and set points, and a flag indicating whether values are stored, the definition should be:
and correct the assignments in your
addMatrix
function.If you do need those arrays, then your assignments as they are are wrong also.
Please turn on all warnings in your compiler.