Uninitialised value was created by a stack allocation valgrind C

c

Solution

The problem is here:

char delimitadorInterno[] = {delimitador,'\n'};

// ...

char * token = strtok(string,delimitador);

The second argument to `strtok` is supposed to point to a string which is the separator to look for . However you passed a pointer to two characters which do not form a string, thereby causing undefined behaviour which valgrind catches when `strtok` reads past the '\n'.

To fix this, change to:

char delimitadorInterno[] = { delimitador,'\n', 0 };

or perhaps

char delimitadorInterno[] = { delimitador, 0 };

I guess you are intending to use just the delimiter character as separator (as opposed to the sequence of the delimiter followed by a newline). Since the result of `fgets` can only have a newline at the end, then the version with `\n` will never match anything unless the line ends in delimiter , in which case it will match once.

Problem

``` setlocale(LC_ALL,"pt_PT.UTF-8"); FILE *vocabulario = fopen(*(++argv),"r"); FILE *original = fopen(*(++argv),"r"); FILE *convertido = fopen(*(++argv),"w"); if (vocabulario == NULL || original == NULL || convertido == NULL){ printf("Não foi possível abrir um dos ficheiros"); return 1; } char delimitador = '\t'; TNode * root = NULL; Conversor * temp = NULL; char linhaLida[BUFFER]; char linhaTemp[BUFFER]; char linhaEscrever[BUFFER]; int i = 0; while(fgets(linhaLida,BUFFER,vocabulario) != NULL){ if ( !criarConversor(&temp,linhaLida,delimitador) ) continue; insertNodeViciado(&root,temp); i++; } /* criarConversor implementation*/ 20 int criarConversor(Conversor ** temp,char * stringAUsar,char delimitador){ 21 int contador = obterNumeroDelimitadores(stringAUsar,delimitador); 22 char delimitadorInterno[] = {delimitador,'\n'}; 23 if (contador == 0) return 0; 24 *temp = malloc(sizeof(Conversor)); 25 verificarAlocacao(*temp); 26 (*temp)->original = obterStringAlocada(stringAUsar,delimitadorInterno); 27 if (contador == 1){ 28 (*temp)->preferencia = obterStringAlocada(NULL,delimitadorInterno); 29 (*temp)->opcoes = NULL; 30 } else { 31 (*temp)->preferencia = NULL; 32 (*temp)->opcoes = malloc((contador+1)*sizeof(char *)); 33 verificarAlocacao((*temp)->opcoes); 34 int i = 0; 35 while(i<contador) *(((*temp)->opcoes)+i++) = obterStringAlocada(NULL,delimitadorInterno); 36 *(((*temp)->opcoes)+i) = NULL; } 37 return 1; typedef struct conversor{ char * original; char * preferencia; char ** opcoes; } Conversor; Conditional jump or move depends on uninitialised value(s) ==2306== at 0x40AB01E: strtok (strtok.S:165) ==2306== by 0x8048E1E: criarConversor (Conversor.c:35) ==2306== by 0x8048984: main (main.c:24) ==2306== Uninitialised value was created by a stack allocation ==2306== at 0x8048D18: criarConversor (Conversor.c:20) /*Gets the token,checks to see if there was a token,allocates on the heap and returns the pointer to the heap allocated string*/ char * obterStringAlocada(char * string,char * delimitador){ char * token = strtok(string,delimitador); verificarAlocacao(token); char * alocada = strcpy(malloc(strlen(token)+1),token); return alocada; } /*Counts how many delimiters there is on the string*/ int obterNumeroDelimitadores(char * string,char delimitador){ int contador = 0; int i = 0; while (*(string+i) != '\0') if (*(string+i++) == delimitador)contador++; return contador; } /* checks if the memory was properly allocated,which why its always called after malloc*/ void verificarAlocacao(void * verificar){ if (verificar == NULL){ printf("Não foi possível alocar a memória necessária\nO programa vai encerrar"); exit(1); } } ``` Valgrind gives me the Uninitialised value was created by a stack allocation in the implementation of criarConversor(). My question is: how can the values be uninitialized? delimitador is a set variable as are the others two. I could understand if someone said linhaLida isn't initialized except the code will never reach the if(!criarConversor()) unless linhaLida is written by the fgets function.

Original source