\chapter{Eina\+\_\+\+List and memory allocation}
\hypertarget{eina_list_04_example_page}{}\label{eina_list_04_example_page}\index{Eina\_List and memory allocation@{Eina\_List and memory allocation}}


If you don\textquotesingle{}t know how to create lists see \doxysectlink{eina_list_01_example_page}{Adding elements to Eina\+\_\+\+List}{0}. In this example we also use \doxylink{group__Eina__Stringshare__Group}{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. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}\ **argv)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ (void)argc;}
\DoxyCodeLine{\ \ \ (void)argv;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *list\ =\ NULL;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *l;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{void}\ *list\_data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}calvin"{}}));}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}Leoben"{}}));}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}D'Anna"{}}));}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}Simon"{}}));}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}Doral"{}}));}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}Six"{}}));}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\textcolor{stringliteral}{"{}Sharon"{}}));}

\end{DoxyCodeInclude}
 This time we are going to iterate over our list in a different way\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}(l\ =\ list;\ l;\ l\ =\ eina\_list\_next(l))}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ (\textcolor{keywordtype}{char}*)l-\/>\mbox{\hyperlink{struct__Eina__List_a043e22d601d3a0504f30255a43a25e7a}{data}});}

\end{DoxyCodeInclude}
 And now we are going to iterate over the list backwards\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}(l\ =\ eina\_list\_last(list);\ l;\ l\ =\ eina\_list\_prev(l))}
\DoxyCodeLine{\ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ (\textcolor{keywordtype}{char}*)eina\_list\_data\_get(l));}

\end{DoxyCodeInclude}
 And now we need to free up the memory allocated during creation of the list\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gae27a2c686a0a11693f9b06957c9bba83}{EINA\_LIST\_FREE}}(list,\ list\_data)}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(list\_data);}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
We don\textquotesingle{}t need to use \doxylink{group__Eina__List__Group_ga82a1bfa543e21853f9ea3ff88dd1250b}{eina\+\_\+list\+\_\+free()} since \doxylink{group__Eina__List__Group_gae27a2c686a0a11693f9b06957c9bba83}{EINA\+\_\+\+LIST\+\_\+\+FREE} takes care of that.
\end{DoxyNote}
And shut everything down\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The full source code can be found on the examples folder on the \doxysectlink{eina_list_04_c}{eina\+\_\+list\+\_\+04.c}{0} file. 