473,385 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.


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
4 1451
donbock
2,426 Expert 2GB
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
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
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
Thanks for the response i corrected the code now it works !
Dec 23 '15 #5

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

Similar topics

16
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
2
by: Elizabeth | last post by:
In a visual basic.net 1.1 windows application, I need to pass required and optional values from a sql server 2000 table for a windows application to process. Each of the 250 columns in the sql...
3
by: Roland Dreier | last post by:
I'm used to initializing struct fields by name using the designator syntax that is a part of ISO C99. For example: struct foo { int x; int y; int z; }; struct foo a = {
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
3
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
3
by: AVL | last post by:
Hi, I'm new to .net. I need some info on serialization. What is serialization? Why do we need it? Why objects need to be serialized if they need to be stored in session or viewstate?
28
by: WaterWalk | last post by:
Hi, I'm haunted by 2 questions about struct copy. Though I searched the net, but still in confusion. 1. Does struct assignment copies every member including array members? For example, struct...
2
!NoItAll
by: !NoItAll | last post by:
Reading XML in VB.NET is pretty straightforward. Create an XML Document Object, then you can load the XML and iterate through all of the nodes as required. You can search for nodes, use x-path,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.