Is Uninitialized Local Variable the Fastest Random Number Generator

How does an uninitialized variable get a random value?

The 'random' value is simply what's left in memory at that location. Memory usually isn't erased/zeroed when it's freed so whatever was there will linger until it's overwritten.

Why prinf prints random number?

For starters, the function main without parameters according to the C Standard shall be declared as follows.

int main( void )

Secondly, the variable a is not initialized and has an indeterminate value, so the program invokes undefined behavior.

You need to initialize the variable a with a value before outputting that value, or the output will be undefined.

The variable a is defined in the outer block scope of the function main without the storage class specifier static. So it has automatic storage duration. Such variables are not implicitly initialized.

Is it good idea to pass uninitialized variable to srand?

No, it isn't.

Reading an uninitialized value results in undefined behavior. It can be zero, it can be semi-random — but as such, it can repeatedly be the same value. It may also cause compilation or runtime errors, or do any other completely unpredictable thing.

Then, someone else compiling your program will notice the compiler warning about uninitialized value and try to fix it. He may fix it correctly, or given complex enough program he may just initialize it to zero. That's how 'small' bugs turn into huge bugs.


Just try your snippet with srand() replaced by printf():

#include <stdio.h>

int main()
{
{
unsigned seed;
printf("%u\n", seed);
}

return 0;
}

On my system it repeatedly gives 0. This means that there's at least one system where your code is broken :).

uninitialized local variable used c++

Why can't I initialize the integer variable num with the value of the number field of the Strct structure?

Because the pointer is uninitialised, and thus it doesn't point to any object. Indirecting through the pointer, or even reading the value of the pointer result in undefined behaviour.

I thought my strct points to the Strct structure, that is, to its type

No. Pointers don't point to types. Object pointers point to objects. Types are not objects in C++.

16.0f is not the value of number. 16.0f is the default member initialiser of that member. If you create an object of type Strct, and you don't provide an initialiser for that member, then the default member initialiser will be used to initialise the member of the object in question.

then can I define a member function that returns the address of this structure?

A structure is a type. A type isn't stored in an address. There is no such thing as "address of a type".


Here is an example of how to create a variable that names an instance of the class Strct:

Strct strct;

You can access the member of this variable using the member access operator:

int num = strct.number;

Here is an example of how to create an instance that doesn't use the default member initialiser:

Strct strct = {
.number = 4.2f,
};

Why uninitialized local variable in main is zero(not garbage) in c?

For the unitialized automatic local variable, the value is indeterminate. That can be anything, including 0.

Quoting C11, chapter §6.7.9, Initialization

If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. [...]

FWIW, any attempt to make use of that value, (including attempt to print), will invoke undefined behavior.

Related, from the annex J, for undefined behavior

The value of an object with automatic storage duration is used while it is
indeterminate.



Related Topics



Leave a reply



Submit