本文介绍了使用GDB进行NAN错误调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如果我将负数作为参数传递给sqrt(),则返回NAN为 。有没有办法使用GDB调试这个问题 让我知道 Co 解决方案 如果它正在按你的预期行事,我不会没有看到问题(但请注意 这种域错误的结果是实现定义的,而不是 必然是可移植的。) 特定工具(例如gdb)在这里是偏离主题的。这个小组讨论了 C语言,而不是工具。 -Kevin - 我的电子邮件地址有效,但会定期更改。 要联系我,请使用最近发布的地址。 如果你想发现什么功能通过 sqrt()的负面参数,一个只有C的方法 将是换行的使用您自己的函数调用sqrt(): #include< stdio.h> #include< math.h> double my_sqrt(double x,const char * file,int line){ if(x< 0.0) fprintf(stderr," sqrt( %g)从%s调用,行%d \ n", x,file,line); 返回sqrt(x); } 然后在每个调用sqrt()的文件中,你会在* #include< math.h>之后插入 之类的东西* ;: #include< math.h> ... double my_sqrt(double,const char *, int); #undef sqrt / * in case< math.h>将其定义为宏* / #define sqrt(x)my_sqrt((x),__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $当然,这些行可以存在于你自己的my_sqrt.h中。头文件,如果方便的话。 - Er*********@sun.com 如果你试图发现什么函数传递 sqrt()的负面参数,一个只有C的方法将是& ;包裹"使用您自己的函数调用sqrt(): #include< stdio.h> #include< math.h> double my_sqrt(double x,const char * file,int line){ if(x< 0.0) fprintf(stderr,从%s调用的sqrt(%g),行%d \ n", x,file,line); 返回sqrt(x); } 然后在每个调用sqrt()的文件中,你会插入这样的东西*之后* #include< math.h>: #include< math.h> ... double my_sqrt(double,const char *,int ); #undef sqrt / * in case< math.h>将其定义为宏* / #define sqrt(x)my_sqrt((x),__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 这些拦截当然,这些行可以存在于你自己的my_sqrt.h中。头文件,如果这很方便。 这是一个很棒的想法! 你从哪里得到的? ;-) If I pass a negative number as argument to sqrt() it returns a NAN asexpected. Is there any way to debug this problem using GDBLet me knowCo 解决方案If it''s doing what you expected, I don''t see a problem (but note thatthe result of such a domain error is implementation-defined, and notnecessarily portable).Specific tools (such as gdb) are off-topic here. This group discussesthe C language, not tools.-Kevin--My email address is valid, but changes periodically.To contact me please use the address from a recent posting.If you are trying to discover what function passesthe negative argument to sqrt(), one C-only approachwould be to "wrap" the sqrt() call with your own function:#include <stdio.h>#include <math.h>double my_sqrt(double x, const char *file, int line) {if (x < 0.0)fprintf (stderr, "sqrt(%g) called from %s, line %d\n",x, file, line);return sqrt(x);}Then in each file that calls sqrt(), you would insertsomething like this *after* the #include <math.h>:#include <math.h>...double my_sqrt(double, const char*, int);#undef sqrt /* in case <math.h> defines it as a macro */#define sqrt(x) my_sqrt((x), __FILE__, __LINE__)These "intercepting" lines could, of course, reside inyour own "my_sqrt.h" header file if that''s convenient.-- Er*********@sun.com If you are trying to discover what function passes the negative argument to sqrt(), one C-only approach would be to "wrap" the sqrt() call with your own function: #include <stdio.h> #include <math.h> double my_sqrt(double x, const char *file, int line) { if (x < 0.0) fprintf (stderr, "sqrt(%g) called from %s, line %d\n", x, file, line); return sqrt(x); } Then in each file that calls sqrt(), you would insert something like this *after* the #include <math.h>: #include <math.h> ... double my_sqrt(double, const char*, int); #undef sqrt /* in case <math.h> defines it as a macro */ #define sqrt(x) my_sqrt((x), __FILE__, __LINE__) These "intercepting" lines could, of course, reside in your own "my_sqrt.h" header file if that''s convenient.That''s a *great* idea!Where did you get it? ;-) 这篇关于使用GDB进行NAN错误调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 07:23