Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:26:11+00:00 2026-06-05T20:26:11+00:00

I know how to serialize an object in the following way: void encodeMsg(char **msg,

  • 0

I know how to serialize an object in the following way:

void encodeMsg(char **msg, const ConnectionParams& params)
{
    std::ostringstream oss;
    if (!(oss << params))
    {
        //failure
    }
    msg = oss.str();
}

how can i deserialize a message back into the object?

meaning i want to have this method:

ConnectionParams encodeMsg(char *msg)
    { ... }

so i don’t have an instringstream object… how can i do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:26:12+00:00Added an answer on June 5, 2026 at 8:26 pm

    Something like:

    ConnectionParams decodeMsg(char *msg)
    {
      ConnectionParams ret;
      std::istringstream iss(msg);
      iss >> ret;
      return ret;
    }
    

    That is assuming that this class was made to be serializable with operator<<, and provides the symmetrically opposite operation operator>>. There is no way to serialize an arbitrary C++ object without knowing its internals, and in this answer I am assuming these operation were already implemented by the class provider.

    Also, note that you should probably pass ConnectionParams as reference, like:

    void encodeMsg(char *msg, ConnectionParams& ret);
    

    Also, your encodeMsg is wrong, has better chance to work as:

    void encodeMsg(char **msg, const ConnectionParams& params)
    {
        ....
        *msg = oss.str();
    }
    

    and even this is wrong, since oss.str() returns an internal pointer from oss, and you can’t return it to the caller, because oss will be destroyed upon function return, and the pointer will be invalid. You’d better return a std::string or something like that.

    EDIT:
    What if the class does not provides the means to serialize and deserialize itself? On classes that are simple enough, and don’t hold pointers allocated on heap, nor file descriptors, nor have fancy behaviors on constructors and destructors, you may be able to do it the same way done with old C structs: interpret all its bytes as a buffer. Note that this is dangerous, type unsafe, and the buffer may not be inter-operable between different compilers/architectures.

    // DISCLAIMER: Untested code!
    
    class A {
      int plain_variable;
      char palin_array[50];
    };
    // Class A is ok to serialize this way
    
    class B {
      A plain_object_array[10];
      double another_plain_variable;
    };
    // Class B is ok to serialize this way
    
    class C {
      float plain_object;
      std::string fancy_string;
    };
    // Class C is *NOT* ok to serialize this way, because it contains an string,
    // which, in turn, contains a heap allocated pointer
    
    template<class T>
    void serialize(const T& obj, std::string& buff)
    {
      const char *accessor = (const char *)&obj;
      std::string tmp(accessor, sizeof(obj));
      std::swap(buff, tmp);
    }
    
    template<class T>
    void deserialize(const std::string& buff, T& obj)
    {
      char *accessor = (char *)&obj;
      std::copy(buff.begin(), buff.end(), accessor);
    }
    

    See, the conditions for this scheme to work are so that it is unlikely you will be able to use it with a third-party provided class. In the end, you will probably need to store all the data needed to rebuild the object yourself, and work on this data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me know the best way to serialize my Java object Download. This is
I'm tryng to serialize an object to XML by using C# with the following
I am receiving the following exception when trying to serialize an object using XMLSerialization.
I am curious to know what happens when we de-serialize an object. For example
I know it is possible to serialize and unserialize in PHP and then have
I want to know whether the PHP serialize function is 100% secure, also if
Given a template template <int n> void f(){...}; I know I can specialize it
I'm trying to serialize and deserialize an array list with a object inside: HairBirt
I am getting the following error in my code: System.JSONException null Don't know the
Can anyone explain why the following is happening: When we serialize a file in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.