John
John

Reputation: 804

why do I get access violation run time error?

I have this structure

typedef struct fpinfo
{
    unsigned long chunk_offset;
    unsigned long chunk_length;
    unsigned char *fing_print;
}fpinfo;

typedef struct Hash_Entry {
    struct Hash_Entry *next;  /* Link entries within same bucket. */
    unsigned namehash;        /* hash value of key */
    struct fpinfo fp;
} Hash_Entry;

and the following line of code to extract the 10 msb from the fing_print array

unsigned int h;
h = (he.fp.fing_print[0] << 2 | (he.fp.fing_print[1] & 0xC0) >> 6) & 0x3FF;

Here is how I initialized the he data member by reading contents from a file

while(fscanf(rd,"%ul,%ul,%X",&test_st.fp.chunk_offset,&test_st.fp.chunk_length,&test_st.fp.fing_print) !=EOF)
{   
    ....
}

vc 2010 gives the error:

Unhandled exception at 0x013217f8 in htable.exe: 0xC0000005: Access violation reading location 0xcccccccc.

what is wrong with it?

Upvotes: 1

Views: 1476

Answers (2)

rakesh
rakesh

Reputation: 612

possible reason could be that test_st is not initialized and you are using it to initialize fp.

Upvotes: 0

NPE
NPE

Reputation: 500913

The most likely reason is that he.fp.fing_print has not been initialized, so your process crashes when trying to access its elements. To verify, print out the value of the pointer, or examine it in the debugger.

edit There are two problems with the fscanf() code:

  1. It's not totally clear what the intent is, but %X together with &test_st.fp.fing_print overwrites the pointer;
  2. You don't appear to allocate memory for test_st.fp.fing_print.

Upvotes: 1

Related Questions