Home > database >  Are they actually garbage values or just the default values of enumeration if I don't initializ
Are they actually garbage values or just the default values of enumeration if I don't initializ

Time:07-19

I have branch positions that are not initialized to any value of side enumeration. However, when I try to draw them to the screen, they're actually on the same side, on the left side of the tree. I think those uninitialized branch positions are garbage values, a remnant from the memory that the PC has been used or hasn't been used. I'm not quite sure every time I run the program, they're always on the left side, which seems consistent, which is the first value in the side enumeration, which I assumed to be 0. Are they actually garbage values or just the default values of enumeration if I don't initialize them?

// Include important libraries here
#include <SFML/Graphics.hpp>
// Make code easier to type with "using namespace"
using namespace sf;
// Function declaration
void updateBranches(int seed);
const int NUM_BRANCHES = 6;
Sprite branches[NUM_BRANCHES];
// Where is the player/branch?
// Left or Right
enum class side { LEFT, RIGHT, NONE };
side branchPositions[NUM_BRANCHES];
// This is where our game start from
int main()
{
    // Create a video mode object
    VideoMode vm(1920, 1080);
    // Create and open a window for the game
    RenderWindow window(vm, "Timber!!!", Style::Fullscreen);
    // Track whether the game is running
    bool paused = true;
    while (window.isOpen())
    {
        /*
        ****************************************
        Handle the player's input
        ****************************************
        */
        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
            window.close();
        }
        // Start the game
        if (Keyboard::isKeyPressed(Keyboard::Enter))
        {
            paused = false;
            // Reset the time and the score
            score = 0;
            timeRemaining = 6;
        }
        /*
        ****************************************
        Update the scene
        ****************************************
        */
        if (!paused)
        {
            // update the branch sprites
            for (int i = 0; i < NUM_BRANCHES; i  )
            {
                float height = i * 150;
                if (branchPositions[i] == side::LEFT)
                {
                    // Move the sprite to the left side
                    branches[i].setPosition(610, height);
                    // Flip the sprite round the other way
                    branches[i].setRotation(180);
                }
                else if (branchPositions[i] == side::RIGHT)
                {
                    // Move the sprite to the right side
                    branches[i].setPosition(1330, height);
                    // Set the sprite rotation to normal
                    branches[i].setRotation(0);
                }
                else
                {
                    // hide the branch
                    branches[i].setPosition(3000, height);
                }
            }
        } // End if(!paused)
        /*
        ****************************************
        Draw the scene
        ****************************************
        */
        // Clear everything from the last frame
        window.clear();
        // Draw the branches
        for (int i = 0; i < NUM_BRANCHES; i  )
        {
            window.draw(branches[i]);
        }
        // Show everything we just drew
        window.display();
    }
        return 0;
}

CodePudding user response:

Uninitialized (non-static) local variables are not initialized.

Uninitialized variables in namespace scope (including the global namespace, like your branchPositions array) are actually "zero" initialized.

So all elements of the branchPositions array should be zero, which is equal to LEFT.

CodePudding user response:

Turn all warnings on and check what the compiler says. If your compiler complains then your code is very likely wrong.

It seems you are relying on the fact that static variables are initialised to zeroes. And by chance zero corresponds to one enumeration value. You are living very dangerous. You should usually be able to change the order of enumeration, but you can’t. Change the enum to LEFT = 1 and everything will break down.

In C it is defined that enum values start with 0 unless you define it otherwise, and each one is 1 more than the previous one unless you state otherwise. That’s something you can rely on. But you are relying on how variables are initialised and that is very, very dangerous.

  • Related