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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:08:54+00:00 2026-05-29T07:08:54+00:00

I am trying to serialize a struct, but the program crashed with: *** glibc

  • 0

I am trying to serialize a struct, but the program crashed with:

*** glibc detected *** ./unserialization: double free or corruption (fasttop): 0x0000000000cf8010 ***

#include <iostream>
#include <cstdlib>
#include <cstring>

struct Dummy
{
    std::string name;
    double height;
};

template<typename T>
class Serialization
{
    public:
        static unsigned char* toArray (T & t)
        {
            unsigned char *buffer = new unsigned char [ sizeof (T) ];
            memcpy ( buffer , &t , sizeof (T) );
            return buffer;
        };

        static T fromArray ( unsigned char *buffer )
        {
            T t;
            memcpy ( &t , buffer , sizeof (T) );
            return t;
        };
};

int main ( int argc , char **argv ) 
{
    Dummy human;
    human.name = "Someone";
    human.height = 11.333;

    unsigned char *buffer = Serialization<Dummy>::toArray (human);

    Dummy dummy = Serialization<Dummy>::fromArray (buffer);

    std::cout << "Name:" << dummy.name << "\n" << "Height:" << dummy.height << std::endl;

    delete buffer;

    return 0;
}
  • 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-29T07:08:55+00:00Added an answer on May 29, 2026 at 7:08 am

    I see two problems with this code:

    1. You are invoking undefined behavior by memcpying a struct containing a std::string into another location. If you memcpy a class that isn’t just a pure struct (for example, a std::string), it can cause all sorts of problems. In this particular case, I think that part of the problem might be that std::string sometimes stores an internal pointer to a buffer of characters containing the actual contents of the string. If you memcpy the std::string, you bypass the string’s normal copy constructor that would duplicate the string. Instead, you now have two different instances of std::string sharing a pointer, so when they are destroyed they will both try to delete the character buffer, causing the bug you’re seeing. There is no easy fix for this other than to not do what you’re doing. It’s just fundamentally unsafe.

    2. You are allocating memory with new[], but deleting it with delete. You should use the array deleting operator delete[] to delete this memory, since using regular delete on it will result in undefined behavior, potentially causing this crash.

    Hope this helps!

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

Sidebar

Related Questions

I am currently trying to serialize a List, it serializes (I think fine), but
I'm trying to serialize a Type object in the following way: Type myType =
I am trying to serialize an object to a sql compact database. I am
I'm trying to serialize a custom class that needs to use multiple elements of
I'm trying to serialize a type using the DataContractSerializer and am getting the exception
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to
I have been trying to serialize a pretty big object. This object uses dictionaries
i'm trying to serialize DataTable to Json or XML. is it possibly and how?
I ran into an error when trying to serialize a linq entitySet. To get

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.