在这个 C 编程案例中,C99 命令行不打印任何内容
今天我在使用 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 A
和 LINE 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在第二行中,您取消引用
NULL
指针,这会导致未定义的行为:您需要使
ind
指向非本地内存,即从堆中分配它malloc
,或将其设置为指向具有全局生命周期的变量:在这种情况下,释放返回的结构(如果它为 NULL,则不取消引用它)的责任委托给调用者。
请注意,如果您将 ind 设置为指向本地声明的变量并返回它,则每当调用者尝试取消引用指针时都会发生 UB,因为堆栈会在函数终止后恢复。
On the second line you are dereferencing a
NULL
pointer, which leads to undefined behaviour:You need to make
ind
point to non-local memory, i.e. allocate it from the heap withmalloc
, or set it to point to a variable with global lifetime: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.它不打印任何内容的原因是它崩溃了:
您正在取消引用 NULL。 是未定义的行为)
当您删除 LINE A 和 LINE B 时,它不会崩溃,因此它会打印 LINE C。(您还忘记了 LINE C 中的
\n
来刷新缓冲区。)(这 您需要通过 malloc 动态分配 ind 并返回。 (并确保稍后释放它)
The reason why it isn't printing anything is because it's crashing:
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)您的问题似乎出在 buildIndex 中,
如您所见,您将
ind
设置为NULL
,然后尝试立即引用。这是未定义的行为,所以接下来任何事情都可能发生。Your problem seems to be in buildIndex
As you can see, you set
ind
toNULL
and then try to reference to immediately afterwards. That's undefined behavior, so anything could happen next.您调用包含这两行的函数
buildIndex
。所以你正在取消引用空指针。这是错误的。你的程序崩溃了,所以不要再运行了。
You call the function
buildIndex
which contains these two lines.So you are dereferencing the null pointer. This is erroneous. Your program crashes, so don't run any further.