C 编程:seg 错误、printf 和相关怪癖 [英] C Programming: seg faults, printf, and related quirks

查看:12
本文介绍了C 编程:seg 错误、printf 和相关怪癖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如许多年轻程序员所做的那样,我学会了在代码的不同点插入大量here1"、here2"等打印到控制台语句以找出我的程序何时出错的有用性.在我的 CS 学习过程中,这种蛮力调试技术为我节省了很多很多次.然而,当我开始用 C 编程时,我偶然发现了一个有趣的问题.如果我尝试运行

As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements of "here1," "here2," and so on at different points in code to figure out when my programs are going awry. This brute force debugging technique has saved me many, many times throughout my CS studies. However, when I started programming in C, I stumbled onto an interesting problem. If I were to try and run

void* test;

printf("hello world");
test[5] = 234;

当然,我得到一个段错误,因为我没有为 testChar 分配内存.但是,从逻辑上讲,您会认为hello world"会在段错误发生之前打印,因为这是代码的流程,但根据我的经验,总是首先发生段错误,并且hello world"" 根本不会打印到控制台.(我无法测试这个确切的例子,但我在linux机器上使用gcc多次遇到这种情况.)我猜这与编译器重新排列一些东西和/或printf有关使用某种异步刷新的缓冲区,因此不是立即的.这完全是我的猜测,因为我真的不知道为什么会发生.在我使用过的任何其他语言中,无论testChar =..."行引起什么问题,hello world"仍然会被打印出来,因此我可以确定问题出在哪里.

Of course I get a segfault for not malloc'ing memory for testChar. However, you would think logically that "hello world" would be printed before the seg fault happens, since that is the flow of the code, but in my experience, it is always the case that the seg fault happens first, and "hello world" is never printed to the console at all. (I wasn't able to test this exact example, but I have run into this sort of situation many times using gcc on a linux box.) I'm guessing this has to do with either the compiler rearranging some things and/or printf using some sort of buffer that is flushed asynchronously and therefore not being immediate. This is entirely speculation on my part because I honestly don't know why it happens. In any other language that I have used, no matter what problem the "testChar =..." line caused, the "hello world" would still be printed, and thus I could determine where the problem is.

我的问题是为什么在我编写 C 语言时会发生这种情况?为什么不先打印hello world?其次,是否有比这更好的 C 编程调试技术来完成相同的基本任务?例如,一种简单/直观的方法来查找有问题的代码行?

My question is why does this happen when I'm programming C? Why isn't the hello world printed first? And secondly, is there a better C programming debugging technique than this that accomplishes the same basic thing? As in, an easy/intuitive way to find the line of code that is a problem?

我偶然给出了一个工作示例哈哈.我现在拥有的应该会导致段错误.有趣的是,当我想要一个段错误时,我通常会得到一个,而现在当我真正想要一个时,我会编写合法代码!

I gave a working example by accident haha. What I have now should cause a segfault. It's funny how usually when I don't want a segfault I get one, and now when I actually wanted one I wrote legal code!

推荐答案

您发布的代码完全合法,不会导致段错误 - 无需 malloc 任何内容.您的问题一定出在其他地方 - 请发布导致问题的最小代码示例.

The code you posted is perfectly legal and should not cause a segfault - there is no need to malloc anything. Your problem must lie somewhere else - please post the smallest example of code that causes the problem.

您现在已将代码编辑为具有完全不同的含义.尽管如此,没有显示hello world"的原因是输出缓冲区没有被刷新.尝试添加

You have now edited the code to have a totally different meaning. Still, the reason that "hello world" is not displayed is that the the output buffer has not been flushed. Try addinig

fflush( stdout );

在 printf 之后.

after the printf.

关于定位问题的根源,您有两个选择:

Regarding locating the source of the problem you have a couple of choices:

  • 使用 __FILE____LINE__ C 宏在您的代码中自由地使用 printfs
  • 学习使用调试器 - 如果您的平台支持核心转储,您可以使用核心映像查找错误所在.
  • liberally sprinkle printfs through your code using the __FILE__ and __LINE__ C macros
  • learn to use your debugger - if your platform supports core dumps, you can use the core image to find where the error is.

这篇关于C 编程:seg 错误、printf 和相关怪癖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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