英文:
How do I free this structure in C?
问题
Here's the translated content you requested:
我在大学的项目中需要创建一个处理终端可执行文件选项的库。我创建了这个结构体 options_s:
typedef struct option_s option_t;
struct option_s {
char* keyword;
enum {
OptVoid,
OptInt,
OptString,
OptFloat
} spec;
union {
void (*opt_void)();
void (*opt_int)(int);
void (*opt_str)(const char*);
void (*opt_float)(float);
} fct;
option_t* next;
};
每个选项类型的变量将保存一个接受 int、float、字符串或无参数的函数。关键字是终端选项的标识符,例如"-o",它在传递给函数的值之前出现。
这是我如何初始化一个需要 int 参数的选项:
option_t* c_null(option_t* l, const char* kw){
l = (option_t*) malloc (sizeof(option_t));
l->keyword = (char*) malloc (strlen(kw) + 1);
strcpy(l->keyword, kw);
l->next = NULL;
return l;
}
option_t* common(option_t* l, const char* kw){
while(l->next != NULL) l = l->next;
l->next = (option_t*) malloc (sizeof(option_t));
l->next->keyword = (char*) malloc (strlen(kw) + 1);
strcpy(l->next->keyword, kw);
l->next->next = NULL;
return l->next;
}
option_t* opt_int(option_t* l, const char* kw, void (*f)(int)){
if(l == NULL){
l = c_null(l, kw);
l->spec = OptInt;
l->fct.opt_int = f;
return l;
}else{
option_t* o = common(l, kw);
o->spec = OptInt;
o->fct.opt_int = f;
return l;
}
}
我在释放选项方面遇到了问题。我写了这个函数:
void opt_delete(option_t* l){
if(l->next != NULL) opt_delete(l->next);
free(l->keyword);
free(l);
}
这似乎不起作用。即使通过此函数运行接受字符串的选项后,执行 opt->fct.opt_str("foo");
仍会打印 "foo"。
我的代码可能存在什么问题?
英文:
I have this project for college where I have to create a library that processes the options of an executable from the terminal.
I created this struct options_s :
typedef struct option_s option_t;
struct option_s {
char* keyword;
enum {
OptVoid,
OptInt,
OptString,
OptFloat
} spec;
union {
void (*opt_void)();
void (*opt_int)(int);
void (*opt_str)(const char*);
void (*opt_float)(float);
} fct;
option_t* next;
};
Each option type variable will hold a function that takes as a parameter an int, a float, a string or nothing. The keyword is the identifier of an option in the terminal and is for example "-o" and precedes the value that will be passed to the function.
This is how i initialize an option that takes and int as parameter :
option_t* c_null(option_t* l, const char* kw){
l = (option_t*) malloc (sizeof(option_t));
l->keyword = (char*) malloc (strlen(kw) + 1);
strcpy(l->keyword, kw);
l->next = NULL;
return l;
}
option_t* common(option_t* l, const char* kw){
while(l->next != NULL) l = l->next;
l->next = (option_t*) malloc (sizeof(option_t));
l->next->keyword = (char*) malloc (strlen(kw) + 1);
strcpy(l->next->keyword, kw);
l->next->next = NULL;
return l->next;
}
option_t* opt_int(option_t* l, const char* kw, void (*f)(int)){
if(l == NULL){
l = c_null(l, kw);
l->spec = OptInt;
l->fct.opt_int = f;
return l;
}else{
option_t* o = common(l, kw);
o->spec = OptInt;
o->fct.opt_int = f;
return l;
}
}
I am having problems with freeing the options. I wrote this function for that :
void opt_delete(option_t* l){
if(l->next != NULL) opt_delete(l->next);
free(l->keyword);
free(l);
}
This doesn't seem to work. Even after running an option that takes in a string through this function, doing opt->fct.opt_str("foo");
will still print "foo".
What might be the problem in my code?
答案1
得分: 2
The free()
function doesn't clean the memory, it only deallocates the memory from the process and let the space deallocated free to be reallocated by another malloc()
.
This means that if you try to access to a piece of memory deallocated, you can find the last value you have written in. However this is only lucky case, because the access to an area of memory deallocated or not allocated, is an Undefined Behaviour.
This Question can help you: How do malloc() and free() work?
英文:
The free()
function doesn't clean the memory, it only deallocates the memory from the process and let the space deallocated free to be reallocated by another malloc()
.
This means that if you try to access to a piece of memory deallocated, you can find the last value you have written in. However this is only lucky case, because the access to an area of memory deallocated or not allocated, is an Undefined Behaviour.
This Question can help you: How do malloc() and free() work?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论