在Windows的C code崩溃,但无法在Linux中 [英] C code crashes in Windows, but not in Linux

查看:169
本文介绍了在Windows的C code崩溃,但无法在Linux中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,C,和我已经测试了我的计划中的Fedora,使用gcc和gdb调试。我有一个程序,它从用户的输入。如果输入的第一个字符串是创造然后,我就来看看第二个命令,如果是这样的对象,然后我继续CreateObject函数。

I'm a newbie to C, and I have been testing my program out in Fedora, using gcc and gdb to debug. I have a program that takes input from the user. If the first string entered is "create" then I take a look at the second command, and if that's "object" then I proceed to the createObject function.

希望我的code将使这一更清楚一点:

Hopefully my code will make this a bit clearer:

static void parseCmd(char **input) {
    if(!strcmp(input[0], "create")) {
        if(!strcmp(input[1], "object")) {
            if(input[2] && strcmp(input[2], ""))
                createObject(input[2]);
            else
                printf("Object needs a name\n");
        }
        else
            printf("Command needs more parameters\n");
    }
    else
        printf("Command not recognized\n");
}

当我测试只输入创建对象(对象之后没有空间,就回车键)

When I test entering just "create object" (no space after object, just the ENTER key)

在Linux中打印对象需要一个名字

In Linux it prints "Object needs a name"

但在Windows程序崩溃,它只是挂起。我怎么可以改变code,使其行为相同的方式,因为它在Linux下呢?

But in windows the program crashes, it just hangs. How could I change the code to make it behave the same way as it does in Linux?

推荐答案

您已经进入到潜在的访问冲突或未定义行为的领域(当然,未定义值):

You've entered into the realm of potential access violations or undefined behavior (well, undefined values):

input[2] && strcmp(input[2], "")

如果输入的长度只有2,那么这是阅读过去的可接受点阅读。最坏的情况下,这将导致一个段错误(如Windows那样),而最好的情况下,操作系统会让它发生,你会在其中获得一个随机值。 (Linux的似乎是治疗要么或*输入[2]如0虽然。)

If the length of input is only 2, then this is reading past the acceptable point to read. Worst case this will cause a segfault (as Windows does), and best case the OS will let it happen and you'll get a random value in it. (Linux appears to be treating either it or *input[2] as 0 though.)

总之,而不是读的内容你是不是允许访问,通过在输入的长度和检查来代替。

Anyway, rather than reading contents you are not allowed to access, pass in the length of the input and check that instead.

static void parseCmd( char **input, size_t num) { //or just int if size_t isn't already defined
    //compare num
}

- 编辑 -

--Edit--

正如丹尼尔·菲舍尔指出,显然 ARGV [ARGC] 确实是一个有效的读取,并且保证是一个空指针。

As pointed out by Daniel Fischer, apparently argv[argc] is indeed a valid read, and it is guaranteed to be a null pointer.

假设你确实传递的argv 来的功能,这意味着你可以依靠这种行为。您的其他两个if语句不检查这虽然,和推广功能的缘故,它仍然是最好沿长度方向传递(或者,如paulsm4说,你应该看看getopt函数的 - 它使解析参数太多比滚动自己的解析方法)更容易。

Assuming that you are indeed passing argv to the function, this means that you could rely on this behavior. Your other two if statements do not check for this though, and for the sake of generalizing the function, it's still better to pass along the length (or, as paulsm4 said, you should look into the getopt function -- it makes parses parameters much easier than rolling your own parsing method).

这篇关于在Windows的C code崩溃,但无法在Linux中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆