title: Data (De)serialization in C
body: <p>I was wondering if someone could possibly help me with my problem.
  In client side, I have a linked list which each item contains an integer number. Suppose I appended 5 integer (let's say 1, 2, 3, 4, 5). I checked and the routine for creating and appending of integers to the linked list is OK. After that, I want to serialize the linked list via below code:</p>
  
  <pre><code>typedef struct ser_buff_ {
    void *b;
    int size; // total size of the buffer
    int next; // length of the contained data 
  }ser_buff_t;
  
  void serialize_linkedlist(table_t *table, ser_buff_t *b){
    table_entry_t *head = table-&gt;next;
    while(head){
      int temp = head-&gt;data;
      serialize_data(b, (char*)&amp;temp, sizeof(int));
      head = head-&gt;next;
    }
    unsigned int sentinel = 0xFFFFFFFF;
    serialize_data(b, (char*)&amp;sentinel, sizeof(unsigned int)); // mark the tail
  }
  </code></pre>
  
  <p>table refers to the first item in the linked list and b is the buffer in which I want to serialize the linked list. As you may see, the function serialize_data() is invoked. The function is like below:</p>
  
  <pre><code>void serialize_data(ser_buff_t *b, char *data, int nbytes){
    if(b == NULL) assert(0);
    ser_buff_t *buff = b;
    int available_size = buff-&gt;size - buff-&gt;next;
    int isResize = 0;
  
    while(available_size &lt; nbytes){
      buff-&gt;size *= 2;
      available_size = buff-&gt;size - buff-&gt;next;
      isResize = 1;
    }
    if(isResize == 0){
      memcpy((char*)buff-&gt;b + buff-&gt;next, data, nbytes);
      buff-&gt;next += nbytes;
      return;
    }
    buff-&gt;b = realloc(buff-&gt;b , buff-&gt;size);
    memcpy((char*)buff-&gt;b + buff-&gt;next, data, nbytes);
    buff-&gt;next += nbytes;
    return;
  }
  </code></pre>
  
  <p>After serializing the linked list, I send the buffer to the server via below code:</p>
  
  <pre><code>ret = write(data_socket, b, b-&gt;next);
  </code></pre>
  
  <hr>
  
  <p>Here in server side, I receive the serialized buffer via socket.</p>
  
  <pre><code>ret = read(data_socket, b, 128);
  </code></pre>
  
  <p>I am sure that <code>b-&gt;next</code> in client side is less than 128. After receiving the serialized data from the client, the server passes it to de-serializaion function:</p>
  
  <pre><code>void de_serialize_linkedlist(ser_buff_t *b, table_t *table){
    int i = 1;
    unsigned int sentinel = 0;
    while(i){
      de_serialize_data((char*)&amp;sentinel, b, sizeof(unsigned int));
      if(sentinel == 0xFFFFFFFF){
        break;
      }
      else{
        serialize_buffer_skip(b, sizeof(unsigned int)* (-1));
        de_serialize_data((char*)&amp;i, b, sizeof(int));
        add(table, i);
      }
    }
  }
  </code></pre>
  
  <p>inside this function, there is another function de_serialize_data() is called:</p>
  
  <pre><code>void de_serialize_data(char *dest, ser_buff_t *b, int nbytes){
    if(!b || !b-&gt;b) assert(0);
    if(!nbytes) return;
    if((b-&gt;size - b-&gt;next) &lt; nbytes) assert(0);
    memcpy((char*)dest, (char*)b-&gt;b + b-&gt;next, nbytes);
    b-&gt;next += nbytes;
  }
  </code></pre>
  
  <p>But I got set fault when I reach the line </p>
  
  <pre><code>memcpy((char*)dest, (char*)b-&gt;b + b-&gt;next, nbytes);
  </code></pre>
  
  <p>I assume <code>b-&gt;b</code> is not accessible which is not my expectation. Could anyone please help me. </p>