Home > Back-end >  C reinitializing variables in a while loop
C reinitializing variables in a while loop

Time:06-29

I have a settings menu that can make the window fullscreen. Most of the menu elements are dependent on the window size. By default, all the menu elements are in the same position they were in before the window was resized (meaning they are not in the position I intend). I understand the positions needs to be recalculated, but in this instance, rather than create a function that resets the position of all the elements in the menu, I've just called the initial load() function.

Here is a code summary to help:

     while(!exit) {
      switch(State) {
        case Menu:
        
          Menu settings = Menu(...);
          settings.load() //load here

          while(State == Menu) {
            
            if (window.fullscreen()) {
              settings.load(); //reload here
            }
            
          }//end while
        break;
        default:
        break;
      }//end switch
    }//end while

This works, but I'm worried about the memory used by load() when it was first called. The variables use the same name, but are new objects created? Are they overwritten? What happened to the memory that stored the objects when I first loaded the menu?

Also, the "What" that is being loaded and reloaded here is a std::unique_ptr<Table> that makes a std::vector<std::pair<std::string, std::unique_ptr<TextRect>>>.

Edit: the Settings class:

class Settings {

  Window& _window;
  sf::RenderWindow& _win;
  //MainState& _mainState;
  MenuState& _menuState;
  Resources& _res;
  Input& _input;

  std::unique_ptr<TextRect> SettingsBanner;
  std::unique_ptr<Table> SettingsMenu;
  std::unique_ptr<Table> SettingsSave;

public:

  Settings(Window& pWindow
          ,MainState& pMainState
          ,MenuState& pMenuState
          ,Resources& pResources
          ,Input& pInput
          );
  ~Settings();

  void load();
  void update();

private:

  void inputs();

};

and the load():

void Settings::load() {

  //all kinds of variables

  SettingsBanner = std::make_unique<TextRect>(_win
                                             ,string
                                             ,font
                                             ,texture
                                             ,sf::Color::White
                                             ,size
                                             ,origin
                                             ,position
                                             ,sf::Color::White
                                             ,outColor
                                             ,charSize
                                             ,outline
                                             );

  //more variables

  SettingsMenu = std::make_unique<Table>(_win
                                        ,font
                                        ,texture
                                        ,textureColor
                                        ,fillColor
                                        ,outColor
                                        ,charSize
                                        ,outline
                                      );

  SettingsMenu->element("FullScreen");
  SettingsMenu->element("VSync");
  SettingsMenu->element("FrameRate");
  SettingsMenu->element("Volume");

  SettingsMenu->makeTable("CenterLeft", position, rowCol);

  //more variables

  SettingsSave = std::make_unique<Table>(_win
                                        ,font
                                        ,texture
                                        ,textureColor
                                        ,fillColor
                                        ,outColor
                                        ,charSize
                                        ,outline
                                        );

  SettingsSave->element("Save");
  SettingsSave->element("Back");

  SettingsSave->makeTable("BottomRight", position, rowCol);

}// end func

CodePudding user response:

In short: std::make_unique makes new objects, every time it's called. It cannot reuse the memory of the previous object. What would happen if the constructor of the new object throws an exception? It's only in the assignment to the existing unique_ptr that the old object is discarded, and you wouldn't get there if an exception is thrown. This ensures exception safety.

You can of course write Settings::load() in different ways. If SettingsBanner is not empty, you can assign to *SettingsBanner instead of calling std::make_unique.

  • Related