2

In a large complex C program, I'd like to save to a file the contents of all memory that is used by static variables, global structures and dynamically allocated variables. Those memory variables are more than 10,000.

The C program has only single thread, no file operation and program itself is not so complex (calculation is complex).

Then, in a same execution of the program, I want to initialize the memory from this saved state.

If this is even possible, can someone offer an approach to accomplish this?

user2775054
  • 33
  • 1
  • 6

1 Answers1

2

You have to define a Struct to keep al your data in and then you have to implement a function to save it into a file.

Something like this: Saving struct to file

Please note, however, that this method is the simplest, but comes with no portability at all.

Edit after Comment: basically, what you would like to do is save whatever is happening in the program and then restart it after a load. I don't think this is possible in any simple way. You MUST understand what "status of your application" means. Think about it: doing a dump of the memory saves not only the data, but also the current Instruction Pointer. So, with that "dumb" dump, you would have also saved the actual instruction currently running. And many more complications you really don't want to care about.

The closest thing you are thinking about is running the program in a Virtual Machine. If you pause the VM the execution status will be "saved", but whenever you restart the VM, the program will restart at the exact same execution point you paused it.

If the configurations are scattered through the application, still you can access a global struct used to save everything. But still you have to know your program and identify what you have to save. No shortcuts on that.

Community
  • 1
  • 1
Alberto Chiesa
  • 7,022
  • 2
  • 26
  • 53
  • Thanks for your answer. But what I want to know is how to define a Struct to keep all my data. Variables I want to save is scattered across hundreds of functions. So I think if I can save and load stack area or heap area memory, it can be easier but I couldn't find the solution how can. – user2775054 Oct 01 '13 at 00:36
  • You certainly can try to save the stack and the heap to a file, but that doesn't account for static/global data. – James M. Lay Oct 18 '20 at 03:41