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.
Additional information
Call Stack:
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
Some other information
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.
Allocation of the array
// ˇ THIS is the mistake
vertices = (Vertex *) malloc(vcount * sizeof(Vertex *));
if (vertices == NULL) exit(-2);
The vcount
is correct.