سؤال

I'm creating this sample run so I can better understand how I can edit dynamic arrays through other functions, but I started running into segfaults once I added the secondary function.

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

void other_side(char ***funk);

int main()
{
    int i;
    char *argv[11] = {"fish", "dish", "lags", "fags", "shag", "cool", "bean", "rekt", "noon", "coon", "lolz"};

    char **yep, **nop;
    yep = malloc(10 * sizeof *yep);
    if(!yep) { // <-----------------added check for malloc error
        printf("Error: failure to allocate memory\n");
        exit(1);
    }

    printf("10 times %lu\n\n", sizeof *yep);
    for(i = 0; i<10; i++) {
        yep[i] = strdup(argv[i]);
        printf("%s is in yep.\n", *(yep+i)); 
    }
    nop = realloc(yep, 11 * sizeof *yep); //you reallocate to the new total size.
    if(nop == NULL) {
        printf("Error: failure to allocate memory\n")
        exit(1);
    }
    yep = nop;
    *(yep+10) = strdup(argv[10]); 
    printf("Last but certainly not least, %s is in yep.\n", *(yep+10));

    printf("Now to send yep over to the other side and have its values changed.\n");

    other_side(&yep);

    printf("Did it change?\n\n");

    for(i=0; i<11; i++)
        printf("%s is in yep.\n", *(yep+i));

    for(i=0; i<11; i++) { //issue fixed when added strdup() above, previously static
        free(*(yep+i)); 
    }
    free(yep);
    return 0;
}

void other_side(char ***funk)
{
    char *arr[11] = {"dude","yeah","gnar","nice","epic","need","more", "word","four","this","test"};
    int i;
    for(i=0; i<11; i++) {
        **(funk+i) = strdup(arr[i]); //added strdup() here as well
        printf("%s is currently in yep.\n", **(funk+i));
    }
    printf("\n");
}

A couple things I noticed with this is that Valgrind notices an unnecessary free when I try to free the 11th block of memory to my array in main(). I'm not sure if that's my issue, but I also noticed that the function will only change two words before it leads to a segmentation fault.

Edit Notes: Since the edit I still get segfaults, but valgrind has been a bit more clear with what is happening. (Bad permissions for mapped region at address 0x400B18)

هل كانت مفيدة؟

المحلول

Your order of precedence regarding operators is important, and you missed a pair of parens to ensure it is done correctly. This: **(funk+i) means this: *(funk[i]), not (*funk)[i] which is what you want.

This:

**(funk+i) = strdup(arr[i]);
printf("%s is currently in yep.\n", **(funk+i));

Should be this:

*((*funk)+i) = strdup(arr[i]);
printf("%s is currently in yep.\n", *((*funk)+i));

and frankly, it is considerably easier to read as:

(*funk)[i] = strdup(arr[i]);
printf("%s is currently in yep.\n", (*funk)[i]);

I leave the rest of the memory management to you to fix. (i.e. the leaks from the dynamic memory pointed to by all the pointers you're overwriting in the above loop code).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top