Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've recently read three separate books on algorithms and data structures, tcp/ip socket programming, and programming with memory. The book about memory briefly discussed the topic of serializing data structures for the purposes of storing it to disk, or sending it across a network. I can't help but wonder why the the other two books didn't discuss serialization at all.

After an unsuccessful web/book search I'm left wondering where I can find a good book/paper/tutorial on serializing data structures in C? Where or how did you learn it?

share|improve this question
I read that one too. He's looking for a library, I'm looking for the knowledge to do it myself. Also, he's not interested in data portability, and that is exactly what I need if for. Thx for the link though. – Nocturno Oct 11 '12 at 20:28

4 Answers

Have a look at the work Google has done with Protocol Buffers.

You write a .proto file like this:

message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}

Then you compile it with protoc, the protocol buffer compiler, to produce code in C++, Java, or Python.

Then, if you are using C++, you use that code like this:

Person person;
person.set_id(123);
person.set_name("Bob");
person.set_email("bob@example.com");

fstream out("person.pb", ios::out | ios::binary | ios::trunc);
person.SerializeToOstream(&out);
out.close();

You can examine the SerializeToOstream method to understand how Google generates the serialization code. Yes, it's C++ code, but it should still be pretty close to C code.

share|improve this answer
That doesn't really tell anyone how to do it themselves. – James Oct 11 '12 at 20:35
1  
Read the last sentence in the answer. – Robert Harvey Oct 11 '12 at 20:36
Read it, still have no idea how to do it without a link to the actual function. Spent too much time looking for it. – James Oct 11 '12 at 20:39
The SerializeToOstream function is code-gen'd by the Protobuf compiler. You have to generate the method first, based on your data template. Then you'll have something to look at. – Robert Harvey Oct 11 '12 at 20:41
@RobertHarvey Just read about it and it is very interesting. I'll definitely keep this in mind. Thx. – Nocturno Oct 11 '12 at 20:42
show 1 more comment

C has no native support for serializing structures, so you're on your own.
The first order approximation is (as stated in other replies) to define it for primitive types, and apply it recursively to larger structures.

However, there are lots of devilish details that have to be addressed beyond the simple concept. To name a few:

  • endian order of integers, and sizes of various common types of integers depending on machine architectures. This isn't much of a problem if all the consumers of serializatin are the same binary, but consider reading data produced by a 32 bit PPC Mac on a 64 Bit Windows machine, or if a "long" is 32 or 64 bits.
  • Different representations for common data types. Color bitmaps have 3 components on a PC, but 4 components, in a different order, on Macs
  • Representation and precision of floating point numbers.
  • If strings with the same letters are idential or only similar.
  • dealing with cyclic or self-referential data structures.
share|improve this answer

Serialization is usually pretty simple and recursive. You just figure out what fields you need to send before the actual data so you can reconstruct the structure at the other end. Issues would mainly be endianness. Learn it by trial and error, I doubt you need a book for this sort of thing.

share|improve this answer
Do you sort of mean like converting the data to XML, then sending that over the wire? At the moment, I'm more interested removing the padding from the structure and sending only the binary data, not a lot of extra XML or formatting. And yes, the endianness! It's little things like that, and relativization, and absolutization that I need to get a foothold on. – Nocturno Oct 11 '12 at 20:52
I personally detest XML. Try starting with arrays, all you need to do is send the number of elements (n), and then send n elements. – James Oct 11 '12 at 21:55

The Wikipedia article Serialization covers the topic fairly well, though oddly it does not mention ASN.1 which is a widely hated, but extremely well defined and well known standard for describing efficient data serialization protocols. ASN compilers typically generate code (e.g. C code) for encoding and decoding the described data structures in a canonical way.

BTW, the endian issue can be trivially dealt with in C, as Rob Pike has shown nicely in his article The Byte Order Fallacy, though some C compilers don't yet always generate the most optimal object code when using this technique.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.