Bug with access violation :(

A

Alex

Hello

Im working on project for my college, nevermind (but it urgent :((()...
So I have this code example (in VS 6.0) in main class :



REAL *Input[NUM_DATA];

....

size[br] = r.getSize();

Input[br] = new double [size[br]];

for (i=0; i<size[br]/12; i++) {
for (j =0; j< 12; j++){
Input[br][i*12+j]=r.vector[j];

}

....
delete[] r.values;

//
size iz a static field witch contains a number of data in object r. Br
is variable which values go from 0 to NUM_VALUES.

when I execute my program, it crashes. I have tried to put breakpoints
and Debug it and Ive found out that it crashes in line

Input[br][i*12+j]=r.vector[j];

when br comes to 3. Debug message is:

Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic
information found.
Loaded 'C:\WINDOWS\system32\shimeng.dll', no matching symbolic
information found.
First-chance exception in NN.exe (NTDLL.DLL): 0xC0000005: Access
Violation.
First-chance exception in NN.exe: 0xC0000005: Access Violation.


How can I fix this? I dont know if it matters, but size[br] values are
very high / around 8500, so "new double[size[br]]" will have to
allocate pretty much space in memory. Is there any limit for allocation
of dynamic fields?
(I doubt in line "Input[br] = new double [size[br]];" when br comes to
3, when no allocation is possible, so it crashes in line I mentioned-
when it wanted to put value from r into "nothing"
But before I change whole code, I wanted to ask if Im wrong..pls answer
me soon).

Thank you a lot.

Aleksandra
 
A

Alf P. Steinbach

* Alex:
Im working on project for my college, nevermind (but it urgent :((()...
So I have this code example (in VS 6.0) in main class :

Consider upgrading to a more modern compiler, it will save you much work; you
can download version 7.1 for free from Microsoft (Visual C++ Toolkit 2003).

REAL *Input[NUM_DATA];

In C++, preferentially use symbolic constants, not macros.

Also, consider using standard library classes, like

std::vector< std::vector<double> > input( numData );

...

assert( br < numData );
int const size = r.getSize();
input.at( br ).resize( size );
for( int i = 0; i < size/12; ++i )
{
for( int j = 0; j < 12; ++j )
{
int const index = 12*i + j;
input.at( br ).at( index ) = r.vector.at( j ); // ?
}
}

instead of raw arrays and pointers and non-checking indexing and other
low-level stuff like that.

Using the above approach you'll get an error as soon as something is wrong,
which makes it much easier to figure out the cause.

...

size[br] = r.getSize();

Input[br] = new double [size[br]];

for (i=0; i<size[br]/12; i++) {
for (j =0; j< 12; j++){
Input[br][i*12+j]=r.vector[j];

}

...
delete[] r.values;
//
size iz a static field witch contains a number of data in object r.

That may be part of your problem: unless there is ever only one object of that
class, or all co-existing objects are the same size, a static field (there is
only one shared one) is bound to have the wrong size for some such object.

Br
is variable which values go from 0 to NUM_VALUES.

br should never be equal to NUM_DATA, always less; is that what "to" means?

Have you confused two symbols NUM_DATA and NUM_VALUES?

when I execute my program, it crashes. I have tried to put breakpoints
and Debug it and Ive found out that it crashes in line
Input[br][i*12+j]=r.vector[j];

...
How can I fix this? I dont know if it matters, but size[br] values are
very high / around 8500, so "new double[size[br]]" will have to
allocate pretty much space in memory. Is there any limit for allocation
of dynamic fields?

Not that low, no.

(I doubt in line "Input[br] = new double [size[br]];" when br comes to
3, when no allocation is possible, so it crashes in line I mentioned-
when it wanted to put value from r into "nothing"
But before I change whole code, I wanted to ask if Im wrong..pls answer
me soon).
 
J

Jim Langston

Alex said:
Hello

Im working on project for my college, nevermind (but it urgent :((()...
So I have this code example (in VS 6.0) in main class :



REAL *Input[NUM_DATA];

...

size[br] = r.getSize();

Input[br] = new double [size[br]];

for (i=0; i<size[br]/12; i++) {
for (j =0; j< 12; j++){
Input[br][i*12+j]=r.vector[j];

}

...
delete[] r.values;

Let me see if I'm following this right.

REAL *Input[NUM_DATA]; // Array of pointers NUM_DATA size
Input[br] = new double [size[br]]; // Array of doubles size[br] size

for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
for (j = 0; j < 12; j++ ) // 0 to 11
Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) * 11
+ 11;

Now. Are you positive that (size[br]-1) * 11 + 11 is smaller that NUM_DATA
?
//
size iz a static field witch contains a number of data in object r. Br
is variable which values go from 0 to NUM_VALUES.

when I execute my program, it crashes. I have tried to put breakpoints
and Debug it and Ive found out that it crashes in line

Input[br][i*12+j]=r.vector[j];

when br comes to 3. Debug message is:

when br comes to 3... I don't see you increasing br in the above code. You
left it out.
but it seems fairly obvious that [size[br]] > NUM_DATA
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic
information found.
Loaded 'C:\WINDOWS\system32\shimeng.dll', no matching symbolic
information found.
First-chance exception in NN.exe (NTDLL.DLL): 0xC0000005: Access
Violation.
First-chance exception in NN.exe: 0xC0000005: Access Violation.

Access violation generally means you're trying to access memory that doesn't
"belong" to you.
How can I fix this? I dont know if it matters, but size[br] values are
very high / around 8500, so "new double[size[br]]" will have to
allocate pretty much space in memory. Is there any limit for allocation
of dynamic fields?
(I doubt in line "Input[br] = new double [size[br]];" when br comes to
3, when no allocation is possible, so it crashes in line I mentioned-
when it wanted to put value from r into "nothing"
But before I change whole code, I wanted to ask if Im wrong..pls answer
me soon).

Thank you a lot.

Aleksandra
 
O

Old Wolf

Alex said:
Im working on project for my college, nevermind (but it urgent :((()...
So I have this code example (in VS 6.0) in main class :

REAL *Input[NUM_DATA];
size[br] = r.getSize();
Input[br] = new double [size[br]];
for (i=0; i<size[br]/12; i++) {
for (j =0; j< 12; j++){
Input[br][i*12+j]=r.vector[j];
}
delete[] r.values;

when I execute my program, it crashes. I have tried to put
breakpoints and Debug it and Ive found out that it crashes in line

Input[br][i*12+j]=r.vector[j];

Please post code that compiles. You haven't given enough
information for people to make an informed guess at what's
going wrong.
 
A

Aleksey Loginov

Jim said:
REAL *Input[NUM_DATA]; // Array of pointers NUM_DATA size
Input[br] = new double [size[br]]; // Array of doubles size[br] size

for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
for (j = 0; j < 12; j++ ) // 0 to 11
Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) * 11
+ 11;

(size[br]/12-1)*12 + 11 == size[br]-1 ?
Now. Are you positive that (size[br]-1) * 11 + 11 is smaller that NUM_DATA
?
[...]

to OP: show complete code.
 
J

Jim Langston

Aleksey Loginov said:
Jim said:
REAL *Input[NUM_DATA]; // Array of pointers NUM_DATA size
Input[br] = new double [size[br]]; // Array of doubles size[br] size

for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
for (j = 0; j < 12; j++ ) // 0 to 11
Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) *
11
+ 11;

(size[br]/12-1)*12 + 11 == size[br]-1 ?

Umm... let's see...

for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
for (j = 0; j < 12; j++ ) // 0 to 11
Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) * 11
+ 11;

((size[br] / 12) - 1) * 12 + 11;

Hmm.. yeah, that doesn't resolve down to (size[br]-1) * 11 + 11 by any
means.

Gah.

That's why it's best not to do this type of stuff this way.

My old algebra teacher would kill me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top