Is any difference between bitwise copy and shallow copy?

Tatranskymedved

With respect to the question What is the difference between memberwise copy, bitwise copy, shallow copy and deep copy? author of first answer says:

Shallow Copy

Refers to copying just the immediate members of an object, without duplicating whatever structures are pointed by them. It is what you get when you do a bit-wise copy.

Why are we having 2 terms used for same thing as Bitwise copy and Shallow copy. Is there really no difference between them?

Does the same apply for other languages (not just C++) and can I use these words interchangeable generally?

largest_prime_is_463035818

A bit-wise copy is a shallow copy but not necessarily vice versa.

The reason is that due to padding there can be bits in objects that you usually ignore, yet they are part of an object.

For example this

struct bar {
    int x;
    foo b;
};

Can lool like this in memory:

| x | some padding | b | more padding |

When you copy the bits via eg memcpy then the padding bits will be copied as well. By comparing the members x and b you cannot tell the difference, but at the level of bits there is a difference. You can notice it when you compare two objects bit wise (instead of member-wise).


As pointed out by Yakk - Adam Nevraumont in a comment, padding is not the only reason why a shallow copy can differ from a bitwise copy. For example

struct foo{ 
    foo* self;
    foo() : self(this) {}
    foo& operator=(const foo& other) {}
    foo(const foo& other) : self(this) {}
};

The member self should point to the object itself, thats an invariant of the class (proper encapsulation omitted for the sake of simplicity). Just copying the pointer would make self->this point to other not to this, hence break the invariant. The operator= does not have to copy anything and a copy constructor merely has to properly initialize self. This can be considered as a shallow copy, because we are only "copying" the pointer self not what it points to (and actually a deep copy would be fatal here). However, a bitwise copy would be different, it would cause a.self point to b after copying b to a (which again would break the invariant).


Consider this example:

#include <iostream>
#include <cstring>

struct X {
    int a = 1;
    double b = 2;
    float c = 3;
    X& operator=(const X& x){
        a = x.a;
        b = x.b;
        c = x.c;
        return *this;
    }
};

int main()
{
    X a;
    X b;
    std::cout << "sizeof(X) " << sizeof(X) << "\n";
    std::cout << "sizeof(int) " << sizeof(int) << "\n";
    std::cout << "sizeof(double) " << sizeof(double) << "\n";
    std::cout << "sizeof(float) " << sizeof(float) << "\n";
    
    //memcpy(&a,&b,sizeof(X));
    a = b;
    char* aptr = reinterpret_cast<char*>(&a);
    char* bptr = reinterpret_cast<char*>(&b);
    for (size_t i = 0; i < sizeof(X); ++i) {
        if (aptr[i] != bptr[i]) std::cout << " !!! ";
    }
}

Possible output is:

sizeof(X) 24
sizeof(int) 4
sizeof(double) 8
sizeof(float) 4
 !!! 

The size of X is not the sum of the size of its members. That is padding. For more details I refer you to Struct padding in C++.

The operator= does a member-wise copy. Because the object contain bytes that are not used for the members, you can observe a difference between a and b after copying the members and looking at the bit representation.

On the other hand, if the copy was made via memcpy then the bit representation of a and b would be guaranteed to be identical (ie no output !!!).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between shallow and deep copy

What is the difference between a deep copy and a shallow copy?

visualizing difference between deep and shallow copy

Why there is no difference between shallow copy and deep copy for a list of immutables

What is the difference between a shallow copy and a deep copy with JavaScript arrays?

What's the difference between a view and a shallow copy of a numpy array?

What is the difference between shallow copy, deepcopy and normal assignment operation?

ES6 React - What are the difference between reference, shallow copy and deep copy and how to compare them?

Difference between INSERT and COPY

Is there any difference between strncat and strncpy to write safe string copy function?

Is there any difference between copy constructor/copy assignment and normal function call optimization in compiling?

What is the difference between copy and deep copy in Julia?

Difference between copy and \copy commands in postgresql

shallow copy of the array but not a copy of the reference

Copy constructor + Shallow & deep copy

Copy command - deep or shallow copy?

GO - Is array copy a deep copy or shallow copy?

DockerFile : Difference between ADD and COPY

Difference between memcpy and copy by assignment

What is the difference between Copy and Clone?

Is there a difference between a borrowed integer and a copy?

In Java, what is a shallow copy?

Shallow copy in Pandas

Is shallow copy really needed?

shallow or deep copy or the array

Shallow copy of a Map in Java

Go deep/shallow copy

Is clone() in java shallow copy?

Shallow Copy in Java