3

Is there any way in C++ or Java or Python that would allow me to save the state of my program, no questions asked? For example, I've spent an hour learning how to save a tree-like structure into a file. Very educative but I feel I could just do:

saveState(file);

And the "file" would contain whole memory my program uses. Just like operating system's "hibernate" or "suspend-to-disk" feature. I know about boost serialization, this is probably not what I'm looking for.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
Matthew
  • 129
  • 2
  • 8
  • 1
    Question is off topic anyway, but to tag it with java, c++ and python? – John3136 Feb 28 '13 at 01:04
  • A rule of thumb - if you can't narrow it down to a single programming language, it is *very* likely to elicit debate, which isn't what Stack overflow was built for. – Hannele Feb 28 '13 at 13:45
  • I know I got conned into this Q. because about the only time multiple language tags are OK are when IPC or native foreign functions involved which I thought the Q. was about... – Adam Gent Feb 28 '13 at 13:56

5 Answers5

2

What you most likely want is what we call serialization or object marshalling. There are a whole butt load of academic problems with data/object serialization that you can easily google.

That being said given the right library (probably very native) you could do a true snapshot of your running program similarly what "OS specific hibernate" does. Here is an SO answer for doing that on Linux: https://stackoverflow.com/a/12190830/318174

To do the above snapshot-ing though you will most likely need an external process from the process you want to save. I highly recommend you don't that. Instead read/lookup in your language of choice (btw welcome to SO, don't tag every language... that pisses people off) how to do serialization or object marshalling... hint... most people these days pick JSON.

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
1

I think that what you describe would be a feature that few people would actually want to use for a real system. Usually you want to save something so it can be transmitted, or so you can stop running the program, or guard against the possibility that the program quits (or power fails).

In most production systems one wants to make the writes to disk small and incremental so that the system can remain responsive, and writing inconsistent data can be avoided. Writing ALL memory to disk on a regular basis would probably result in lots of non-responsive time. You would need to lock the entire system to avoid inconsistent state.

Writing your own persistence is tedious and error prone however so you may find this SO question of interest: Persisting graph data (Java)

Community
  • 1
  • 1
Gus
  • 6,719
  • 6
  • 37
  • 58
1

There are a couple of frameworks around this. Check out Google Protocol Buffers if you need support for Java, Python, and C++ https://developers.google.com/protocol-buffers/ I've used it in some projects and it works well.

There's also Thrift (orginally from Facebook) http://thrift.apache.org/ I don't have any experience with it though.

Another option is what @QuentinUK suggests. Use a class that inherits from something streamable and/or make streamable operators/functions.

I'd use a framework.

Joel
  • 2,928
  • 2
  • 24
  • 34
1

Here's your problem:

Back in ancient history (16-bit DOS programs with extenders), compilers used to support "based" pointers which stored relative addresses. These were safe to serialize en masse. And applications did so, saving both code and data, the serialized modules were called "overlays".

Today, you'd need based pointer support in your toolchain (resulting in every pointer access requiring an extra adjustment), or else to go through all the data, distinguishing the pointers from the other data (how?) and adjusting them to their new storage location, in case the OS already loaded some library at the same address your old program had used for its heap. In modern "managed" environments, where pointers already have to be identified for the garbage collector, this is feasible even if not commonly done. In native code, it's very difficult, although that metadata is created to enable relocation of shared libraries.

So instead people end up walking their entire data structures manually, and converting object links (pointers) into something that can be restored on the other end, even though the object has a new address (again, because the old address may have been used for a shared library).

Note that many processors have features to support based addressing... and that since based addressing is no longer common, compilers went ahead and used those pointer arithmetic features to speed up user code.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Yes, derive objects from a streamable class and add the streaming functions. Then you can stream everything to disk. You will need a library for this such as MFC.

QuentinUK
  • 2,997
  • 21
  • 20