C 如何取消分配数据结构
我已经使用dataset_creator函数分配了以下结构,我想释放这个结构(dataset,i maen)及其所有组件。 你能帮我吗C 如何取消分配数据结构,c,C,我已经使用dataset_creator函数分配了以下结构,我想释放这个结构(dataset,i maen)及其所有组件。 你能帮我吗 struct dataset{ char *title; struct linked_data *head; struct dataset *next; struct attribute **hash_table; }; struct dataset* dataset_creator(char *name,struct attribute *e
struct dataset{
char *title;
struct linked_data *head;
struct dataset *next;
struct attribute **hash_table;
};
struct dataset* dataset_creator(char *name,struct attribute *expressions,struct query *query){
struct dataset *ds; //"ds" is the abbreviation of word "dataset"
//struct attribute **hash_table;
//struct linked_data *data;
struct attribute *current_attribute;
//int i;
int j;
int index;
ds=(struct dataset*)malloc(sizeof(struct dataset));
if(ds==NULL){
printf("failed memory allocation request\n");
exit(EXIT_FAILURE);
}
ds->next=NULL;
ds->title=(char*)calloc((strlen(name)+1),sizeof(char));
strcpy(ds->title,name);
ds->head=(linked_data*)malloc(sizeof(struct linked_data));
if(ds->head==NULL){
printf("failed memory allocation request\n");
exit(EXIT_FAILURE);
}
ds->head->next=NULL;
ds->hash_table=(attribute**)malloc(MAX_NUM_OF_ATTRS * sizeof(attribute*));
if(ds->hash_table==NULL){
printf("failed memory allocation request\n");
exit(EXIT_FAILURE);
}
for(j=0;j<MAX_NUM_OF_ATTRS;j++)
ds->hash_table[j]=NULL;
for(j=0;j<MAX_NUM_OF_ATTRS;j++){
index=hash(expressions[j].name,MAX_NUM_OF_ATTRS);
if(ds->hash_table[j]==NULL){
ds->hash_table[index]=(struct attribute*)malloc(sizeof(struct attribute));
ds->hash_table[index]->next=NULL;
}
else{
current_attribute=ds->hash_table[index]->next;
while(current_attribute->next != NULL){
current_attribute=current_attribute->next;
}
current_attribute->next=(struct attribute*)malloc(sizeof(struct attribute));
current_attribute->next->next=NULL;
}
}
return ds;
}
结构数据集{
字符*标题;
结构链接的_数据*头;
结构数据集*下一步;
结构属性**哈希表;
};
结构数据集*数据集\创建者(字符*名称、结构属性*表达式、结构查询*查询){
struct dataset*ds;//“ds”是单词“dataset”的缩写
//结构属性**哈希表;
//结构链接的_数据*数据;
结构属性*当前_属性;
//int i;
int j;
整数指数;
ds=(结构数据集*)malloc(sizeof(结构数据集));
如果(ds==NULL){
printf(“内存分配请求失败\n”);
退出(退出失败);
}
ds->next=NULL;
ds->title=(char*)calloc((strlen(name)+1),sizeof(char));
strcpy(ds->标题、名称);
ds->head=(链接的_数据*)malloc(sizeof(结构链接的_数据));
如果(ds->head==NULL){
printf(“内存分配请求失败\n”);
退出(退出失败);
}
ds->head->next=NULL;
ds->hash_table=(属性**)malloc(属性*大小的最大值);
如果(ds->hash_table==NULL){
printf(“内存分配请求失败\n”);
退出(退出失败);
}
对于(j=0;jhash_表[j]=NULL;
对于(j=0;jhash_表[j]==NULL){
ds->hash_表[索引]=(结构属性*)malloc(sizeof(结构属性));
ds->hash_table[index]->next=NULL;
}
否则{
当前\u属性=ds->哈希\u表[索引]->下一步;
while(当前属性->下一步!=NULL){
当前属性=当前属性->下一步;
}
当前_属性->下一步=(结构属性*)malloc(sizeof(结构属性));
当前属性->下一步->下一步=空;
}
}
返回ds;
}
为语法错误感到抱歉;
注意:这不是全部代码;请查看顺序:
1. free(ds->hash_table[index]); /* Free all the single pointers memory first in a loop*/
2. free(ds->hash_table);
3. free(ds->head);
4. free(ds->title):
5. free(ds);
您的结构有另一个分配current\u属性
,因此也可以释放内存
free(current_attribute->next);
对于数据结构中的每个指针,您必须使用
free
,也必须对结构本身使用。但请确保您正在释放的指针没有在其他地方使用。此外,如果您有嵌套指针(例如struct attribute**hash_table
)请您注意指针内的指针。标准警告:请返回<代码> MalCube()/<代码> >代码>(或代码>)。每个代码< >代码> >代码> >或>代码> CALLUNE()/代码>。请考虑此样式<代码> DS-> HasyTabe= Malc(Max NothOfth-Atths*sieOff**(DS-> Hash表));更干净,更易于维护,对错误代码的更改更少。