Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving objects/serialization

Tags:

c#

I'm trying to make the transition from C to C# and I have a few questions about saving objects to files and serialization.

In C if you want to save a data structure, I've been taught that saving it in a text format as a string is often unnecessary and a binary file that exist as a memory snapshot is often better because it doesn't need encoding/decoding and matching strings to fields. In C# the approach appears to be different, it converts object fields separately to a string or some other format and then it reconstructs the object when necessary. I'm not sure how binary serialization works but I think it sill converts the data to some format and doesn't exist as a pure non-formatted memory snapshot.

Why is the "memory snapshot" method without any encoding/decoding not used in C# ? The only reason I can think is compatibility with other code and environments, and maybe it has to do with the complexity of objects vs regular structures.

like image 600
user3604097 Avatar asked Jul 02 '15 13:07

user3604097


2 Answers

In C# you don't have access to the memory layout of an object. You can't get the memory snapshot of an object, or create an object from the snapshot. In fact, you don't even have points (yes, you have them in unsafe code, but you rarely ever write unsafe code).

Even if you did have access to the memory used by the object, it might not survive being saved to disk and then reloaded. If you upgrade the .NET environment, you may get a better optimizer that decides to reorder the object's fields differently, or use different memory alignment.

So in short - no access to object memory, so you need to serialize field by field.

The upside is, that since .NET has reflection, serializing an object this way is not hard. In fact, it's a lot easier than serializing a C++ class that holds pointers to other C++ classes.

like image 133
zmbq Avatar answered Oct 09 '22 00:10

zmbq


The .NET binary serialization format defers to the types how they would like to be stored and puts in some meta-data so that you know how to decode it (which type, what field name, etc).

This is not primarily for interop with .NET languages -- although it could be. It is for interop with past and future versions of the same objects, which can be written in a way to be resilient to those changes -- or at least know to reject them.

In C, if you change a struct in any way -- and you have just used write() to store the memory, you would probably also opt to write some meta-data so that you could know if you had the right version (and needed to convert).

Another benefit is that the .NET languages know what to do with references. By default, C would write() an address, which would be useless later. .NET also supports the idea of fields that shouldn't be serialized (like a password) -- another thing you'd need to hand-write in the C version.

like image 36
Lou Franco Avatar answered Oct 08 '22 23:10

Lou Franco