有人知道这里会发生什么吗?
我明白了
Unhandled exception at 0x5081f508 (msvcr100d.dll) in myProgram.exe: 0xC0000005: Access violation writing location 0x041e0010.
在这一行:
fscanf(fp, " %lf %lf %lf\n", &vertices[i].x, &vertices[i].y, &vertices[i].z );
当运行我的程序时,但当我在 Debug模式 (Visual Studio 2010) 中单步执行它时,一切正常; fscanf()
按预期读取文件。
异常其实是在input.c
的那一行抛出的:
#else /* _UNICODE */
_FASSIGN( longone-1, (char*)pointer , pFloatStr, (char)decimal, _loc_update.GetLocaleT());
#endif /* _UNICODE */
如果我没记错的话。我不知道这些关于 UNICODE 的注释是什么意思,这正是我将它们包含在这里的原因。
附加信息
调用堆栈:
msvcr100d.dll!_fassign_l(int flag, char * argument, char * number, localeinfo_struct * plocinfo) Line 258 + 0x6 bytes C++
>msvcr100d.dll!_input_l(_iobuf * stream, const unsigned char * format, localeinfo_struct * plocinfo, char * arglist) Line 1281 + 0x21 bytes C++
msvcr100d.dll!vfscanf(int (_iobuf *, const unsigned char *, localeinfo_struct *, char *)* inputfn, _iobuf * stream, const char * format, localeinfo_struct * plocinfo, char * arglist) Line 61 + 0x13 bytes C
msvcr100d.dll!fscanf(_iobuf * stream, const char * format, ...) Line 99 + 0x18 bytes C
myProgram.exe!main(int argc, char * * argv) Line 166 + 0x49 bytes C++
myProgram.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
myProgram.exe!mainCRTStartup() Line 371 C
一些其他信息
该程序是关于使用 OpenGL 进行着色的,您在 fscanf()
调用中看到的 vertices
是一个数组:
typedef struct _Vertex {
double x, y, z;
int polygonsThisPartOf; // Number of polygons this vertex is a part of
Point normal;
} Vertex;
在我的程序的第一个版本中,vertices
是一个数组数组,一切都运行良好;在我修改代码以使用 vertices
作为上述 struct
的数组后,此异常开始发生。
数组的分配
// ˇ THIS is the mistake
vertices = (Vertex *) malloc(vcount * sizeof(Vertex *));
if (vertices == NULL) exit(-2);
vcount
是正确的。
最佳答案
1) 您的“fscanf()”语法看起来没问题。
2) “_UNICODE”消息(在您进入的 MSVC 内部)仅表示您正在使用所有 Win32 代码的 16 位 Unicode 版本,它需要 16 位 Unicode 格式字符串(而不是 8 位ASCII 格式字符串)。
这是正常的,也是意料之中的。如果您在 Visual Studio 中从源代码编译所有内容,这应该不是问题。
3) 我会将您的精力集中在确保您的数组元素“vertices[i]”已成功分配上。
建议:
在您的“fscanf()”处放置一个断点,并在调试器中调用 fscanf 之前查看变量。
此外,您可能想在 fscanf 之前添加它,并将断点放在此调试行:
vertices[i].x = vertices[i].y = vertices[i].z = 0;
关于c - "Access violation"运行时使用 fscanf(),但调试时不使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162574/