-2

For uninitialized pointers, such as

int * temp;

what determines the random location in memory that they end up pointing to? Does c++ use a random number generator to fill a memory address for my uninitialized pointer?

gabriel__
  • 20
  • 4
  • 2
    Undefined behavior – drescherjm Sep 26 '22 at 01:44
  • 2
    ***Does c++ use a random number generator to fill a memory address for my uninitialized pointer?*** No it does not. With that said in msvc memory blocks are filled with special values in debug mode: [https://stackoverflow.com/questions/127386/what-are-the-debug-memory-fill-patterns-in-visual-studio-c-and-windows](https://stackoverflow.com/questions/127386/what-are-the-debug-memory-fill-patterns-in-visual-studio-c-and-windows) – drescherjm Sep 26 '22 at 01:45
  • 5
    The actual number is obtained [by counting how many demons flew out of everyone's noses, to date](http://www.catb.org/jargon/html/N/nasal-demons.html). – Sam Varshavchik Sep 26 '22 at 01:45
  • 2
    Undefined does not imply random. It could be extremely regular and dead predictable for any given instance. Unfortunately the rules could change for every distinct case. Typically you get whatever happens to be in memory when the program decided to use that memory for the pointer. Sometimes it's worth figuring out exactly what happens, especially when working in security, but most of the time don't even try reasoning with it. Just don't do it. – user4581301 Sep 26 '22 at 02:27
  • Regarding "random", see [Is uninitialized local variable the fastest random number generator?](https://stackoverflow.com/questions/31739792/) In this context, "random location in memory" means "unpredictable" rather than "randomly distributed". (A pseudo-random number generator, on the other hand, produces randomly distributed values that are predictable if you know the seed.) – JaMiT Sep 26 '22 at 03:53
  • C++ is known for its efficiency. Filling uninitialized variables with random numbers would be extremely inefficient - pseudo-random numbers are very slow to generate. – Mark Ransom Sep 26 '22 at 13:51

2 Answers2

3

An uninitialized variable usually contains whatever pre-existing data was already present in the memory that the variable occupies.

Although, some compilers do fill in uninitialized memory with preset byte patterns in debug builds to aid in debugging. See Debug values for some examples, and also see What are the debug memory fill patterns in Visual Studio C++ and Windows?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

Nothing, it's undefined. It is not "random", it is simply non-deterministic. in practice, the value of any initialised variable is determined by whatever happens to be in the memory that the variable occupies. That may for example be leftover from previous operations, as initialised by the system on start-up, or just however the physical hardware behaves. None of that is deterministic but unlikely to be in any sense random.

The point is that undefined behaviour is usually the result of the compiler doing exactly nothing and accepting the result will be undefined. So no, the compiler does not generate a random number - that would be initialisation. Pointless initialisation, but initialisation nontheless.

Clifford
  • 88,407
  • 13
  • 85
  • 165