Q: Crashes dealing with invalid pointers As I am trying to debug my server code using GDB I sometimes get a stack trace that seems to make no sense to me. 
Examples:


*

*An argument value to a function that should be impossible because the code that calls the function prevents the invalid value.

*Crashing while iterating a map where it should be guaranteed that all values inside the map are valid.
My question is as follows:
Is it possible for an invalid pointer (one that has been deleted but is still being used like it's valid) to break other code that would otherwise be fine? Could an invalid pointer somehow mess up the memory inside of a std::map, even if the pointer has nothing to do with the std::map? Basically if your pointer is pointing to some random memory and doing operations on it, is it possible that it could invalidate anything in your program?

A: Yes, if your pointers really are bad, then almost anything could happen.  But it is also likely that GDB has failed to reconstruct the state of execution correctly, perhaps because the stack is corrupt, or perhaps because certain values have been optimised out of existence.
How easy or hard it is to recover a stack depends on which processor is in use and the ABI for that platform.  If you stated those things, and perhaps gave an example stack, then it might be possible to be more definitive.
Here's a debugging tip: If you can, halt the execution of your program near to, but before it gets to the bit where it crashes, and take a look at the stack then.  Remember some of the memory addresses of relevant objects.  Then, when the program gets a bit deeper into its execution, dump out those same objects and see if they have changed.  This will give you confidence which of the values in the stack trace should be believed.

A: Yes it is possible.
A pointer if not initialized, deleted but then used, has a random value and if you write at its pointer location you can overwrite any part of your code, or anything in memory.
Also, if you defined memory area too small for its data you can overwrite any type of code within your program/memory.
Often as C/C++ programmer you get mysterious crashes and core dumps, which are usually that you have tried to use an invalid pointer.
