它是安全与输出和错误禁用缓冲? [英] Is it safe to disable buffering with stdout and stderr?

查看:163
本文介绍了它是安全与输出和错误禁用缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我们把我们的$ C $一些调试打印C此方式

 的printf(在调试点1 \\ n成功杀入);一些code是这里的printf(成功在调试点达到2);这里segmantaion故障的发生是由于某些原因

现在在这种情况下唯一的调试点1将在标准输入输出调试点打印2打印写入到标准输入缓冲区,但它不是刷新,因为它没有得到 \\ n 等等我们认为,暴跌之后发生的调试点1

过度来源于此,如果我禁用与标准输入输出和标准错误流像这样的缓冲选项

  setvbuf用来(标准输出,NULL,_IONBF,0);
setvbuf用来(标准错误,NULL,_IONBF,0);

然后就是这个安全做到这一点?

为什么所有数据流都默认行缓冲?

编辑:

Usualy是什么这样的任何文件流的大小在默认情况下分配的缓冲区?我认为它的OS依赖。我想了解Linux呢?


解决方案

  

为什么所有的流被缓冲的默认行


他们正在缓冲性能的原因。图书馆力图避免使系统调用,因为它需要长。而且不是所有的人都在默认情况下缓冲。例如标准错误一般的缓冲和标准输出是行缓冲,只有当它是指一个tty。


  

然后就是这个安全做到这一点?


这是安全的禁用缓冲,但我必须说,这是不是最好的调试技术。

Sometimes we put some debug prints in our code this way

printf("successfully reached at debug-point 1\n"); 

some code is here

printf("successfully reached at debug-point 2"); 

Here segmantaion fault occur due to some reason 

Now in this condition only debug-point1 will be print on stdio debug-point 2 print was written to stdio buffer but its not flushed because it didnt get \n so we thinks that crash occur after debug-point1

to over come from this if i disable buffering option with stdio and stderr stream like this way

setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);

then is this safe to do this?

why all stream are by default line buffered ?

Edit :

Usualy what is the size of such by default allocated buffer for any file stream? i think its os depended. i would like to know about Linux?

解决方案

why all stream are by default line buffered

They are buffered for performance reasons. The library tries hard to avoid making the system call because it takes long. And not all of them are buffered by default. For instance stderr is usually unbuffered and stdout is line-buffered only when it refers to a tty.

then is this safe to do this?

It is safe to disable buffering but I must say it's not the best debugging technique.

这篇关于它是安全与输出和错误禁用缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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