I've learned about wild pointers and how to avoid them. I've heard that they point to a random value in memory and I was wondering if they actually have a random and completely irrelevant value or if they point to a certain value that's probably implementation dependent. Which is it?
CodePudding user response:
These dangling pointers are not random in the "mathematical" sense. They point to an implementation dependent location (based on hardware arch, runtime sequence, etc), that your program isn't supposed to have access to & the content of the pointed location is undefined from your program's point of view. The operating system is free to re-allocate & write whatever whenever so your pointer to that memory is deemed "random" in its value.
CodePudding user response:
The value isn't random in the sense that it has an even probability of being any value. The value is merely unknown.
It could point to memory being used by something else in your program. Writing to this memory could cause other parts of your program to malfunction, or it might trigger a protection fault.
It could point to unused memory. Reading from this memory could trigger a protection fault. Writing to this memory might work fine, or it might trigger a protection fault.
You don't know which of these outcomes you're going to get.[1] And it's not always going to be the same. So there's no practical purpose to using an uninitalized pointer.
- The hardware limits some, but not all, options.