Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created June 2, 2011 16:34
Show Gist options
  • Save mastbaum/1004758 to your computer and use it in GitHub Desktop.
Save mastbaum/1004758 to your computer and use it in GitHub Desktop.
Invalid pointer dereference in C++
/** 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