McqMate
I'm working on a project where I need to store multiple strings using dynamic memory allocation. I'm using malloc to allocate an array of char pointers, then allocating each string with strdup. After processing, when I call free on the array and each string, it crashes with a segmentation fault. Here's a simplified version of my code:char **arr = malloc(3 * sizeof(char *)); arr[0] = strdup("hello"); arr[1] = strdup("world"); arr[2] = strdup("test"); // ... do something ... for (int i = 0; i < 3; i++) { free(arr[i]); } free(arr);I've compiled with -g and used gdb, but I'm not sure if I'm freeing in the right order or if there's a memory corruption issue. Any tips?
Emily Davis
1 week ago