1
I am having a segmentation fault error when I have imploded a dynamical array of pointers to be able to store the contents of a row-by-row file.
size_t file_mem_init(char ***ptr,FILE * file){
size_t mem_size = getnline(file); // retorna a quantidades de linha no arquivo
size_t x;
if(mem_size == 0){
puts("Arquivo vazio");
exit(1);
}
*ptr = malloc(sizeof(**ptr)*mem_size);
if(*ptr == NULL){
perror("malloc in *ptr");
exit(-1);
}
for(x = 0; x < mem_size && !feof(file);x++){
// DEBUG
printf("%zu",x);
// sizeofline(file) retorna a quantidade de caracteres de uma linha no arquivo.
*ptr[x] = malloc(sizeof(***ptr) * sizeofline(file)); <---- Falha de Segmentação
if(*ptr[x]==NULL){
printf("Na variavel *ptr[%zu]: %s",x,strerror(errno));
exit(-1);
}
x++;
}
return mem_size;
}
void file_mem_free(char ***ptr,size_t len){
while(len > 0)
free(*ptr[--len]);
free(*ptr);
}
On which line is the error? Have you tried running in a debugger?
– Vinícius Gobbo A. de Oliveira
Just so I understand the idea, it has how to explain the logic you used to write this line:
*ptr = malloc(sizeof(**ptr)*mem_size);
?– Bacco
According to gdb the segmentation failure is in malloc(). sizeofline() returns at least 1.
– tunixbsd