List is initialised this way:
Expand|Select|Wrap|Line Numbers
- char **Istate=NULL;
- Istate=malloc(sizeof (char *));
- Istate[0]=NULL;
Expand|Select|Wrap|Line Numbers
- nIstate=appendList(&Istate,buff);
Expand|Select|Wrap|Line Numbers
- int appendList(char ***lst,char *str){
- int i;
- char **auxlst=NULL;
- for(i=0;*lst[i]!=NULL;i++);
- auxlst=realloc(*lst, (i+2)*sizeof (char *));
- if(auxlst==NULL){
- fprintf(stderr,"La cagaste\n");
- free(lst);
- return -1;
- }
- *lst=auxlst;
- *lst[i]=malloc(strlen(str));
- strcpy(*lst[i],str);
- *lst[i+1]=NULL;
- return i;
- }
Testing with gdb i've seen that strcopy works fine.
First time I call the function, for loop finishes with i=0, so realloc gives space for two (char *).
strlen(str) bytes are reserved for the first one, and the second one is supposed to point NULL, but then is when the program fails...
Can anybody help me?. Thank you