C - 双指针通过函数调用丢失

发布于 2024-09-25 12:04:29 字数 1011 浏览 2 评论 0 原文

这里是快速 C 问题。我们最近一直在玩双指针、三指针、甚至四指针。我们虽然已经掌握了一些东西,直到遇到这个问题......

char ***data;
data_generator(&data);
char **temp = data[0];          
printf("printing temp[%d]: %s\n",0, temp[0]);
printf("printing temp[%d]: %s\n",1, temp[1]);
dosomething(temp);

int dosomething(char **array) { 

    printf("printing array[%d]: %s\n",0, array[0]);
    printf("printing array[%d]: %s\n",1, array[1]);
    ......
}

int data_generator(char ****char_data) {
    char *command1[2];
    char *command2[2];

    command1[0] = "right";
    command1[1] = "left";

    command2[0] = "up";
    command2[1] = "down";

    char **commandArray[2];

    commandArray[0] = command1;
    commandArray[1] = command2;

    number_of_commands = 2;

    if(number_of_commands > 1){
    *char_data = commandArray;
    }

    return number_of_commands - 1;
}

并且打印出来......

printing temp[0]: right
printing temp[1]: left
Segmentation fault

看起来我对指针在通过函数时会发生什么有一些误解。有什么想法吗?

Quick C question here. We've been playing with double, triple, even quadruple pointers recently. We though we had a grasp on things until we ran into this problem...

char ***data;
data_generator(&data);
char **temp = data[0];          
printf("printing temp[%d]: %s\n",0, temp[0]);
printf("printing temp[%d]: %s\n",1, temp[1]);
dosomething(temp);

int dosomething(char **array) { 

    printf("printing array[%d]: %s\n",0, array[0]);
    printf("printing array[%d]: %s\n",1, array[1]);
    ......
}

int data_generator(char ****char_data) {
    char *command1[2];
    char *command2[2];

    command1[0] = "right";
    command1[1] = "left";

    command2[0] = "up";
    command2[1] = "down";

    char **commandArray[2];

    commandArray[0] = command1;
    commandArray[1] = command2;

    number_of_commands = 2;

    if(number_of_commands > 1){
    *char_data = commandArray;
    }

    return number_of_commands - 1;
}

And this prints out...

printing temp[0]: right
printing temp[1]: left
Segmentation fault

Looks like I have some misconceptions about what happens to a pointer while passed through a function. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-10-02 12:04:29
*char_data = commandArray;

您将堆栈(自动)数组的地址放入外部内存位置。这是灾难的根源(未定义的行为),因为一旦 data_generator 返回,commandArray 的生命周期就结束了。对于 commandArray 的元素也是如此,它们本身就是指向堆栈数组元素的指针。

*char_data = commandArray;

You are putting the address of a stack (automatic) array in an outside memory location. This is a recipe for disaster (undefined behavior), since commandArray's lifetime ends as soon as data_generator returns. The same is true for the elements of commandArray, which are themselves pointers to stack array elements.

郁金香雨 2024-10-02 12:04:29

更改:

char *command1[2];
char *command2[2];

至:

static char *command1[2];
static char *command2[2];

这会将 command1[] 和 command2[] 保留在保留内存中。

或者像其他发帖者所建议的那样使用 malloc() 它们,尽管正确使用 malloc'c 内存需要的考虑因素比我在这里讨论的要多。

Change:

char *command1[2];
char *command2[2];

to:

static char *command1[2];
static char *command2[2];

That will keep command1[] and command2[] in retained memory.

That, or malloc() them, as the other poster recommends, though proper use of malloc'c memory requires more considerations than I will discuss here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文