TripleX. Declared but undefined variable question. why not always 0 value?

Hello. I was wondering what the value of a declared but undefined variable would be. I assumed 0, so i tested. Then things got wierd.
with this code:

#include <iostream>

int main()
{
    std::cout << "Hello secret agent. Good luck breaking into the server.";
    std::cout << std::endl;
    std::cout << "Enter the correct code on your first try, or the bomb will detonate.";

    const int a = 4;
    const int b = 7;
    const int c = 3;
    int x;
    std::cout << x << std::endl;
    const int sum = a+b+c;
    const int product = a*b*c;

    std::cout << std::endl;
    std::cout << sum << std::endl;
    std::cout << product << std::endl << std::endl;
    std::cout << "haha";

    return 0;
}

Returns this:

Hello secret agent. Good luck breaking into the server.
Enter the correct code on your first try, or the bomb will detonate.6165043

14
84

haha

Why is x not zero?

But when i make a positional change as follows:

#include <iostream>

int main()
{
    std::cout << "Hello secret agent. Good luck breaking into the server.";
    std::cout << std::endl;
    std::cout << "Enter the correct code on your first try, or the bomb will detonate.";

    const int a = 4;
    const int b = 7;
    const int c = 3;


    const int sum = a+b+c;
    const int product = a*b*c;
    int x;
    std::cout << x << std::endl;
    std::cout << std::endl;
    std::cout << sum << std::endl;
    std::cout << product << std::endl << std::endl;
    std::cout << "haha";

    return 0;
}

It properly return zero, as seen in:

Hello secret agent. Good luck breaking into the server.
Enter the correct code on your first try, or the bomb will detonate.0

14
84

haha

I am baffled.

That is defined, that line both declared and defined x.

C and by extension C++ does not zero initialise variables. The following

Type Name;

Performs “default initialisation” which is a little misleading of a name as the following happens.

  • If Type is a class type then that would call the default constructor
  • If Type is an array then each element will perform default initialisation
  • Otherwise nothing is done.

So for int nothing is done and the variable is uninitialised (in technical jargon “initialised to a indeterminate value”). Reading the value of which is “undefined behaviour”(UB), quite literally anything can happen when invoked. You’ll often hear people saying missiles will be launched somewhere if you invoke UB.

Thank you for the quick response and clear explanation.

That is not what i expected, but i will keep that in mind from now on.
Seems not even a hint of a safety net in c++.
Note to self: Do not allow initialization to indeterminate values

Be well!

Well more importantly, don’t read from them. It’s not uncommon to not initialise a variable because you would overwrite the value before it’s read. e.g.

int X;
GiveXValue(X);

Where GiveXValue gives X a value via “out parameters”. This is quite common in Unreal

FVector Location;
FRotator Rotation;
GetPlayerViewPoint(Location, Rotation);

That function would assign values to Location and Rotation.
Also whilst FVector and FRotator are “class types” their default contructor does not initialise their members.

I see. Thank you. I had not considered that, but it makes sense in context.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms