Explode in C. Problem with pointer to pointer and realloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RafaelSousa
    New Member
    • Dec 2013
    • 2

    Explode in C. Problem with pointer to pointer and realloc

    Hi guys. I have this code:

    char **explode(char *delimiter, char *str, int *numberelements ){
    char *str2;
    char *straux=NULL;
    char *strinicio=NULL ;
    char **result=NULL;
    int tokens=1;

    str2=(char *)malloc(strlen (str)+1);
    strcpy(str2,str );
    *numberelements =0;
    straux=strinici o=str2;
    while(straux){
    straux=strstr(s trinicio,delimi ter);
    if (straux){
    *straux='\0';
    result=(char **)realloc(resu lt,tokens*sizeo f(char *));

    if (!result){
    MessageBoxA(NUL L,"Erro em alocar memória para explode","Out of memory",MB_OK|M B_ICONWARNING);
    exit(1);
    }

    result[tokens-1]=(char *)malloc((strle n(strinicio)+1) *sizeof(char));
    strcpy(result[tokens-1],strinicio);
    strinicio=strau x+strlen(delimi ter);
    tokens++;
    }
    else{
    result=(char **)realloc(resu lt,tokens*sizeo f(char *));

    if (!result){
    MessageBoxA(NUL L,"Erro em alocar memória para explode","Out of memory",MB_OK|M B_ICONWARNING);
    exit(1);
    }

    result[tokens-1]=(char *)malloc(strlen (strinicio)*siz eof(char));
    strcpy(result[tokens-1],strinicio);
    }
    *numberelements =*numberelement s+1;
    }
    return(result);
    }


    I wanna know if there is something wrong with this. Cause my program is closing sometimes and must to be something with this fuction... But i really don't find any error...
  • RafaelSousa
    New Member
    • Dec 2013
    • 2

    #2
    Code:
    char **explode(char *delimiter, char *str, int *numberelements){
    	char *str2;
        char *straux=NULL;
        char *strinicio=NULL;
        char **result=NULL;
        int tokens=1;
    
    	str2=(char *)malloc(strlen(str)+1);
    	strcpy(str2,str);
        *numberelements=0;
        straux=strinicio=str2;
        while(straux){
            straux=strstr(strinicio,delimiter);
            if (straux){
                *straux='\0';
                result=(char **)realloc(result,tokens*sizeof(char *));
    
    			if (!result){
    				MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
    				exit(1);
    			}
    
                result[tokens-1]=(char *)malloc((strlen(strinicio)+1)*sizeof(char));
                strcpy(result[tokens-1],strinicio);
                strinicio=straux+strlen(delimiter);
                tokens++;
            }
            else{
    			result=(char **)realloc(result,tokens*sizeof(char *));
    
    			if (!result){
    				MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
    				exit(1);
    			}
    
                result[tokens-1]=(char *)malloc(strlen(strinicio)*sizeof(char));
                strcpy(result[tokens-1],strinicio);
            }
    		*numberelements=*numberelements+1;
        }
        return(result);
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The error may be right here:

      Code:
      result[tokens-1]=(char *)malloc(strlen(strinicio)*sizeof(char));
       strcpy(result[tokens-1],strinicio);
       }etc...
      You are using strcpy so you need to allocate room for the null terminator:

      Code:
      result[tokens-1]=(char *)malloc(strlen(strinicio[B]+1[/B])*sizeof(char));
       strcpy(result[tokens-1],strinicio);
       }

      Comment

      Working...