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

  • Home
  • SEARCH
  • 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 4051104
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:09:11+00:00 2026-05-20T14:09:11+00:00

I have a struct/class which is partiall Plain Old Data (POD). struct S {

  • 0

I have a struct/class which is partiall Plain Old Data (POD).

struct S {
  // plain-old-data structs with only arrays and members of basic types (no pointers);
  Pod1 pod1;
  Pod2 pod2;
  Pod3 pod3;
  Pod4 pod4;
  vector<int> more;
};

I copy objects of class S a lot.
I would like to copy it with memcpy, but S::more prevents it.
I would like to avoid call to 4 memcpy’s and make it all with one for an extra bit of performance.
Should I do someting like this?

memcpy(s1, s2, sizeof(Pod1) + sizeof(Pod2) + sizeof(Pod3) + sizeof(Pod4);

I can’t pack them in separate struct since it would clobber all the code that uses pod1 – pod4.

What is the best solution?

  • 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-20T14:09:12+00:00Added an answer on May 20, 2026 at 2:09 pm

    The best solution is to rely on C++ automatic copy constructor and copy operator. The compiler has then a chance to understand your code and optimize it well. Try to avoid memcpy in C++ code.

    If you need to copy only part of the structure, create a method for it:

    struct S {
      // plain-old-data structs with only arrays and members of basic types (no pointers);
      Pod1 pod1;
      Pod2 pod2;
      Pod3 pod3;
      Pod4 pod4;
      vector<int> more;
    };
    
    void copyPartOfS(S& to, const S& from)
    {
      s.pod1 = from.pod1;
      s.pod2 = from.pod2;
      s.pod3 = from.pod3;
      s.pod4 = from.pod4;
    }
    
    ...
    
    S one, two;
    one = two; // Full copy
    copyPartOfS(one, two); // Partial copy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class which has boolean field: public struct Foo { bool
I have a struct (could be a class) which defined in 'h' file: struct
I have a struct like this: class Items { private: struct item { unsigned
I have class with a member function that takes a default argument. struct Class
Is it possible in C# to have a Struct with a member variable which
I have a metafunction: struct METAFUNCION { template<class T> struct apply { typedef T
I have a template class which looks like the following: template <template <class TypeT>
I have a class called SparseMatrix which contain a private vector of type Cell.
I have implemented a C++ class which behaves very similarly to the standard int
I have a list which contains a number of _MEMORY_BASIC_INFORMATION structs nested within mb

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.