Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Bug with access violation :( (http://www.velocityreviews.com/forums/t448337-bug-with-access-violation.html)

Alex 09-13-2005 08:27 AM

Bug with access violation :(
 
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


Alf P. Steinbach 09-13-2005 08:54 AM

Re: Bug with access violation :(
 
* 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).


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jim Langston 09-13-2005 08:03 PM

Re: Bug with access violation :(
 

"Alex" <aleksandra.cerekovic@gmail.com> wrote in message
news:1126600066.770770.196890@g49g2000cwa.googlegr oups.com...
> 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
>




Old Wolf 09-13-2005 09:46 PM

Re: Bug with access violation :(
 
Alex wrote:
>
> 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.


Aleksey Loginov 09-14-2005 07:22 AM

Re: Bug with access violation :(
 

Jim Langston wrote:
> 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.


Jim Langston 09-14-2005 01:09 PM

Re: Bug with access violation :(
 

"Aleksey Loginov" <Aleksey.Loginov@gmail.com> wrote in message
news:1126682568.376898.296680@g49g2000cwa.googlegr oups.com...
>
> Jim Langston wrote:
>> 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.




All times are GMT. The time now is 05:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.