By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Basic struct serialization operation on C

P: 11
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.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <assert.h>
  6. #include "limits.h"
  7.  
  8. #define BUFFERSIZE 256
  9. #define PACKETSIZE sizeof(cloudMessage)
  10.  
  11.  
  12. typedef struct cloudMessage
  13. {
  14.     int      station_id;
  15.     int      location_area;
  16.     char     command[BUFFERSIZE];
  17.  
  18. }cloudMessage;
  19.  
  20. void serialize(cloudMessage *message, char *data);
  21. void deserialize(char *data, cloudMessage *message);
  22. void printMyMessage(cloudMessage* message);
  23.  
  24. void serialize(cloudMessage *message, char *data)
  25. {
  26.     data = (char*)malloc(sizeof(message));
  27.     assert(data != NULL);
  28.     memcpy(data, &message, sizeof(message));
  29. }
  30.  
  31. void deserialize(char *data, cloudMessage *message)
  32. {
  33.     memset(message, 0, sizeof(data));
  34.     memcpy(message, &data, sizeof(cloudMessage));
  35. }
  36.  
  37. void printMyMessage(cloudMessage *message)
  38. {
  39.  
  40.     printf((void*)message->location_area);
  41.     printf((void*)message->station_id);
  42.     printf((void*)message->command);
  43.  
  44. }
  45. int main()
  46. {
  47.     cloudMessage *newcloudMessage = malloc(sizeof(cloudMessage));
  48.     newcloudMessage->location_area = 7214;
  49.     newcloudMessage->station_id = 45632;
  50.     strcpy(newcloudMessage->command, "HANDOVER\0");
  51.     char data[PACKETSIZE];
  52.     serialize(newcloudMessage, data);
  53.     cloudMessage *tempMessage = malloc(sizeof(cloudMessage));  // To store deserialized message.
  54.     deserialize(data, tempMessage);
  55.     printMyMessage(tempMessage);
  56.  
  57.     return 0;
  58. }
Dec 7 '15 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 2,400
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.
Dec 7 '15 #2

P: 11
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.
Dec 8 '15 #3

P: 15
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:
Expand|Select|Wrap|Line Numbers
  1. void serialize(cloudMessage *message, char *data)
  2. {
  3.   assert(data != NULL);
  4.   memcpy(data, message, sizeof *message);
  5. }
  6.  
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(cloudMessage) instead.
Also, as donbock pointed out, your printf calls are missing a format specifier.
Expand|Select|Wrap|Line Numbers
  1. void printMyMessage(cloudMessage *message)
  2. {
  3.   printf("%d\n", message->location_area);
  4.   printf("%d\n", message->station_id);
  5.   printf("%s\n", message->command);
  6. }
  7.  
Dec 11 '15 #4

P: 11
Thanks for the response i corrected the code now it works !
Dec 23 '15 #5

Post your reply

Sign in to post your reply or Sign up for a free account.