Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Bug with access violation :(

Reply
Thread Tools

Bug with access violation :(

 
 
Alex
Guest
Posts: n/a
 
      09-13-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-13-2005
* 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?
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      09-13-2005

"Alex" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-13-2005
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.

 
Reply With Quote
 
Aleksey Loginov
Guest
Posts: n/a
 
      09-14-2005

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.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-14-2005

"Aleksey Loginov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[BUG] cross-thread violation on rb_gc() Tim Hunter Ruby 1 06-19-2009 10:46 PM
Access Violation error while saving config from PIX through PDM toa TFTP server. AM Cisco 4 10-10-2006 07:59 PM
[BUG] cross-thread violation rakaur Ruby 4 02-17-2006 08:07 AM
ASP.NET and managed C++: Access violation when AppDomain is unloaded. news ASP .Net 0 10-06-2004 06:51 AM
time.mktime memory access violation bug Bengt Richter Python 6 11-21-2003 03:07 AM



Advertisments