0xC0000005: Access violation reading location 0x00000004 in c code

hi guys,

i get this error in part of my program. that part is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
float **matrix(long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
if (!m) 
	nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) 
	nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) 
	m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}


the program is correct, becuase i took it form a book.i call this function with this program:

1
2
float **beta;
beta=matrix(1,4,1,1)


and it shows this error:

1
2
First-chance exception at 0x008e50a4 in lfit2.exe: 0xC0000005: Access violation reading location 0xcdcdcdd1.
Unhandled exception at 0x771115de in lfit2.exe: 0xC0000005: Access violation reading location 0xcdcdcdd1.


i dont know if this information is enough or i should put here whole program which is so long. if you need to see whole program please let me know.


I really appreciate any help.


Hi Nhh,

a couple of questions,

1) What is NR_END?

2) What is the purpose of the following statements?

1
2
m += NR_END;
m -= nrl;


1
2
m[nrl] += NR_END;
m[nrl] -= ncl;
Since your program is long, what makes you think this particular part is the cause of the error?
The cdcdcd.. pattern suggests you're trying to use a bit of unititlialized heap memory as the variable for a memory address.

Have you run your code under the debugger to see which line is blowing up? (The debug build should stop at the line where things go wrong, if you're using a debugger.)

Note that you program is trying to access memory ranges which are inaccessible to normal "user mode" apps, where I'm assuming you're using a normal 32-bit Windows PC. In that case, apps can only see address from 0x00000000 through to 0x7FFFFFFF (unless specifically configured to increase the upper limit).

The address 0xcdcdcdd1 is in the range reserved for "kernel mode" (used by device drivers and the lower level parts of the o/s).

Andy

PS For fill patterns, see (e.g.) the "Magic debug values" section of:

Magic number (programming)
http://en.wikipedia.org/wiki/Magic_number_%28programming%29
Last edited on
Topic archived. No new replies allowed.