Basic struct serialization operation on C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akadayifci
    New Member
    • Dec 2015
    • 11

    Basic struct serialization operation on C

    I would like to store my struct data by converting as a char typed data. In order to do that I've tried to write a serialization program but whenever i debug i see different values on my data structure. Could some expert can make a comment on this i can't see what i am missing here.

    Thanks in advance.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <signal.h>
    #include <assert.h>
    #include "limits.h"
    
    #define BUFFERSIZE 256
    #define PACKETSIZE sizeof(cloudMessage)
    
    
    typedef struct cloudMessage
    {
        int      station_id;
        int      location_area;
        char     command[BUFFERSIZE];
    
    }cloudMessage;
    
    void serialize(cloudMessage *message, char *data);
    void deserialize(char *data, cloudMessage *message);
    void printMyMessage(cloudMessage* message);
    
    void serialize(cloudMessage *message, char *data)
    {
        data = (char*)malloc(sizeof(message));
        assert(data != NULL);
        memcpy(data, &message, sizeof(message));
    }
    
    void deserialize(char *data, cloudMessage *message)
    {
        memset(message, 0, sizeof(data));
        memcpy(message, &data, sizeof(cloudMessage));
    }
    
    void printMyMessage(cloudMessage *message)
    {
        
        printf((void*)message->location_area);
        printf((void*)message->station_id);
        printf((void*)message->command);
        
    }
    int main()
    {
        cloudMessage *newcloudMessage = malloc(sizeof(cloudMessage));
        newcloudMessage->location_area = 7214;
        newcloudMessage->station_id = 45632;
        strcpy(newcloudMessage->command, "HANDOVER\0");
        char data[PACKETSIZE];
        serialize(newcloudMessage, data);
        cloudMessage *tempMessage = malloc(sizeof(cloudMessage));  // To store deserialized message.
        deserialize(data, tempMessage);
        printMyMessage(tempMessage);
        
        return 0;
    }
    Last edited by zmbd; Dec 8 '15, 12:18 AM. Reason: [z{please format script using the [CODE/] tool }]
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The first argument to printf is a string specifying the format of the data to be printed -- but you're passing the data itself as the first argument.

    Comment

    • akadayifci
      New Member
      • Dec 2015
      • 11

      #3
      Thanks for the answer, but while i am debugging my code i can't recover elements of my struct data. Type casting should work for serialization.

      Comment

      • hpmachining
        New Member
        • Oct 2015
        • 15

        #4
        I see a couple of things in serialize. First, you have already allocated space on the stack in main for data so don't use malloc. Second, sizeof(message) returns the size of the pointer, not the size of the data it points to. To get the size of the data you can use sizeof *message instead. The following should work for serialize:
        Code:
        void serialize(cloudMessage *message, char *data)
        {
          assert(data != NULL);
          memcpy(data, message, sizeof *message);
        }
        In deserialize, I don't think memset is doing what you want it to do because of sizeof(data). I think you probably want sizeof(cloudMes sage) instead.
        Also, as donbock pointed out, your printf calls are missing a format specifier.
        Code:
        void printMyMessage(cloudMessage *message)
        {
          printf("%d\n", message->location_area);
          printf("%d\n", message->station_id);
          printf("%s\n", message->command);
        }

        Comment

        • akadayifci
          New Member
          • Dec 2015
          • 11

          #5
          Thanks for the response i corrected the code now it works !

          Comment

          Working...