[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppAccessViolation

(C++) Access violation

A nasty run-time error. It means you've read/written to an uninitialized memory location. Some compilers call these segmentation faults or return SIGSEGV.

There can be many causes for an access violation: A simple example inevitable causing an access violation due to reading out-of-range:
  1. include <iostream>
int main() { int index = 0; while(1) { std::cout << "Reading from index " << index << std::endl; std::cout << "Found: " << argv[index] << std::endl; ++index; } }


A simple example inevitable causing an access violation due to reading from an unitialized pointer:
  char * myName;
  std::cout << myName;


Finding access violations in your code caused by arrays /std::vectors

There are multiple options to find these:

1) Breakpoints

Just walk through your code at run-time until you find the line causing this error. Then check whether the index is in range of the array or container.

Note that this might NOT always directly give you the line causing the error. If the line is something like below, you have probably found it:
  myVector[i] = 0.0; //probably 'i' is out of range
But if you find a 'strange' line like the one below, what do you think then:
  const double myValue = myClass.getValue();
Then probably the object 'myClass' is read from an out-of-range location! For example:

  1. include <iostream>
struct MyClass { double getValue() const { return 0.0; } }; void doStuff(const MyClass& myClass) { const double myValue = myClass.getValue(); //The innocent looking line std::cout << "myValue: " << myValue << std::endl; } int main() { std::vector<MyClass> v(10); doStuff(v[10]); //ERROR: index 10 does not exist!!! }


The drawback of this method is that you can find an error, but it can occur later in the same line again. Therefore, it is a non-permantent solution. Prefer using assert.

2) assert-statements

Just assert all indices are in range. Instead of:
  myVector[i] = 0.0;
write
  assert(i>=0 && i<static_cast<int>(myVector.size()));
  myVector[i] = 0.0;
Now you will get a nice error message about the right line.

There are no drawbacks to this method, as one #define NDEBUG line can remove all statements for you.

3) The at method

Some STL containers have the at method that does range checking itself. Instead of:
  myVector[i] = 0.0;
write
  myVector.at(i) = 0.0;
The drawback of this way is that when you want to speed-optimize your program (after debugging of course), you have to replace all at methods by the unchecked index operators []. Prefer using assert.

Finding access violations in your code caused by uninitialized pointers

At first, when you use a pointer, either directly initialize it, or else initialize it with 0 (or NULL).

Then debugging is easy. Instead of writing:

  myPointer->doSomething();


Write
  assert(myPointer!=0); //Assert it is initialized
  myPointer->doSomething();


last edited (November 9, 2006) by bilderbikkel, Number of views: 4319, Current Rev: 9 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.