Created
June 2, 2011 16:34
-
-
Save mastbaum/1004758 to your computer and use it in GitHub Desktop.
Invalid pointer dereference in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** A simple example of an invalid pointer dereference. | |
* | |
* a is not a valid pointer until initialized, so dereferencing it (here, | |
* setting it to 37) is bad news. | |
* | |
* Note that C++ doesn't necessarily initialize pointers to NULL, so it is | |
* possible that this will "work" and not cause a segmentation fault. This | |
* could be really bad, trampling some other memory. We're lucky to get a | |
* segfault. | |
*/ | |
int main() | |
{ | |
// a is declared but not initialized... | |
int* a; | |
// then dereferenced. Bad news. | |
*a = 37; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment