在这个 C 编程案例中,C99 命令行不打印任何内容

发布于 2024-12-11 21:37:33 字数 866 浏览 0 评论 0原文

今天我在使用 C 时遇到了一个奇怪的问题。快速浏览一下这个简化的代码片段:

typedef struct
{
    /* The number of index terms */
    int nTerms;
    /* Information about each index term */
    TERMINFO *terms;
} INDEX;

INDEX *buildIndex(char *termsfile, char *dirs[], int n, OPTIONS opts)
{
    INDEX *ind = NULL;
    ind->nTerms = 5;
    return ind;
}

int main(int argc, char *argv[]) {
    ... // declare and assign values for TERMFILE, DIRS and opts.
    INDEX *ind = buildIndex(TERMFILE, DIRS, sizeof(DIRS), opts); // LINE A 
    printf("Does NOT print %d\n",ind->nTerms); // LINE B
    printf("Does NOT print as well"); // LINE C
    return 0;
}

当我编译这个程序时,没有发生错误,但是当我运行编译的文件时,它不会在命令行中打印任何内容(我在 Windows 机器上使用 PuTTy) )。当我删除行 LINE ALINE B 时,它变得更加奇怪,然后可以打印 LINE C。

简而言之,无论 A 行之后的内容都无法打印出来(或执行?)。

不知道我的代码有没有问题。

I'm experiencing a weird problem with C today. Have a quick look at this simplified code snippet:

typedef struct
{
    /* The number of index terms */
    int nTerms;
    /* Information about each index term */
    TERMINFO *terms;
} INDEX;

INDEX *buildIndex(char *termsfile, char *dirs[], int n, OPTIONS opts)
{
    INDEX *ind = NULL;
    ind->nTerms = 5;
    return ind;
}

int main(int argc, char *argv[]) {
    ... // declare and assign values for TERMFILE, DIRS and opts.
    INDEX *ind = buildIndex(TERMFILE, DIRS, sizeof(DIRS), opts); // LINE A 
    printf("Does NOT print %d\n",ind->nTerms); // LINE B
    printf("Does NOT print as well"); // LINE C
    return 0;
}

When I compile this program, there is no errors occurred, however when I run the compiled file, it doesn't print anything to the commmand-line (I'm using PuTTy on Windows machine). It becomes even weird when I remove the line LINE A and LINE B, then LINE C can be printed.

In short, whatever goes after LINE A can't be printed out (or executed?).

I don't know if there is any problem with my code.

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

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

发布评论

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

评论(4

寒江雪… 2024-12-18 21:37:33

在第二行中,您取消引用 NULL 指针,这会导致未定义的行为:

INDEX *ind = NULL;
ind->nTerms = 5;
return ind;

您需要使 ind 指向非本地内存,即从堆中分配它malloc,或将其设置为指向具有全局生命周期的变量:

INDEX *ind = malloc(sizeof(INDEX));

if (ind != NULL)
    ind->nTerms = 5;

return ind;

在这种情况下,释放返回的结构(如果它为 NULL,则不取消引用它)的责任委托给调用者。

请注意,如果您将 ind 设置为指向本地声明的变量并返回它,则每当调用者尝试取消引用指针时都会发生 UB,因为堆栈会在函数终止后恢复。

On the second line you are dereferencing a NULL pointer, which leads to undefined behaviour:

INDEX *ind = NULL;
ind->nTerms = 5;
return ind;

You need to make ind point to non-local memory, i.e. allocate it from the heap with malloc, or set it to point to a variable with global lifetime:

INDEX *ind = malloc(sizeof(INDEX));

if (ind != NULL)
    ind->nTerms = 5;

return ind;

The responsibility for freeing the returned struct (and not dereferencing it if it's NULL) is delegated to the caller in this case.

Note that if you were to set ind to point to a locally-declared variable and return it, UB will occur whenever the caller attempts to dereference the pointer, because the stack is restored after the function terminates.

爱,才寂寞 2024-12-18 21:37:33

它不打印任何内容的原因是它崩溃了:

INDEX *ind = NULL;
ind->nTerms = 5;

您正在取消引用 NULL。 是未定义的行为)

当您删除 LINE A 和 LINE B 时,它不会崩溃,因此它会打印 LINE C。(您还忘记了 LINE C 中的 \n 来刷新缓冲区。)

(这 您需要通过 malloc 动态分配 ind 并返回。 (并确保稍后释放它)

INDEX *buildIndex(char *termsfile, char *dirs[], int n, OPTIONS opts)
{
    INDEX *ind = malloc(sizeof(INDEX));   //  Allocate

    //  You may wish to check if `ind == NULL` to see if the allocation failed.

    ind->nTerms = 5;
    return ind;
}

int main(int argc, char *argv[]) {
    ... // declare and assign values for TERMFILE, DIRS and opts.
    INDEX *ind = buildIndex(TERMFILE, DIRS, sizeof(DIRS), opts); // LINE A 
    printf("Does NOT print %d\n",ind->nTerms); // LINE B
    printf("Does NOT print as well"); // LINE C

    free(ind);  //  Free

    return 0;
}

The reason why it isn't printing anything is because it's crashing:

INDEX *ind = NULL;
ind->nTerms = 5;

You're dereferencing a NULL. (which is undefined behavior)

When you remove LINE A and LINE B, it doesn't crash so it prints LINE C. (you also forgot the \n in LINE C to flush the buffer.)

What you need to do it dynamically allocate ind via malloc and return. (and be sure to free it later)

INDEX *buildIndex(char *termsfile, char *dirs[], int n, OPTIONS opts)
{
    INDEX *ind = malloc(sizeof(INDEX));   //  Allocate

    //  You may wish to check if `ind == NULL` to see if the allocation failed.

    ind->nTerms = 5;
    return ind;
}

int main(int argc, char *argv[]) {
    ... // declare and assign values for TERMFILE, DIRS and opts.
    INDEX *ind = buildIndex(TERMFILE, DIRS, sizeof(DIRS), opts); // LINE A 
    printf("Does NOT print %d\n",ind->nTerms); // LINE B
    printf("Does NOT print as well"); // LINE C

    free(ind);  //  Free

    return 0;
}
凡间太子 2024-12-18 21:37:33

您的问题似乎出在 buildIndex 中,

INDEX *buildIndex(char *termsfile, char *dirs[], int n, OPTIONS opts) 
{
    INDEX *ind = NULL;
    ind->nTerms = 5;
    return ind; 
}

如您所见,您将 ind 设置为 NULL,然后尝试立即引用。这是未定义的行为,所以接下来任何事情都可能发生。

Your problem seems to be in buildIndex

INDEX *buildIndex(char *termsfile, char *dirs[], int n, OPTIONS opts) 
{
    INDEX *ind = NULL;
    ind->nTerms = 5;
    return ind; 
}

As you can see, you set ind to NULL and then try to reference to immediately afterwards. That's undefined behavior, so anything could happen next.

圈圈圆圆圈圈 2024-12-18 21:37:33

您调用包含这两行的函数buildIndex

索引 *ind = NULL;
ind->nTerms = 5;

所以你正在取消引用空指针。这是错误的。你的程序崩溃了,所以不要再运行了。

You call the function buildIndex which contains these two lines.

INDEX *ind = NULL;
ind->nTerms = 5;

So you are dereferencing the null pointer. This is erroneous. Your program crashes, so don't run any further.

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