导致bool输出随机数的原因是什么?

时间:2016-02-29 01:49:15

标签: c++ boolean undefined-behavior

我正在开发我的游戏,在实现中,我从项目的不同位置获得了这些代码块。我正在检查变量isCreated是否为假,将其绘制到GUI,而是,cosnole在100s的某处输出一个随机数(最近的尝试显示104和120)。代码中是否存在导致地址问题的内容?我不知道在声明布尔值时会发生这样的事情。

注意:InputMenuPtr是指向InputMenuGUI的智能指针。

编辑:isCreated输出为0,直到我进入一致性测试#3 ...

另一个编辑:这是证明事实上正在使用setter函数的代码。

InputMenuGUI testMenuGUI("custom"); //create object

InputMenuGUI(std::string s){setter(s);}; // calls this constructor

void setter(std::string s){ myStyle=s;
            isCreated=false;isClean=false;isActive=false;}

//this is the setter function.

原出......

struct InputMenuGUI{
        InputMenu *myMenu;
        ScrImage iMenu, tMenu;
        Sprite sMenu[3];
        Paragraph pMenu;
        Coord2 pos;///Pos is Top Mid of Menu Border
        bool isCreated, isClean, isActive;
        std::string myStyle;
        Button clickRange;
        std::map<int, ScrImage> screenMap;
        std::map<int, Sprite> spriteMap;
        std::map<int, Paragraph> textMap;
        std::map<int, Button> buttonMap;
        ///--------------------------------------------
        InputMenuGUI(){setter("custom");};
        InputMenuGUI(std::string s){setter(s);};
        void setter(std::string s){ myStyle=s;
            isCreated=false;isClean=false;isActive=false;}
        void display(Gorilla::Silverback *gameSB, Ogre::Viewport *gameVP, Coord2 anchorT);///->

        }

void GameApp::startInputMenu(){
        {///Escape Menu (Testing Phase)
            //Create menu, add params
            InputMenu testMenu("Really Exit?");
            testMenu.addButton(ChoiceButton("Please!",1,10));
            testMenu.addButton(ChoiceButton("I'm kiddin!",2,12));
            //create menu's gui, link ptr of menu, set menu gui ptr in map
            InputMenuGUI testMenuGUI("custom");
            std::cout<<"testing: " << testMenuGUI.isCreated << std::endl;
            testMenuGUI.myMenu=&testMenu;
            std::cout<<"testing2: " << testMenuGUI.isCreated << std::endl;
            appMenu["ExitMenu"].reset(&testMenuGUI);
            printf("Testing Consistency #%d: %d\n", 1, testMenuGUI.isCreated);
            printf("Testing Consistency #%d: %d\n", 2, appMenu["ExitMenu"]->isCreated);
        }
        ///start the input menus for GUI
    }

void GameApp::loopInput(){
    if(myKeyboard->isKeyDown(OIS::KC_ESCAPE)){
                std::cout<< "Pushing menu"<<std::endl;
                printf("Testing Consistency #%d: %d\n", 5, appMenu["ExitMenu"]->isCreated);
                myGUI.pushMenu(appMenu["ExitMenu"]);
            }
{

void GameUI::pushMenu(InputMenuPtr i){
        Coord2 tAnchor((*myGameAnchor)["T"]);
        tAnchor.add(0,(*myGameAnchor)["B"].y/4);
        printf("Testing Consistency #%d: %d\n", 4, i->isCreated);
        ///Note: when changing res, update menu positions
        ///ToDo: Movable menus. if you hold the mouse button down, have notes on the last pos to make a relative movement
        activeMenus.push_back(i);
        printf("Testing Consistency #%d: %d\n", 3, i->isCreated);
        i->display(myGameSB,myGameVP,tAnchor);
        std::cout<<"Menu pushed"<<std::endl;
    }

void InputMenuGUI::display(Gorilla::Silverback *gameSB, Ogre::Viewport *gameVP, Coord2 anchorT){
        std::cout<<"testing created: "<<isCreated<<std::endl;
        if(!isCreated){

        //...
        }

1 个答案:

答案 0 :(得分:1)

isCreated肯定是未初始化的。在这种情况下,它的值通常是当前在其内存位置的任何值。