Jimmy
Jimmy

Reputation: 45

Segment fault ( core dumped ) from malloc() but I can't see where the problem is coming from

I'm new to C, coming from another language and I'm getting this error that I understand now but couldn't find how to fix it inside of my code.

It's pretty simple but even when debugging with printf or perror I couldn't figure it out because it returns Success on the memory allocation.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> 

char *tasks[6];

// Made the size 5 to allow for `\0`
tasks[0] = (char *) malloc(5 * sizeof(char));
tasks[1] = (char *) malloc(5 * sizeof(char));
tasks[2] = (char *) malloc(5 * sizeof(char));
tasks[3] = (char *) malloc(5 * sizeof(char));
tasks[4] = (char *) malloc(5 * sizeof(char));
tasks[5] = (char *) malloc(5 * sizeof(char));

for (int i = 0; i < sizeof(tasks); i++) {
    if (!tasks[i]) {
        perror("malloc result : "); // Returns Success
    }
}

strncpy(tasks[0], "Dish", 5);
strncpy(tasks[1], "Laun", 5);
strncpy(tasks[2], "Bath", 5);
strncpy(tasks[3], "Flor", 5);
strncpy(tasks[4], "Tras", 5);
strncpy(tasks[5], "Offi", 5);

for (int i = 0; i < sizeof(tasks); i++) {
    printf("%s\n", tasks[i]);
}

for (int i = 0; i < sizeof(tasks); i++) {
    free(tasks[i]);
}

Upvotes: 0

Views: 100

Answers (1)

Asile34
Asile34

Reputation: 457

Is because you don't take the good size of your array:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> 


int main(void){

    char *tasks[6] = {NULL};
    
    // Made the size 5 to allow for `\0`
    tasks[0] = (char *) malloc(5 * sizeof(char));
    tasks[1] = (char *) malloc(5 * sizeof(char));
    tasks[2] = (char *) malloc(5 * sizeof(char));
    tasks[3] = (char *) malloc(5 * sizeof(char));
    tasks[4] = (char *) malloc(5 * sizeof(char));
    tasks[5] = (char *) malloc(5 * sizeof(char));
    
    for (int i = 0; i < sizeof(tasks) / sizeof(tasks[0]); i++) {
        if (!tasks[i]) {
            perror("malloc result : "); // Returns Success
        }
    }
    
    strncpy(tasks[0], "Dish", 5);
    strncpy(tasks[1], "Laun", 5);
    strncpy(tasks[2], "Bath", 5);
    strncpy(tasks[3], "Flor", 5);
    strncpy(tasks[4], "Tras", 5);
    strncpy(tasks[5], "Offi", 5);
    
    for (int i = 0; i < sizeof(tasks) / sizeof(tasks[0]); i++) {
        printf("%s\n", tasks[i]);
    }
    
    for (int i = 0; i < sizeof(tasks) / sizeof(tasks[0]); i++) {
        free(tasks[i]);
    }

}

Give the right result without any memory error:

==1026== Memcheck, a memory error detector
==1026== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1026== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==1026== Command: ./a.out
==1026==
Dish
Laun
Bath
Flor
Tras
Offi
==1026==
==1026== HEAP SUMMARY:
==1026==     in use at exit: 0 bytes in 0 blocks
==1026==   total heap usage: 7 allocs, 7 frees, 1,054 bytes allocated
==1026==
==1026== All heap blocks were freed -- no leaks are possible
==1026==
==1026== For lists of detected and suppressed errors, rerun with: -s
==1026== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Upvotes: 3

Related Questions