1

Before ask I'm not the native speaker pardon for my poor english

Reading some book about c++ and the book mentioned variables should be initialize after declared. Otherwise variable having a garbage value and this may cause some problem. This garbage values are random and not predictable, what if using this garbage value as a ID? I tried to find relational topic an google and couldn't find any result

Please leave a comment of idea of references

mon0mon
  • 11
  • 1
  • 1
    What's in an not initialized variable, is not defined: it may be garbage or zero or anything else, depending on the compiler and its setting. You certainly cannot rely on it's being unique or unpredictable or use it as a source of randomness. – bereal Jun 15 '21 at 04:12
  • Not initialized variable I mentioned was variable that just declared. e.g (int a;) Thanks for the comment :) – mon0mon Jun 15 '21 at 04:20

3 Answers3

2

When you initialize a non-static(local) variable and use/read/print it before assigning a variable to it, the action is undefined behavior.

When you encounter undefined behavior:

  • You cannot assume that there's any value in that memory space to begin with
  • You cannot assume that the value won't change throughout the code execution, therefore, it's not ideal to use it as an ID
  • You cannot even assume that the program will run, or it will crash/freeze/etc... as everything action from that point on is undefined

Theoretically, an undefined behavior permits anything to happen (obviously in reality there's many restrictions to try and keep it from doing everything and probably sending everyone your pictures, corrupt all your files and frying your computer afterward).

For example, with this piece of code:

int num;
int a = num;
int b = num;

When you print it out, a and b is not guaranteed to have identical value, or the program will run at all. It's different with each systems/compilers.

As such, one must never rely on an undefined behavior to act normally.

Quote from @AnT:

So in general, the popular answer that "it is initialized with whatever garbage was in memory" is not even remotely correct. Uninitialized variable's behavior is different from that of a variable initialized with garbage.

However, never mistakes undefined for inconsistency/randomness. For a (pretty bad) real life example, a broken pipe may be unexpected behavior, but the stream of water that hits you isn't inconsistent or random (until the pipe is fixed, of course).

An example when undefined behavior isn't simply taking some garbage value and results in some bizarre stuff: https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=633

Related :

silverfox
  • 1,568
  • 10
  • 27
1

Nothing guarantees that an uninitialized variable contains garbage. It may be garbage, but in fact, any access to such variable yields undefined behavior, which means that literally anything may happen. It may be garbage, it may be constant, it may crash, it may contain previously used data, it may work like expected, but only with some compilers, etc. One is not supposed to rely on anything that comes from such variable and build any logic based on that. And, certainly, it cannot be a source of uniqueness or randomness.

bereal
  • 32,519
  • 6
  • 58
  • 104
0

First of all you should not use garbage value as ID.

Since not detail about your question, I am Considering your id is an unique value also by using your ID you are going to retrieve some value. if that is the case then,

you cannot because on each and every run your garbage value may change.

And Garbage value in the sense it may be a numerical alphanumerical even not a proper value may come.

Dhanasekaran Don
  • 294
  • 1
  • 14