Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

list - Function pointer called with free


I am practicing some programs from a book on Algorithms using C programming. I see the following function:
void list_init(List *list, void (*destroy)(void *data)) {

/*  Initialize the list. */

list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;

return;

}

which is later called as:

list_init(&list, free);

I thought free is a function in C language that is described as: void free(void *ptr) and therefore requires an argument.

So, why is free provided as an argument to list_init by itself? Can someone help me understand this please?

Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I figured this one out. The argument to free is provided in another function when list->destroy(data) is called. This is because list->destroy is set to the address of 'free'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...