Monday, April 11, 2011

Second assignment posted… and done! Let me introduce you to Serialization!

Sup dudes!

Week #2 is over, second assignment is posted and I already finished it, sweet! This is a new for me. I am usually (99% of the time) the kind of guy that leaves everything for the last minute (not that I am proud of, but I am honest at least).

The assignment is due this Friday 4/15 and was about Serialization, which basically is the process of converting data structures or objects into a format that can be stored in a buffer so that it can be saved to a file, transmitted over a network, and many other options.

The task was to take four (4) classes already defined by the professor and implement their respective serialize and deserialize methods. In order to test what we are doing, we were given a test unit that does the following: creates an object for each one these classes, serializes them to buffers in memory, then creates new empty objects for each class, reconstructs the former objects from these buffers and finally compares the objects values (members) to check for inconsistencies.

Here is a little description of the classes so you have an idea of what kind of data we were handling:
  • Cat (1 double, 2 floats, 1 int, and 3 chars): serialize/deserialize was just a memcpy to/from buffer with length equal to sizeof(Cat).
  • Dog (2 int, 1 float and 1 char): same procedure as Cat.
  • Bird (1 int and 1 char*): when serializing I copy the int, one extra int to store the length of the c-string and the actual char array. Therefore, when deserializing I just copy the int back, read the length of the c-string and then read the respective number of chars from the buffer to reconstruct the object.
  • Fish (1 int, 1 float, 1 char, 1 apple* and 1 orange*): this one might seem complicated, but is not. Basically this one is done by using memcpy three (3) times: one for the actual fish data (sizeof(Fish)-2*sizeof(void*)), one for the apple and another one for the orange.
Well my fellow readers, there you have it. My second assignment is done and explained to you. Next week project should be about Sockets. I will be back as soon as it gets posted.

Adios!

1 comment:

  1. Hey Diego,

    I just wanted to say I appreciated the advice for the fish, it was very helpful. Had to toy with the idea for a while but in the end it gave me the idea for the methods. Thanks!

    ReplyDelete