编译器中的错误? [英] Bug in Compiler?

查看:80
本文介绍了编译器中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在MS C ++ 2010编译器中发现了一个错误。调试版本中不会出现这种情况,但是在启用或不启用

I seem to have found a bug in the MS C++ 2010 compiler. It doesn't occur in the debug version, but does in the release version with or without the

优化器的发布版本中都会出现这种情况。

optimizer turned on.

这是一个产生错误的短程序:

(从更长的程序缩短)

Here is a short program that produces the bug:
(shortened from a much longer program)

#include <STDIO.H>
#include <MATH.H>
#include <STRING.H>
#include <direct.h>
#include <windows.h>
#define MAXSAMP 32000
int main(int argc, char *argv[])
{ char inputline[_MAX_FNAME];
  char fmtsp,fmtxy; //8 bit integer
  unsigned char hbsp,hbx,hby;
  FILE *fin; //file pointers
  if(argc>1) //parameters file is given in command line
    strcpy_s (inputline,_MAX_FNAME,argv[1]);
  else
  { printf("Type the name of the file telling which seismic files to change & which header bytes to use\n");
    gets_s(inputline,_MAX_FNAME);
  }
  if ( (fin=fopen(inputline,"r")) == NULL )
  { printf("Can't open %s\n", inputline);
    exit(1);
  }
  fgets(inputline,_MAX_FNAME,fin);
  sscanf_s(inputline,"%d %c",&hbsp,&fmtsp,1);
printf("0 Shot Point format is %c\n",fmtsp);
  hbsp= (hbsp-1)/4;
printf("1 Shot Point format is %c\n",fmtsp);
  fgets(inputline,_MAX_FNAME,fin);
printf("2 Shot Point format is %c\n",fmtsp);
  sscanf_s(inputline,"%d %d %c",&hbx,&hby,&fmtxy,1);
printf("3 Shot Point format is %c\n",fmtsp);
  hbx=(hbx-1)/4;
  hby=(hby-1)/4;
  fcloseall();
  exit(0);
}

当输入文件为:

17 B        ;&NBSP;&NBSP;&NBSP;&NBSP; :shotpoint标题字节&格式

181 185我       :XY头字节&格式

17 B            :shotpoint header byte & format
181 185 I       :XY header bytes & format



此程序的输出为:


The output from this program is:

0镜头格式为B

1 Shot Point格式为B

2 Shot Point格式为B

3 Shot Point格式为

0 Shot Point format is B
1 Shot Point format is B
2 Shot Point format is B
3 Shot Point format is

IE:不知何故之间最后2个printf语句变量'fmtsp'从B变为空格。

IE: somehow between the last 2 printf statements the variable 'fmtsp' gets changed from B to a blank space.

BTW我试过在2台不同的计算机上编译,一台用Windows XP&一个用Windows 7.可执行文件产生了相同的错误结果。

BTW I tried compiling on 2 different computers, one with Windows XP & one with Windows 7. The executable produced the same wrong results.

推荐答案


sscanf_s(inputline,"%d %c",&hbsp,&fmtsp,1);


它更不容易出错使用

its less error prone to use

std::cin >> inputline;





你用sscanf_s引用的行是解析输出输入行的内容,



您建议的代码是将数据放入输入行,



如果您建议您的代码优于使用gets_s或fgets,就像在OP的


第一个代码示例中那样,那么你错过了一个明显的区别:



gets_s和fgets将从输入中得到一行(字符串)并将其放入输入行。

它们将在换行符处停止。
$


使用流提取运算符的代码将停在第一个空格

字符处。正确的替换将是getline。



这些与OP的原始关注密切相关。



- Wayne

The line you quoted with sscanf_s is parsing the contents out of inputline,

The code you suggested is putting the data into inputline,

If you're suggesting that your code is better than using gets_s or fgets as in the OP's
first code example, then you're missing an obvious difference:

gets_s and fgets will get a line (string) from the input and put it into inputline.
They will stop at a newline.

Your code using the stream extraction operator will stop at the first whitespace
character. A proper replacement would be getline.

None of which is really germane to the OP's original concern.

- Wayne


这篇关于编译器中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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