首页 / 更多教程 / C程序崩溃终端 – 我该如何调试?
C程序崩溃终端 – 我该如何调试?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C程序崩溃终端 – 我该如何调试?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1996字,纯文字阅读大概需要3分钟。
内容图文
不要问我怎么样 – 我没有丝毫.
以下代码崩溃了我的终端和我使用的任何运行时分析工具,同时没有引发任何静态检查工具警告. Valgrind,cppcheck和gdb对我没那么好. g -Wall没有给我任何有用的东西.
代码的目标是通过USB串行连接将字符写入audrino. audrino地址作为第一个参数传递.传递unsigned int并将其强制转换为unsigned char.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <iostream>
using namespace std;
int main(int argc,char** argv){
struct termios stdio;
int tty_fd;
unsigned char ctrlChar;
unsigned int ctrlInt;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
tcgetattr(tty_fd, &stdio);
cfmakeraw(&stdio);
stdio.c_cc[VMIN] = 1;
stdio.c_cc[VTIME] = 0;
tcsetattr(tty_fd,TCSANOW, &stdio);
tcsetattr(tty_fd,TCSAFLUSH, &stdio);
fcntl(tty_fd, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
cfsetospeed(&stdio,B9600); // 9600 baud
cfsetispeed(&stdio,B9600); // 9600 baud
cout << "ready on " << argv[1] << endl;
scanf("%u", ctrlInt);
cout <<"recieved initial read.\n";
while (ctrlInt < 256){
ctrlChar = ((char) ctrlInt);
cout << ctrlInt << ctrlChar << endl;
write(tty_fd,&ctrlChar,1);
cout << "sent to audrino.\n";
scanf("%u", ctrlInt);
}
cout << "done!" << endl;
close(tty_fd);
return 0;
}
现在,在这看起来很健全的情况下,我将提交一份错误报告.
系统规格:最新的Arch linux 64位,用“g -g -Wall code.c”编译
解决方法:
如果通过“崩溃我的终端”你的意思是终端停止工作,原因很可能是你在stdout(也就是你的终端)上将各种标志设置为零.你正在调用tcsetattr()并基本上将所有终端控制标志归零.
你应该使用tcgetattr()获取当前的终端标志,修改你想要修改的那些,然后使用该结构来调用tcsetattr().不这样做甚至是undefined behavior:
The effect of
tcsetattr()
is undefined if the value of thetermios
structure pointed to bytermios_p
was not derived from the result of a call totcgetattr()
onfildes
; an application should modify only fields and flags defined by this specification between the call totcgetattr()
andtcsetattr()
, leaving all other fields and flags unmodified.
内容总结
以上是互联网集市为您收集整理的C程序崩溃终端 – 我该如何调试?全部内容,希望文章能够帮你解决C程序崩溃终端 – 我该如何调试?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。