What might be going on here?
I get
Unhandled exception at 0x5081f508 (msvcr100d.dll) in myProgram.exe: 0xC0000005: Access violation writing location 0x041e0010.
at this line:
fscanf(fp, " %lf %lf %lf\n", &vertices[i].x, &vertices[i].y, &vertices[i].z );
when running my program, but when I step through it in the debug mode (Visual Studio 2010), everything goes just fine; the fscanf()
reads the file as expected.
The exception is actually thrown at input.c
's line:
#else /* _UNICODE */
_FASSIGN( longone-1, (char*)pointer , pFloatStr, (char)decimal, _loc_update.GetLocaleT());
#endif /* _UNICODE */
if I'm not mistaken. And I don't know what these comments about UNICODE around the line mean. That's exactly why I've included them here.
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
The program is something about shading with OpenGL, and the vertices
that you see in the fscanf()
call is an array of these:
typedef struct _Vertex {
double x, y, z;
int polygonsThisPartOf; // Number of polygons this vertex is a part of
Point normal;
} Vertex;
In the first version of my program, the vertices
was an array of arrays, and everything ran fine; this exception started occurring after I had modified the code to use vertices
as an array of the above-mentioned struct
s.
// ˇ THIS is the mistake
vertices = (Vertex *) malloc(vcount * sizeof(Vertex *));
if (vertices == NULL) exit(-2);
The vcount
is correct.
1) Your "fscanf()" syntax looks OK.
2) The "_UNICODE" message (inside the MSVC internals you stepped into) simply means you're using the 16-bit Unicode version of all Win32 codes, which expect 16-bit Unicode format strings (instead of 8-bit ASCII format strings).
This is normal and expected. If you're compiling everything from source inside visual Studio, it shouldn't be an issue.
3) I would focus your efforts on making sure that your array element, "vertices[i]" has been successfully allocated.
SUGGESTION:
Put in a breakpoint at your "fscanf()", and look at the variable before you call fscanf in the debugger.
Additionally, you might want to add this before fscanf, and put your breakpoint at this debug line:
vertices[i].x = vertices[i].y = vertices[i].z = 0;