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

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

问题描述

许多年轻的程序员,我学到了在代码中的不同点插入here1,here2等许多打印到控制台语句的有用性,以弄清我的程序何时出现。这种强力调试技术在我的CS研究中节省了很多次。但是,当我开始在C编程时,我偶然发现了一个有趣的问题。如果我尝试运行

  void * test; 

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

当然,我得到一个segfault,不会malloc内存为testChar。然而,你会认为逻辑上会在seg故障发生之前打印hello world,因为这是代码的流程,但是在我的经验中,始终是seg故障首先发生,hello world 根本没有打印到控制台。 (我没有能够测试这个确切的例子,但是我已经遇到这种情况使用gcc在linux框上。)我猜这是与编译器重新排列一些东西和/或printf使用某种缓冲区异步刷新,因此不立即。这完全是我的猜测,因为我真的不知道为什么会这样。使用任何其他语言,无论testChar = ...行出现什么问题,hello world仍然会打印出来,因此我可以确定问题的位置。



我的问题是为什么在编程C时会发生这种情况?为什么不打好你的世界?其次,是否有更好的C编程调试技术,而不是完成同样的基本事情呢?像在一个简单/直观的方式找到一个问题的代码行?



编辑:我偶然发了一个工作的例子哈哈。我现在应该造成一个segfault。当我想要一个segfault我得到一个,现在当我真的想要一个我写法律代码的时候,通常是多么平常!

解决方案

您发布的代码是完全合法的,不应该导致segfault - 不需要malloc任何东西。您的问题必须位于其他地方 - 请发布导致问题的最小代码示例。



编辑:您现在已将代码编辑为有完全不同的意思。不过,没有显示hello world的原因是输出缓冲区还没有被刷新。尝试addinig

  fflush(stdout); 

after printf。



问题的根源有几个选择:




  • 通过代码使用 __ FILE __ __ LINE __ 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;

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.

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?

Edit: 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!

解决方案

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.

Edit: 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 );

after the printf.

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

  • 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 fault,printf和相关怪癖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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