Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Access Violation / Problem with Memory Read

I'm trying to read a file which has numerous structs which have to be looped through . I've just started coding the fread statements. Each one will need to read through a struct a certain number of times and then I'll probably have to do an fseek.

When not in debug mode, the code runs without error but no data is printed to the file. I am getting this error in debug: "Unhandled exception in file.exe: 0xC0000005: Access Violation.

When I step through it the errors seems to occur during MEMCPY.ASM.

Here is the code with the error line highlighted:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct preRec1 {
char ESTIMATE_NUMBER1[4];
char PAGE_NUMBER1[4];
char PROC_TYPE1[3];
};

struct preRec1a{
char X1[1];
};

struct preRec1b{
char ESTIMATE_NUMBER2[4];
char PAGE_NUMBER2[4];
char PROC_TYPE2[2];
char X2[8];
char PRIORITY[2];
char SALES_TAX[2];
};

struct Rec1 {
char ESTIMATE_NUMBER1[5];
char PAGE_NUMBER1[5];
char PROC_TYPE1[3];
};
struct Rec1a{
char X1[1];
};
struct Rec1b{
char ESTIMATE_NUMBER2[5];
char PAGE_NUMBER2[5];
char PROC_TYPE2[3];
char X2[9];
char PRIORITY[3];
char SALES_TAX[2];
};

void main(void)
{
FILE *f, *f2, *f3; // *f4, *f5;

struct preRec1 pr1;
struct preRec1a pr1a;
struct preRec1b pr1b;

struct Rec1 r1;
struct Rec1a r1a;
struct Rec1b r1b;

f = fopen("ESTINDEX", "rb");
f2 = fopen("ESTINDEX_R1_OUT", "wb");
f3 = fopen("ESTINDEX_R2_OUT", "wb");

memset(&r1, 0x0, sizeof(r1));

while (fread(&pr1, sizeof(struct preRec1), 22, f))
{
memcpy(&r1.ESTIMATE_NUMBER1, &pr1.ESTIMATE_NUMBER1, sizeof(r1.ESTIMATE_NUMBER1));
memcpy(&r1.PAGE_NUMBER1, &pr1.PAGE_NUMBER1, sizeof(r1.PAGE_NUMBER1));
memcpy(&r1.PROC_TYPE1, &pr1.PROC_TYPE1, sizeof(r1.PROC_TYPE1));
fprintf(f2, "%s|", r1.ESTIMATE_NUMBER1);
fprintf(f2, "%s|", r1.PAGE_NUMBER1);
fprintf(f2, "%s\n", r1.PROC_TYPE1);
}

fclose(f);
printf("All Done\n");

}

Once the error line is hit I am taken to the disassembly: 393335FF ?.

What does this mean?

Thanks, cj
[2394 byte] By [cjspsx] at [2007-11-11 10:19:45]
# 1 Re: Access Violation / Problem with Memory Read
It means you have made a mistake on either your memset or one of the memcpy's. You went off the end of one of your addresses or pointers into memory that does not belong to your program, and the OS stopped you with a crash. Debug programs are padded and so while the bug remains, you fail to crash (accidentally moved into memory that you do own (by luck!) but its still not part of the same variable and still a mistake!).

Memset and memcpy and the like are frowned upon unless your writing speed critical code. They are raw memory to memory functions with no protections, so you have to be careful with them, and they are extremely unsafe for classes & templates and the like.
jonnin at 2007-11-11 20:59:08 >
# 2 Re: Access Violation / Problem with Memory Read
Note that it could be almost anything from a file that failed to open or a bad pointer or a bad sizeof operation etc. First place to start is to verify that you actually opened the files (read first line and echo it out to test for example).
jonnin at 2007-11-11 21:00:08 >
# 3 Re: Access Violation / Problem with Memory Read
Why do not you try to read element by element and fill up the structure later, you will find out which field is causing the problem
it career at 2007-11-11 21:01:11 >
# 4 Re: Access Violation / Problem with Memory Read
I think you are trying to read 22 structures, but the destination buffer contains only one element:
struct preRec1 pr1;
. . .
while( fread(&pr1, sizeof(struct preRec1), 22, f))
{
. . .
}
You should change this either to:
struct preRec1 pr1;
. . .
while( fread(&pr1, sizeof(struct preRec1), 1, f))
{
. . .
}
or
struct preRec1 pr1[22];
. . .
while( fread(pr1, sizeof(struct preRec1), 22, f))
{
. . .
}
I hope this helps.
Viorel at 2007-11-11 21:02:06 >