通过C中的函数更改动态2D字符数组?

通过C中的函数更改动态2D字符数组?,c,arrays,function,dynamic,segmentation-fault,C,Arrays,Function,Dynamic,Segmentation Fault,我创建这个示例运行是为了更好地理解如何通过其他函数编辑动态数组,但在添加了辅助函数后,我开始遇到segfaults #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", "sha

我创建这个示例运行是为了更好地理解如何通过其他函数编辑动态数组,但在添加了辅助函数后,我开始遇到segfaults

#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");
}
#包括
#包括
#包括
另一侧无效(字符***funk);
int main()
{
int i;
char*argv[11]={“鱼”、“菜”、“拉格”、“fags”、“shag”、“酷”、“豆”、“rekt”、“中午”、“浣熊”、“lolz”};
字符**是,**否;
yep=malloc(10*sizeof*yep);

如果(!yep){/你关于运算符的优先顺序很重要,你错过了一对参数来确保它正确执行。这:
**(funk+i)
意味着:
*(funk[i])
,而不是你想要的
(*funk)[i]

这:

应该是这样的:

*((*funk)+i) = strdup(arr[i]);
printf("%s is currently in yep.\n", *((*funk)+i));
坦率地说,它更容易理解为:

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

我将内存管理的其余部分留给您来修复(即,您在上面的循环代码中覆盖的所有指针指向的动态内存泄漏).

我建议阅读以更好地了解如何调试它。
nop==NULL
?为什么不呢?
!nop
?很高兴听到您一直在使用Valgrind,但您也应该使用调试器来确定seg故障是如何产生的。
nop=realloc(是的,11*sizeof*yep)
:很好,您可以保留旧值,直到不再需要它为止。但是为什么分配失败时释放
nop
NULL
)呢?尽管如此,为什么要检查
realloc
-失败而不是
malloc
-失败?有没有理由在这里省略
strdup
*(yep+10)=argv[10];
)?无法释放字符串文字…对此的预言性注释:
**(funk+i)=arr[i]
您扔掉分配的字符串,而代之以分配字符串文字。这些是不可释放的…我没有想到使用!不,我想这会使事情更干净。谢谢您指出这一点!我想检查malloc失败也会有所帮助,但意外地在另一端函数中忽略了strdupn可能是问题的一部分!我会很快修复其中的两个,看看它们是否有影响,并保持yall的更新。你会推荐什么调试器?到目前为止,我还没有使用任何特定的程序,我只是使用gedit、gcc和valgrind。非常感谢!我在sta之前添加了一个免费声明tement以及指针修复,并且不再有内存泄漏。
(*funk)[i] = strdup(arr[i]);
printf("%s is currently in yep.\n", (*funk)[i]);