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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:42:59+00:00 2026-06-05T12:42:59+00:00

This is purely hypothetical, but I’m not sure whether the following code will result

  • 0

This is purely hypothetical, but I’m not sure whether the following code will result in undefined behavior according to the C++ spec. I’d like to make a copy of the bytes in an object, blast the object by overwriting it with zeros, then copy the old bytes back. Can I do so without causing undefined behavior?

Sample code:

NonPODType o;
char bytes[sizeof(o)];

memcpy(bytes, &o, sizeof(o));
memset(&o, 0, sizeof(o));
memcpy(&o, bytes, sizeof(o));
  • 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-05T12:43:01+00:00Added an answer on June 5, 2026 at 12:43 pm

    In general, no. There’s an explicit guarantee that this works for trivially copyable types on §3.9/2, but there’s no such thing for other types.

    For any object (other than a base-class subobject) of trivially
    copyable type T, whether or not the object holds a valid value of type
    T, the underlying bytes (1.7) making up the object can be copied into
    an array of char or unsigned char. If the content of the array of
    char or unsigned char is copied back into the object, the object shall
    subsequently hold its original value. [Example:

    #define N sizeof(T)
    char buf[N];
    T obj;
    // obj initialized to its original value
    std::memcpy(buf, &obj, N);
    // between these two calls to std::memcpy,
    // obj might be modified
    std::memcpy(&obj, buf, N);
    // at this point, each subobject of obj of scalar type
    // holds its original value
    

    —end example ]

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

Sidebar

Related Questions

System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself
I am not sure if I can do this purely with sed: I am
Assuming you have a hypothetical enum in java like this (purely for demonstration purposes,
I understand this is purely a coding related forum, but i believe techies can
This is purely out of curiosity, but why does this occur? >>> a =
This is purely an experiment, but I'm wondering if it's possible to get a
I have tried many times to do this, but it has not worked out
I have been putting off developing this part of my app for sometime purely
This should not be so complicated: I am using Easiest Tooltip and Image Preview
This has been posted, but I've tried lot of solutions found on SO and

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.