From: Dragan Milenkovic on
On 08/08/2010 05:31 AM, MC wrote:
> Hi I am writing a library in C++. In my library I want to implement a
> feature that if an application programmer instantiates a class object
> and later does a shallow copy by memcpy or by any other means. During
> the run time when ever any function of the shallow copy object is
> called it should print out a message Warning: Shallow Copy detected.
>
> What will be the best way to do this?

Only plain structures should be memcpy-ed.

You simply design a class so that there is no shallow copy available.
Copy-ctor/assignment will either do deep copy or be removed.

Or maybe you wanted to detect your structures copied... But since
you mention "shallow copy", I'm guessing that this is not a structure
but encapsulates some complex meaning.

--
Dragan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Maclaren on
In article <a5817923-6f72-4b3b-ac8e-b876d9b84aa3(a)u26g2000yqu.googlegroups.com>,
marcin.sfider(a)gmail.com <marcin.sfider(a)gmail.com> wrote:
>
>However IMO using memcpy or any of similar means to "shallow copy"
>non-POD C++ object is such an fundamental error that you can ignore
>it.
>Someone who does such things will probably learn on his mistakes
>soon enough.

I wish :-( And I am afraid that Jens Schmidt is wrong, too; it does
happen, very commonly. I am not denying the fundamental nature of
the error, but the problem is that it 'works' in so many common cases.
The people who learn programming from copying incantations and hacking
them until the compiler shuts up often believe that it is allowed.
They then bleat about compiler bugs when it fails. And I am not
denying that they aren't programmers, either!

But there are a lot of them around ....


Regards,
Nick Maclaren.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
> Hi I am writing a library in C++. In my library I want to implement a
> feature that if an application programmer instantiates a class object
> and later does a shallow copy by memcpy or by any other means. During
> the run time when ever any function of the shallow copy object is
> called it should print out a message Warning: Shallow Copy detected.
>
> What will be the best way to do this?

Start by defining the term "shallow copy".

No, seriously. First, memcpy on anything but aggregate types (POD) in C
++ is an undefined behavior. Second, "shallowness" is a trait of the
type being copied, defined by the programmer who wrote the type.
There's no way for your code to know what is "shallow" for an
arbitrary type, simply because code can't read minds.

Even if you have some simplistic idea about a shallow copy, e.g.
"everything but members that are pointers", you still can't do it.
e.g. if you use your own memcpy, to "shallow copy" an object, how do
you plan to detect that? I mean,

TYPE v;
TYPE v2;
memcpy(v, v2, sizeof(v2)); // Warning: Shallow Copy detected?
Bwahahahha...

What you should instead do is to learn to handle memory. Learn "rule
of three" (wikipedia has a nice explanation). Learn auto_ptr, learn
shared_ptr, weak_ptr. When designing your code, learn to think in
terms of ownership (of objects). Stop barking up the wrong tree.

Goran.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: cpp4ever on
On 08/08/2010 04:31 AM, MC wrote:
> Hi I am writing a library in C++. In my library I want to implement a
> feature that if an application programmer instantiates a class object
> and later does a shallow copy by memcpy or by any other means. During
> the run time when ever any function of the shallow copy object is
> called it should print out a message Warning: Shallow Copy detected.
>
> What will be the best way to do this?
>
> Thanks.
>

First and foremost you need to take great care. The Qt framework
implements this type of thing, calling it implicit sharing which
basically only allocates more memory when required. Underlying this
implementation is a number indicating the number of objects sharing the
memory. This leads to the address of the memory referenced by an object
can change seemingly unexpectedly, as I found out when using QVector
which then invalidated an iterator I was relying on. Hence Qt provides
mechanisms for checking if the object is attached to shared memory,
detaching the object from using shared memory, and ensuring the object
will not use shared memory. Finally you may want to consider whether
this library is going to be used in multi-threaded application.

HTH

cpp4ever

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Vladimir Jovic on
Kai-Uwe Bux wrote:
> As for detecting shallow copies, what about something like this:
>
> class detect_shallow_copy {
>
> detect_shallow_copy * this_ptr;
>
> public:
>
> detect_shallow_copy ( void )
> : this_ptr ( this )
> {}
>
> detect_shallow_copy ( detect_shallow_copy const & )
> : this_ptr ( this )
> {}
>
> detect_shallow_copy &
> operator= ( detect_shallow_copy const & ) {
> return ( *this );
> }
>
> bool
> is_shallow ( void ) const {
> return ( this != this_ptr );
> }
>
> };
>
> #include <cstring>
> #include <iostream>
>
> int main ( void ) {
> detect_shallow_copy d;
> detect_shallow_copy e;
> std::memcpy( &e, &d, sizeof(d) );
> std::cout << d.is_shallow() << "\n";
> std::cout << e.is_shallow() << "\n";
> }
>

Very nice trick.
Does it depends on the compiler and how the hidden pointer *this is
implemented?
Will it work if detect_shallow_copy inherit from a class with virtual
destructor and methods?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]