From: Jens Schmidt on 8 Aug 2010 23:52 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? Several ideas: 1. Don't do that. An application programmer who uses memcpy for non-POD data is not a programmer. So it just doesn't happen. 2. #define memcpy to something that results in a compile error. Do that for memmove too. 3. In the constructor and in the assignment operator, initialize a pointer with this. Later you may check this equality. -- Greetings, Jens Schmidt [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Kai-Uwe Bux on 8 Aug 2010 23:56 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? I don't know about "best", but I would conjecture that it would be best to design the program so that this feature would not be needed. 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"; } Best Kai-Uwe Bux -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 9 Aug 2010 11:27 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. > The only way I can think of is to store the address of the object internally something like: class example { example const * const my_address // rest of class }: Now ensure that all the ctors set that address (and note that you will have to write a copy ctor because the default copy ctor and copy assignment cannot be instantiated). Now make all member functions check that my_address == this. However I think this is rather draconian, and I would much rather trust the programmer not to shoot himself in the foot by applying memcpy to copy a class instance. IIRC doing such a copy results in undefined behaviour unless the class is a POD (I think that C++0x has introduced more latitude to include other safe cases) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Chris Uzdavinis on 9 Aug 2010 11:26 On Aug 7, 10:31 pm, MC <manan.cho...(a)gmail.com> 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? If you memcpy an object that is not POD, the result is undefined behaviour. As such, there is no reliable way to make code detect it, since you cannot trust what your program will actually do. So, copies involving the copy constructor are the next area to detect/ prevent. The simplest way is to make the class's copy ctor private and then nobody can use it. If you need to use it but want this warning you describe, you might store the "this" pointer, and later compare the current this to the stored one to detect a problem whenver you want. Of course, copies should not copy the stored pointer or the test would fail. I do not see much value in this, however. What are you trying to accomplish with this feature? Chris -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 9 Aug 2010 13:19 MC wrote: > [...] 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? How about just preventing copying or assignment by making copy constructor and assignment operator private? That would cover the correct ways to copy a (non-trivial) C++ object. Concerning memcpy(), I wouldn't bother. Anyone applying memcpy() to a class object obviously has either no clue what they are doing or is a C++ expert that knows precisely what they are doing. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Set implementation in STL Next: Is there std function that calculates a bounded value? |