Tizen(Headed) Native API
6.5
|
Eina_List and memory allocation
If you don't know how to create lists see Adding elements to Eina_List. In this example we also use Stringshare, however it should be possible to understand the code regardless of previous knowledge about it.
Here we have the usual list creation code with a twist, now we are using as data for the list memory that has to be freed later on.
#include <stdio.h> #include <Eina.h> int main(int argc, char **argv) { (void)argc; (void)argv; Eina_List *list = NULL; Eina_List *l; void *list_data; eina_init(); list = eina_list_append(list, eina_stringshare_add("calvin")); list = eina_list_append(list, eina_stringshare_add("Leoben")); list = eina_list_append(list, eina_stringshare_add("D'Anna")); list = eina_list_append(list, eina_stringshare_add("Simon")); list = eina_list_append(list, eina_stringshare_add("Doral")); list = eina_list_append(list, eina_stringshare_add("Six")); list = eina_list_append(list, eina_stringshare_add("Sharon"));
This time we are going to iterate over our list in a different way:
for(l = list; l; l = eina_list_next(l)) printf("%s\n", (char*)l->data);
And now we are going to iterate over the list backwards:
for(l = eina_list_last(list); l; l = eina_list_prev(l)) printf("%s\n", (char*)eina_list_data_get(l));
And now we need to free up the memory allocated during creation of the list:
EINA_LIST_FREE(list, list_data) eina_stringshare_del(list_data);
- Note:
- We don't need to use eina_list_free() since EINA_LIST_FREE takes care of that.
And shut everything down:
eina_shutdown(); return 0; }
The full source code can be found on the examples folder on the eina_list_04.c file.