从外部访问函数内部分配的数组 - 意外结果
Accessing array malloced inside a function from outside - unexpected results
我有一个函数设计用于 malloc 一个数组,然后用文件中的值填充它(n 维坐标,虽然现在在 2d 中工作)。
#include <stdio.h>
#include <stdlib.h>
#define dim 2
typedef struct {
double **array; /*store the coordinates*/
int num; /* store the number of coordinates*/
/* store some other things too */
} foo;
void read_particles(foo *bar);
int main(void)
{
foo bar;
read_particles(&bar);
printf("\n");
for(int i = 0; i < bar.num; i++)
printf("%f %f\n", bar.array[0][i], bar.array[1][i]);
/* printf here does not output the array properly.
* Some values are correct, some are not.
* Specifically, the first column bar.array[0][i] is correct,
* the second column bar.array[1][i] is not, some values from the
* first column are appearing in the second.
*/
return 0;
}
void read_particles(foo *bar)
{
FILE *f = fopen("xy.dat", "r");
/* read number of coordinates from file first*/
fscanf(f, "%d", &bar->num);
bar->array = (double**) malloc(bar->num * sizeof(double*));
for(int i = 0; i < bar->num; i++)
bar->array[i] = (double*) malloc(dim * sizeof(double));
for(int i = 0; i < bar->num; i++)
{
for(int j = 0; j < dim; j++)
fscanf(f, "%lf", &(bar->array[j][i]));
/* For now, coordinates are just 2d, print them out
* The values are displayed correctly when printing here*/
printf("%f %f\n", bar->array[0][i], bar->array[1][i]);
}
fclose(f);
}
Some sample data is available here.
当值从函数内部打印时它们很好,当它们在函数外部打印时则不然。所以我一定没有正确处理这些指针。可能(或可能不)值得注意的是,我最初没有使用结构并将函数定义为 double **read_and_malloc(num)
,返回指向数组的指针,并且产生的输出是相同的。
所以这是怎么回事?
如果需要,我可以包含一些示例数据或任何其他信息。
你的第二个循环不正确:
for(int i = 0; i < dim; i++)
bar->array[i] = (double*) malloc(dim * sizeof(double));
您创建了 bar->num
个元素,但您迭代了 dim
个元素:
bar->array = (double**) malloc(bar->num * sizeof(double*))
循环应遍历第一维中的元素数:bar->num
在更新后的代码中,您分配了 bar->num
行和 2
列。但是,您的 fscanf
和 printf
代码尝试处理具有 2
行和 bar->num
列的数组。
为了保持您的 reading/writing 代码完整,分配代码为:
bar->array = malloc(dim * sizeof *bar->array);
for (int i = 0; i < dim; ++i)
bar->array[j] = malloc(bar->num * sizeof *bar->array[j]);
注意。如果您不熟悉这个 malloc 习惯用法,see here
我有一个函数设计用于 malloc 一个数组,然后用文件中的值填充它(n 维坐标,虽然现在在 2d 中工作)。
#include <stdio.h>
#include <stdlib.h>
#define dim 2
typedef struct {
double **array; /*store the coordinates*/
int num; /* store the number of coordinates*/
/* store some other things too */
} foo;
void read_particles(foo *bar);
int main(void)
{
foo bar;
read_particles(&bar);
printf("\n");
for(int i = 0; i < bar.num; i++)
printf("%f %f\n", bar.array[0][i], bar.array[1][i]);
/* printf here does not output the array properly.
* Some values are correct, some are not.
* Specifically, the first column bar.array[0][i] is correct,
* the second column bar.array[1][i] is not, some values from the
* first column are appearing in the second.
*/
return 0;
}
void read_particles(foo *bar)
{
FILE *f = fopen("xy.dat", "r");
/* read number of coordinates from file first*/
fscanf(f, "%d", &bar->num);
bar->array = (double**) malloc(bar->num * sizeof(double*));
for(int i = 0; i < bar->num; i++)
bar->array[i] = (double*) malloc(dim * sizeof(double));
for(int i = 0; i < bar->num; i++)
{
for(int j = 0; j < dim; j++)
fscanf(f, "%lf", &(bar->array[j][i]));
/* For now, coordinates are just 2d, print them out
* The values are displayed correctly when printing here*/
printf("%f %f\n", bar->array[0][i], bar->array[1][i]);
}
fclose(f);
}
Some sample data is available here.
当值从函数内部打印时它们很好,当它们在函数外部打印时则不然。所以我一定没有正确处理这些指针。可能(或可能不)值得注意的是,我最初没有使用结构并将函数定义为 double **read_and_malloc(num)
,返回指向数组的指针,并且产生的输出是相同的。
所以这是怎么回事?
如果需要,我可以包含一些示例数据或任何其他信息。
你的第二个循环不正确:
for(int i = 0; i < dim; i++)
bar->array[i] = (double*) malloc(dim * sizeof(double));
您创建了 bar->num
个元素,但您迭代了 dim
个元素:
bar->array = (double**) malloc(bar->num * sizeof(double*))
循环应遍历第一维中的元素数:bar->num
在更新后的代码中,您分配了 bar->num
行和 2
列。但是,您的 fscanf
和 printf
代码尝试处理具有 2
行和 bar->num
列的数组。
为了保持您的 reading/writing 代码完整,分配代码为:
bar->array = malloc(dim * sizeof *bar->array);
for (int i = 0; i < dim; ++i)
bar->array[j] = malloc(bar->num * sizeof *bar->array[j]);
注意。如果您不熟悉这个 malloc 习惯用法,see here