printf 期间我刚刚设置的变量出现段错误

发布于 2024-12-07 09:58:06 字数 856 浏览 0 评论 0原文

所以当我在以下情况下调用 printf 时,我会出现段错误。我只是看不出我做错了什么。有什么想法吗?谢谢一百万。我已经用注释标记了代码中出现段错误的位置(在代码的第一块中)。

...
    char* command_to_run;
    if(is_command_built_in(exec_args, command_to_run)){
        //run built in command
        printf("command_to_run = %s\n", command_to_run); // <--- this is where the problem is
        run_built_in_command(exec_args);
    }
...

int is_command_built_in(char** args, char* matched_command){
    char* built_in_commands[] = {"something", "quit", "hey"};
    int size_of_commands_arr = 3;
    int i;
    //char* command_to_execute;
    for(i = 0; i < size_of_commands_arr; i++){
        int same = strcmp(args[0], built_in_commands[i]);
        if(same == 0){
            //they were the same
            matched_command = built_in_commands[i];
            return 1;
        }
    }
    return 0;
}

So I'm seg faulting when I call printf in the following situation. I just can't see what I'm doing wrong. Any ideas? Thanks a million. I've marked the spot in the code where i get the seg fault with a comment (in the first chunk of code).

...
    char* command_to_run;
    if(is_command_built_in(exec_args, command_to_run)){
        //run built in command
        printf("command_to_run = %s\n", command_to_run); // <--- this is where the problem is
        run_built_in_command(exec_args);
    }
...

int is_command_built_in(char** args, char* matched_command){
    char* built_in_commands[] = {"something", "quit", "hey"};
    int size_of_commands_arr = 3;
    int i;
    //char* command_to_execute;
    for(i = 0; i < size_of_commands_arr; i++){
        int same = strcmp(args[0], built_in_commands[i]);
        if(same == 0){
            //they were the same
            matched_command = built_in_commands[i];
            return 1;
        }
    }
    return 0;
}

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

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

发布评论

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

评论(2

鲜肉鲜肉永远不皱 2024-12-14 09:58:06

您正在按值传递指针matched_command。因此,调用 is_command_built_in 不会改变它。所以它保留了未初始化的值。

试试这个:

char* command_to_run;
if(is_command_built_in(exec_args, &command_to_run)){   //  Changed this line.
    //run built in command
    printf("command_to_run = %s\n", command_to_run); // <--- this is where the problem is
    run_built_in_command(exec_args);
}


int is_command_built_in(char** args, char** matched_command){   //  Changed this line.
    char* built_in_commands[] = {"something", "quit", "hey"};
    int size_of_commands_arr = 3;
    int i;
    //char* command_to_execute;
    for(i = 0; i < size_of_commands_arr; i++){
        int same = strcmp(args[0], built_in_commands[i]);
        if(same == 0){
            //they were the same
            *matched_command = built_in_commands[i];   //  And changed this line.
            return 1;
        }
    }
    return 0;
}

You're passing the pointer matched_command by value. Therefore it isn't changed by the call to is_command_built_in. So it retains it's uninitialized value.

Try this:

char* command_to_run;
if(is_command_built_in(exec_args, &command_to_run)){   //  Changed this line.
    //run built in command
    printf("command_to_run = %s\n", command_to_run); // <--- this is where the problem is
    run_built_in_command(exec_args);
}


int is_command_built_in(char** args, char** matched_command){   //  Changed this line.
    char* built_in_commands[] = {"something", "quit", "hey"};
    int size_of_commands_arr = 3;
    int i;
    //char* command_to_execute;
    for(i = 0; i < size_of_commands_arr; i++){
        int same = strcmp(args[0], built_in_commands[i]);
        if(same == 0){
            //they were the same
            *matched_command = built_in_commands[i];   //  And changed this line.
            return 1;
        }
    }
    return 0;
}
給妳壹絲溫柔 2024-12-14 09:58:06

command_to_run 未初始化。对 is_command_built_in 的调用很容易崩溃。这就是未定义行为的本质。

command_to_run is uninitialized. The call to is_command_built_in could as easily have crashed. Such is the nature of undefined behavior.

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