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
How to Make Cin Take Only Numbers
Which Is Faster: Stack Allocation or Heap Allocation
Is Std::Unique_Ptr≪T≫ Required to Know the Full Definition of T
How to Pass a Member Function Where a Free Function Is Expected
Difference Between Char A[] = String; and Char *P = String;
Why Do C and C++ Compilers Allow Array Lengths in Function Signatures When They'Re Never Enforced
How to Use Boost in Visual Studio 2010
Why How to Not Push_Back a Unique_Ptr into a Vector
Why Isn't It Legal to Convert "Pointer to Pointer to Non-Const" to a "Pointer to Pointer to Const"
How to Link to a Library With Code::Blocks
What Are Access Specifiers? Should I Inherit With Private, Protected or Public
Optimizing Away a "While(1);" in C++0X
How to Serialize an Object in C++
Store Derived Class Objects in Base Class Variables
How to Use Cout ≪≪ Myclass
Calling Class Method Through Null Class Pointer