如何在结构中重新分配双精度数组
how to realloc an array of double inside a struct
我想动态更改结构中数组的大小。
我得到以下结构:
struct PolynomStruct {
double * term;
unsigned int size;
};
typedef struct PolynomStruct *Polynom;
当我尝试创建一个新的 Polynom 时,我必须为结构保留内存以使用结构内的变量,对吗?:
Polynom res = malloc(sizeof(struct PolynomStruct));
res->size = 10;
然后我想在索引 4 处的术语数组中添加一个双精度数。
所以它应该看起来像这样 [0,0,0,0,2.0000]。
我做的第一件事是重新分配数组的内存。
res->term = realloc(5 * sizeof(double));
我认为 sizeof(res->term) 应该是 5 * 8 字节 = 40 字节。
但是下面的代码returns 8.
printf("size term: %lu\n",sizeof(res->term));
"size term: 8"
后来我尝试这样做:
res->term[4] = 2;
printf("%f\n",res->term[4] );
它将“2.000000”打印到标准输出。我真的不明白这是怎么回事。
如果有人能给我提示,我会很高兴。
对不起我的英语。
sizeof(res->term)
returns 指针的大小,而不是分配的内存。
您需要手动跟踪分配的数量,即通过 res->size * sizeof(*term) 或类似的东西。
首先你不想要这个:
Polynom res = malloc(sizeof(struct PolynomStruct));
res->size = 10;
您已为结构分配 space 但未初始化所需的 term
指针:
Polynom res = malloc(sizeof(struct PolynomStruct));
if(res==NULL){
//Handle allocation failure...
}
res_>term=NULL;
res->size = 0;
//Later....
free(res->term);
free(res);
为 struct
分配 space 并将数组初始化为空。
请注意,可以将 NULL
传递给 free()
,它什么都不做,returns 正常。
或者如果您确实想预分配 10 个学期:
Polynom res = malloc(sizeof(struct PolynomStruct));
if(res==NULL){
//Handle allocation failure...
}
res->size = 10;
res_>term=malloc(res->size*sizeof(double));
if(res->term==NULL){
res->size=0;
//Handle error...
}
//Later (when finished with res)...
free(res->term);
free(res);
这会将数组预分配为 10。如果预分配,您可能希望跟踪 capac
(分配了多少)和 size
(使用了多少)。但这超出了这里的范围。
要重新分配,请编写如下函数:
int reallocate(Polynom res,int newsize){
double *resized=realloc(res->term,newsize*sizeof(double));
if (resize==NULL){
//Allocation failed. The array is the same size as before.
return 1; //Or handle error your own way.
}
res->term=resized;
res->size=newsize;
//realloc may extend the space allocated in place or realloc space elsewhere.
//If it does reallocate elsewhere the current contents are just copied over
//(byte for byte) and the old space freed.
return 0;//Success. No error.
}
//Later (when finished with res)...
free(res->term);
free(res);
通常明智的做法是 res=NULL;
以避免混淆事故。
请注意,如果指针由 malloc
或 realloc
(而不是 NULL
)返回,则它必须转到 free()
(恰好一次)。
另请注意 realloc
可以减小大小,因此 newsize < res->size
没问题。
我觉得指针与数组之间可能存在一些混淆。请阅读这篇有用的文章:Are pointers and arrays equivalent in C?
你不需要 "change the size of an array inside a struct" 因为拥有结构的全部意义在于保持稳定。因此,Polynom res = malloc(sizeof(struct PolynomStruct)) 将始终在堆上为 res 分配相同数量的 memo。
如果你想构建一个双精度堆数组并指向它,你可以这样做:
int member=10; // grow array size by member=member*2 for example
double a[]=malloc(member*sizeof(double));
term=a;
这样,您就可以动态地增加数组。
我想动态更改结构中数组的大小。 我得到以下结构:
struct PolynomStruct {
double * term;
unsigned int size;
};
typedef struct PolynomStruct *Polynom;
当我尝试创建一个新的 Polynom 时,我必须为结构保留内存以使用结构内的变量,对吗?:
Polynom res = malloc(sizeof(struct PolynomStruct));
res->size = 10;
然后我想在索引 4 处的术语数组中添加一个双精度数。 所以它应该看起来像这样 [0,0,0,0,2.0000]。 我做的第一件事是重新分配数组的内存。
res->term = realloc(5 * sizeof(double));
我认为 sizeof(res->term) 应该是 5 * 8 字节 = 40 字节。 但是下面的代码returns 8.
printf("size term: %lu\n",sizeof(res->term));
"size term: 8"
后来我尝试这样做:
res->term[4] = 2;
printf("%f\n",res->term[4] );
它将“2.000000”打印到标准输出。我真的不明白这是怎么回事。 如果有人能给我提示,我会很高兴。 对不起我的英语。
sizeof(res->term)
returns 指针的大小,而不是分配的内存。
您需要手动跟踪分配的数量,即通过 res->size * sizeof(*term) 或类似的东西。
首先你不想要这个:
Polynom res = malloc(sizeof(struct PolynomStruct));
res->size = 10;
您已为结构分配 space 但未初始化所需的 term
指针:
Polynom res = malloc(sizeof(struct PolynomStruct));
if(res==NULL){
//Handle allocation failure...
}
res_>term=NULL;
res->size = 0;
//Later....
free(res->term);
free(res);
为 struct
分配 space 并将数组初始化为空。
请注意,可以将 NULL
传递给 free()
,它什么都不做,returns 正常。
或者如果您确实想预分配 10 个学期:
Polynom res = malloc(sizeof(struct PolynomStruct));
if(res==NULL){
//Handle allocation failure...
}
res->size = 10;
res_>term=malloc(res->size*sizeof(double));
if(res->term==NULL){
res->size=0;
//Handle error...
}
//Later (when finished with res)...
free(res->term);
free(res);
这会将数组预分配为 10。如果预分配,您可能希望跟踪 capac
(分配了多少)和 size
(使用了多少)。但这超出了这里的范围。
要重新分配,请编写如下函数:
int reallocate(Polynom res,int newsize){
double *resized=realloc(res->term,newsize*sizeof(double));
if (resize==NULL){
//Allocation failed. The array is the same size as before.
return 1; //Or handle error your own way.
}
res->term=resized;
res->size=newsize;
//realloc may extend the space allocated in place or realloc space elsewhere.
//If it does reallocate elsewhere the current contents are just copied over
//(byte for byte) and the old space freed.
return 0;//Success. No error.
}
//Later (when finished with res)...
free(res->term);
free(res);
通常明智的做法是 res=NULL;
以避免混淆事故。
请注意,如果指针由 malloc
或 realloc
(而不是 NULL
)返回,则它必须转到 free()
(恰好一次)。
另请注意 realloc
可以减小大小,因此 newsize < res->size
没问题。
我觉得指针与数组之间可能存在一些混淆。请阅读这篇有用的文章:Are pointers and arrays equivalent in C?
你不需要 "change the size of an array inside a struct" 因为拥有结构的全部意义在于保持稳定。因此,Polynom res = malloc(sizeof(struct PolynomStruct)) 将始终在堆上为 res 分配相同数量的 memo。
如果你想构建一个双精度堆数组并指向它,你可以这样做:
int member=10; // grow array size by member=member*2 for example
double a[]=malloc(member*sizeof(double));
term=a;
这样,您就可以动态地增加数组。