纯C / C ++中的二进制序列化 [英] Binary serialization in pure C/C++

查看:189
本文介绍了纯C / C ++中的二进制序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自己实现二进制序列化,而不使用 Boost 或任何其他第三方库。

I'd like to implement the binary serialization on my own, without using Boost or any other third-party library.

在C ++中,实现它的最简单的方法是使用 ofstream ,然后通过网络发送一个二进制文件。但是有没有其他流类,我可以使用作为临时缓冲区,以避免写入文件到磁盘?

In C++ the simpliest way to achieve it is to use ofstream and then send a binary file over network. But is there any other stream class which I can use as a temporary buffer to avoid writing file to disk?

此外,如何在纯C? p>

Also, how can I achieve that in pure C?

推荐答案

持久性很难。将对象序列化到磁盘并不是微不足道。比如说,你在C中有这样的结构:

Persistence is hard issue. It is not trivial to even serialize an object to disk. Say that, for example, you have a structure like this one in C:

struct Person {
    char name[100];
    int year;
};

这是一个sef包含的结构,可能是最简单的方法,序列化真正可以应用。但是,您必须面对以下问题:

This is a sef-contained structure, probably the simplest way in which serialization can really be applied. However, you'll have to face the following problems:


  1. 编译器的填充系统。

  1. The compiler's padding system. The way to complete a structure in memory so it occupies a whole number of words in memory is not standard.

操作系统和机器本身代表的方式二进制形式的数据。显然,这种表示从一台机器改变到另一台机器。

The way the operating system and the machine itself represents data in binary form. Obviously, this representation changes from one machine to another one.

结论是,在同一操作系统中的程序可能与同一操作系统中的同一程序不兼容,因为这两个程序都是用不同的C编译器编译的。

The conclusion is that a file created even by the same program in the same operating system may not be compatible with the same program in the same operating system, because maybe both programs were compiled with different C compilers.

现在让我们看看C ++中的对象:

Now let's see an object in C++:

class Person {
public:
    // more things...

private:
    string name;
    Date * birth;
    Firm * firm;
};

现在,同样的事情变得非常复杂。对象不再是自包含的,你应该遵循指针,以决定如何处理每个对象(这被称为3.指针交换和传递持久性)。你还有1)和2)的问题。

Now the very same thing has become really complex. The object is no more self-contained, you should follow the pointers in order to decide how to deal with each object (this is called 3. pointer swizzling and transitive persistence). And you still have 1) and 2) problems.

所以让我们说你专注于自包含的对象,仍然需要一个点1& 2.唯一的方法是以a)文本格式或b)字节码格式来决定表示。字节码格式可以被任何操作系统中的任何程序理解,用任何C编译器编译,因为信息是逐字节读取和写入的。这是Java或C#将其对象序列化的方式。文本格式作为表示与字节码一样有效,但速度较慢。它的主要优点是它可以被人类以及计算机(结构化文本格式可以是XML)理解。

So let's say that you focus on self-contained objects, and still need a solution for points 1 & 2. The only way to go is to decide a representation in either a) text format or b) bytecode format. Bytecode format can be understood by any program in any operating system, compiled with any C compiler, because the information is read and written byte by byte. This is the way that Java or C# serialize their objects. Text format as a representation is as valid as bytecode, though slower. Its main advantage is that it can be understood by a human being as well as the computer (a structured text format could be XML).

因此,为了序列化你的自包含对象,但是选择的输出格式,你需要有能够读取int,chars,字符串等的基本函数(或C ++中的类)。当你有每对的写/读对时,你必须为程序员提供为她的对象创建自己的写/读对的可能性,使用读/写对元素数据。

So, in order to serialize your self-contained objects, however the output format chosen, you need to have basic functions (or classes in C++) that are able to read ints, chars, strings, and so on. When you have the write/read pairs for each one, you'll have to provide the programmer with the possibility to create her own write/read pairs for her objects, using you read/write pairs for elemental data.

我们在这里谈论一个完整的框架,就像Python提供的 pickle 模块。

We are talking here about a complete framework, something like what Python offers with its pickle module.

最后,能够缓存序列化而不是将其保存到磁盘的事实是您的问题的最少。如果使用基于文本的格式,则可以使用 ostringstream 类;如果使用字节码,则可以使用内存块。

Finally, the fact of being able to cache your serialization instead of saving it to disk, is the least of your problems. You could use the ostringstream class if you are using a text-based format, or a memory block if you are using bytecode.

你可以看到,这不是一个简单的工作。
希望这有帮助。

As you can see, it is not a simple job. Hope this helps.

这篇关于纯C / C ++中的二进制序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆