save "state"

Discussion in 'Programmer's Corner' started by VVS, Jul 29, 2007.

  1. VVS

    Thread Starter Active Member

    Jul 22, 2007
    Hi I am have started C++ programming and I am looking for a function or something to kind of save all variables and the whole input output converstaion in a file in order to load it.
    does anybody have an idea how to do that?
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    It's hard to understand your requirement. If you need to see the state of the program as it runs, the tool to use is a debugger. You simply set breakpoints to halt instruction execution. The debugger should display the variables. Afraid I don't quite get the concept of the "whole input output conversation".
  3. Art

    AAC Fanatic!

    Sep 10, 2007
    The "input output conversation" is how the program got into it's current state.

    Timing is a part of that conversation because if you input something while the
    last routine was running, the input might have been ignored until a requestor
    popped up, or something similar.

    It appears to me that the program state is seperate, and if you save the value
    of every program variable to some media, and restore them at another run time
    the program should be in the same state.
  4. Eduard Munteanu

    Active Member

    Sep 1, 2007
    In a POSIX-compatible operating system you could send the signal SIGSTOP to stop a process and SIGCONT to unstop it. Or, as said before, you could use a debugger.

    I'm not aware of anything that can save the current state, kill the process and allow you to reload it at a future point in time. In fact, that's very likely impossible, because it would have to recreate open file descriptors, windows, memory maps and so on, which don't depend only on the kernel, but on the other libraries and applications too.
  5. Art

    AAC Fanatic!

    Sep 10, 2007
    Since he says he's starting out, it sounds like he wants to save something of
    a configuration file to be reloaded next time the program is run.
    For example the time the alarm was set to if it was a clock program.
    In which case saving the value of all variables and reloading them the next time
    the program is executed to get it into the same state is very achieveable.

    If state means something else in some c++ talk I misunderstand.
    I'm not assuming it's a Windows program, but of course I am assuming he means the program's state within itself,
    any external libraries are initialised as they would when the program is restarted and it comes time to load these settings.

    Bear in mind he's starting out. He's probably on about his third or fouth program and it needs some settings saved to a file.
  6. Eduard Munteanu

    Active Member

    Sep 1, 2007
    I was not assuming Windows or any other OS (POSIX, which I just mentioned, and Windows don't really agree with each other, do they? :)).

    VVS, anyway, there is no C/C++ function that would just save all or a group of variables to a file. You have a few options:
    1) Write a configuration/saved-state file parser and saver. You have to specify each variable by hand.
    2) The same thing, but use an existing one. For example, you could use the XML format.
    3) Have a struct hold all of them and load/save it to disk byte-by-byte. Be careful to use sizeof() to determine its size; don't just calculate it by hand, because it will get messed up by alignment issues. The downside is that the same config file won't be usable across different architectures (or even different builds of the program, with different compiler flags) because of different endianess, alignment and such. Also, two configs might not look the same even if they are functionally-equivalent, because of garbage data (alignment comes into play again).