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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:39:57+00:00 2026-05-15T08:39:57+00:00

Say I have a class, something like the following; class MyClass { public: MyClass();

  • 0

Say I have a class, something like the following;

class MyClass
{
public:
  MyClass();
  int a,b,c;
  double x,y,z;
};

#define  PageSize 1000000

MyClass Array1[PageSize],Array2[PageSize];

If my class has not pointers or virtual methods, is it safe to use the following?

memcpy(Array1,Array2,PageSize*sizeof(MyClass));

The reason I ask, is that I’m dealing with very large collections of paged data, as decribed here, where performance is critical, and memcpy offers significant performance advantages over iterative assignment. I suspect it should be ok, as the ‘this’ pointer is an implicit parameter rather than anything stored, but are there any other hidden nasties I should be aware of?

Edit:

As per sharptooths comments, the data does not include any handles or similar reference information.

As per Paul R’s comment, I’ve profiled the code, and avoiding the copy constructor is about 4.5 times faster in this case. Part of the reason here is that my templated array class is somewhat more complex than the simplistic example given, and calls a placement ‘new’ when allocating memory for types that don’t allow shallow copying. This effectively means that the default constructor is called as well as the copy constructor.

Second edit

It is perhaps worth pointing out that I fully accept that use of memcpy in this way is bad practice and should be avoided in general cases. The specific case in which it is being used is as part of a high performance templated array class, which includes a parameter ‘AllowShallowCopying’, which will invoke memcpy rather than a copy constructor. This has big performance implications for operations such as removing an element near the start of an array, and paging data in and out of secondary storage. The better theoretical solution would be to convert the class to a simple structure, but given this involves a lot of refactoring of a large code base, avoiding it is not something I’m keen to do.

  • 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-15T08:39:57+00:00Added an answer on May 15, 2026 at 8:39 am

    According to the Standard, if no copy constructor is provided by the programmer for a class, the compiler will synthesize a constructor which exhibits default memberwise initialization. (12.8.8) However, in 12.8.1, the Standard also says,

    A class object can be copied in two
    ways, by initialization (12.1, 8.5),
    including for function argument
    passing (5.2.2) and for function value
    return (6.6.3), and by assignment
    (5.17). Conceptually, these two
    operations are implemented by a copy
    constructor (12.1) and copy assignment
    operator (13.5.3).

    The operative word here is “conceptually,” which, according to Lippman gives compiler designers an ‘out’ to actually doing memberwise initialization in “trivial” (12.8.6) implicitly defined copy constructors.

    In practice, then, compilers have to synthesize copy constructors for these classes that exhibit behavior as if they were doing memberwise initialization. But if the class exhibits “Bitwise Copy Semantics” (Lippman, p. 43) then the compiler does not have to synthesize a copy constructor (which would result in a function call, possibly inlined) and do bitwise copy instead. This claim is apparently backed up in the ARM, but I haven’t looked this up yet.

    Using a compiler to validate that something is Standard-compliant is always a bad idea, but compiling your code and viewing the resulting assembly seems to verify that the compiler is not doing memberwise initialization in a synthesized copy constructor, but doing a memcpy instead:

    #include <cstdlib>
    
    class MyClass
    {
    public:
        MyClass(){};
      int a,b,c;
      double x,y,z;
    };
    
    int main()
    {
        MyClass c;
        MyClass d = c;
    
        return 0;
    }
    

    The assembly generated for MyClass d = c; is:

    000000013F441048  lea         rdi,[d] 
    000000013F44104D  lea         rsi,[c] 
    000000013F441052  mov         ecx,28h 
    000000013F441057  rep movs    byte ptr [rdi],byte ptr [rsi] 
    

    …where 28h is the sizeof(MyClass).

    This was compiled under MSVC9 in Debug mode.

    EDIT:

    The long and the short of this post is that:

    1) So long as doing a bitwise copy will exhibit the same side effects as memberwise copy would, the Standard allows trivial implicit copy constructors to do a memcpy instead of memberwise copies.

    2) Some compilers actually do memcpys instead of synthesizing a trivial copy constructor which does memberwise copies.

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

Sidebar

Related Questions

Say i have something like: [DataContract(Namespace=http://bla.bla)] public class MyClass { [DataMember] public long ResponseCode
Say I have a simple object such as class Something { public int SomeInt
Say I have this class: class myclass { public int Field1{ get; set; }
Let's say I have a bunch of classes that look something like... class Foo{
say I have: class Test { public static int Hello = 5; } This
Let's say I have the following class: class ABC { private int myInt =
Say I have a class that looks like the following: internal class SomeClass {
I have classes that are structured like the following: public class Forecast { [Key]
Lets say i have the following scenario: public class A { public String createString(final
Long story short Say I have the following code: // a class like this

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.