Saving data at runtie

Hi, is there a way that is considered best to save data at runtime in C++ program? For example, we want to sort a vector of integers and during sorting, we want to save and remember every iteration so it can be used later. Or we want to remember some user inputs.
Is it external file where we should save that data or some static variables?
I would really appreciate some good answers.
If you mean within the same program run (i.e. not after the program ends and then starts again), the best practice is to have a variable, and to set that variable with the data you need, and then later when you need that data, use that variable.

Here's a simple example:

1
2
3
4
5
6
int x;
x = some_function();  // going to need this data later.

// LATER

some_other_function(x);


They don't have to be static. Just design your program so that data is held somewhere sensible so you can use it when you need it.
Last edited on
Topic archived. No new replies allowed.