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 3490454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:31:06+00:00 2026-05-18T11:31:06+00:00

This is something the professor showed us in his scripts. I have not used

  • 0

This is something the professor showed us in his scripts. I have not used this method in any code I have written.

Basically, we take a class, or struct, and reinterpret_cast it and save off the entire struct like so:

struct Account
{
    Account()
    {   }
    Account(std::string one, std::string two)
        : login_(one), pass_(two)
    {   }

private:
    std::string login_;
    std::string pass_;
};

int main()
{
    Account *acc = new Account("Christian", "abc123");

    std::ofstream out("File.txt", std::ios::binary);
    out.write(reinterpret_cast<char*>(acc), sizeof(Account));
    out.close();

This produces the output (in the file)

ÍÍÍÍChristian ÍÍÍÍÍÍ              ÍÍÍÍabc123 ÍÍÍÍÍÍÍÍÍ     

I’m confused. Does this method actually work, or does it cause UB because magical things happen within classes and structs that are at the whims of individual compilers?

  • 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-05-18T11:31:07+00:00Added an answer on May 18, 2026 at 11:31 am

    It doesn’t actually work, but it also does not cause undefined behavior.

    In C++ it is legal to reinterpret any object as an array of char, so there is no undefined behavior here.

    The results, however, are usually only usable if the class is POD (effectively, if the class is a simple C-style struct) and self-contained (that is, the struct doesn’t have pointer data members).

    Here, Account is not POD because it has std::string members. The internals of std::string are implementation-defined, but it is not POD and it usually has pointers that refer to some heap-allocated block where the actual string is stored (in your specific example, the implementation is using a small-string optimization where the value of the string is stored in the std::string object itself).

    There are a few issues:

    • You aren’t always going to get the results you expect. If you had a longer string, the std::string would use a buffer allocated on the heap to store the string and so you will end up just serializing the pointer, not the pointed-to string.

    • You can’t actually use the data you’ve serialized here. You can’t just reinterpret the data as an Account and expect it to work, because the std::string constructors would not get called.

    In short, you cannot use this approach for serializing complex data structures.

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

Sidebar

Related Questions

When you have code like this: Something something = new Something(); BlahEntities b =
I have JSON like this: { something: http://something.com } and HTML like this: <a
This Code: Something = new Guid() is returning: 00000000-0000-0000-0000-000000000000 all the time and I
I was given some code by our professor with some things I have never
I have a table that looks something like this: word big expensive smart fast
In my Java class, the professor uses something like: integerBox.add(new Integer(10)); Is this the
I have a problem with this question from my professor. Here is the question:
Why the designers of C# did not allow for something like this? public readonly
Is this something I shouldn't try?
I'm aware of using syntax like this: (something) ? TRUE : FALSE But what

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.