[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppAccessViolation
There can be many causes for an access violation:
A simple example inevitable causing an access violation due to reading from an unitialized pointer:
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:
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.
There are no drawbacks to this method, as one #define NDEBUG line can remove all statements for you.
Then debugging is easy. Instead of writing:
Write
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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:
- Read/writing out-of-range of arrays/std::vectors
- Read/writing to/from an uninitialized pointer
int main() { int index = 0; while(1) { std::cout << "Reading from index " << index << std::endl; std::cout << "Found: " << argv[index] << std::endl; ++index; } }
- include <iostream>
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
- 2) assert-statements
- 3) The at CppMethod
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 rangeBut 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:
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!!! }
- include <iostream>
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();
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
