Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the program run perfect when the program use the data from the coding.
but when i try to import from txt file he make a problems.
the code of the import from txt is working

i got message error
Unhandled exception at 0x014621fa in Tanks.exe: 0xC0000005: Access violation reading location 0xfeeefeee.

and it point to line
C++
void Graph::addEdge(int u, int v) {
    A[u-1][v-1] =1; 
THIS LINE->	A[v-1][u-1] = 1;
}


the fun get 1 and 2.
really dont know why the problem

reply to Sergey-
yeah ofcourse its just comment that i added here.

did few times debug and same error.
if i put data in my code like
g.addEdge(1, 2); g.addEdge(1, 3);
g.addEdge(2, 3); g.addEdge(2, 4);
g.addEdge(3, 4); g.addEdge(4 ,5);

it works perfect. but when i add g.addedge between the lines of import from txt and show the problem.
Posted
Updated 8-Jan-16 7:16am
v2
Comments
Sergey Alexandrovich Kryukov 8-Jan-16 13:10pm    
You simply address beyond the memory fragment allocated for A. You did not show what it is. Just check up everything thoroughly, use the debugger.

"THIS LINE" should be your comment in your code sample, not the "code" which won't compile.

—SA
Foothill 8-Jan-16 15:49pm    
If you always perform a boundry check on any index accessor before you use it, you can preven this problem and many others as well. IIRC, the Heartbleed bug was caused by a function that didn't validate a parameter before using it to access an array.
Jochen Arndt 8-Jan-16 15:51pm    
Sergey already told you what is happening:
You are accessing A out of bounds.

Probable error sources are calling addEdge with u greater than the x size of the array, and/or v greater than the y size of the array (array allocated as A[x][y]), or with u and/or v less or equal to zero.
Sergey Alexandrovich Kryukov 8-Jan-16 16:58pm    
Also, A could be anything, including null or some non-initialized garbage.
—SA
[no name] 8-Jan-16 22:03pm    
Are you using visual studio. Which version? However only one way to solve it (we can't do it for you) - learn to use assert() and your debugger.

1 solution

write some check code in your function like:

C++
void Graph::addEdge(int u, int v) {
  ASSERT( (u>0) && (v>0) ); //!!!! 0 is valid (my tip)
  ASSERT( (u    A[u-1][v-1] =1; 
}


writing some error check and test code is good style and always paying off. ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900