Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Necessary for copy constructor??

Reply
Thread Tools

Necessary for copy constructor??

 
 
Pete Becker
Guest
Posts: n/a
 
      01-11-2007
peter koch wrote:
> (E-Mail Removed) skrev:
>> Will the following program cause any problem, because of the missing
>> explicit copy constructor for the 'test' class?

>
> Yes. You will have double deletes.


No, there are no double deletes in the posted code.

> Depending on your real needs, you
> should use std::vector or std::string instead of the lowlevel pointer.
> In fact, operator new[] should almost never be used. Prefer std::vector
> (or in rare situations raw operator new)
>
> /Peter
>> #include <vector>
>> #include <iostream>
>>
>> class test
>> {
>> char* cPtr ;
>>
>> public:
>>
>> test()
>> {
>> cPtr = new char[10] ;
>> }
>>
>> ~test()
>> {
>> delete cPtr ;
>> }
>> } ;
>>
>>
>> int main()
>> {
>> std::vector< test*> vecTest ;
>>


Object allocated here, never copied:

>> test* testPtr = new test() ;
>>
>> vecTest.push_back( testPtr ) ;
>>


Object deleted here, exactly once.

>> delete testPtr ;
>>
>> vecTest.clear() ;
>> }

>



--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-11-2007
Pete Becker wrote:
> Clark S. Cox III wrote:
> > Stuart Redmann wrote:
> > > As pointers have their own copy constructor, you can use them in
> > > STL containers freely.

> >
> > Woah there! That's horrible advice. Without a copy constructor, using
> > such an object in an STL container will invariably lead to
> > double-deletes (i.e. undefined behavior).
> >

>
> Whoa, again! In the specific context of the original example, there is
> no problem with double deletes. Pointers do, in fact, have valid copy
> semantics.


Indeed, I see that now (i.e. that the OP was creating a vector of
*pointers* to test).

--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-11-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Will the following program cause any problem, because of the missing
> explicit copy constructor for the 'test' class?
>
> #include <vector>
> #include <iostream>
>
> class test
> {
> char* cPtr ;
>
> public:
>
> test()
> {
> cPtr = new char[10] ;
> }
>
> ~test()
> {
> delete cPtr ;
> }
> } ;
>
>
> int main()
> {
> std::vector< test*> vecTest ;
>
> test* testPtr = new test() ;
>
> vecTest.push_back( testPtr ) ;
>
> delete testPtr ;
>
> vecTest.clear() ;
> }


As used, there won't be a problem. But, you should either make a non public
copy and assignment operator, or make them. The reason is if you do try to
copy this class you'll get a double delete. Making a non public copy and
assignement operator means you can't accidently copy the class.


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-11-2007
Clark S. Cox III wrote:
> Pete Becker wrote:
>> Clark S. Cox III wrote:
>>> Stuart Redmann wrote:
>>>> As pointers have their own copy constructor, you can use them in
>>>> STL containers freely.
>>> Woah there! That's horrible advice. Without a copy constructor, using
>>> such an object in an STL container will invariably lead to
>>> double-deletes (i.e. undefined behavior).
>>>

>> Whoa, again! In the specific context of the original example, there is
>> no problem with double deletes. Pointers do, in fact, have valid copy
>> semantics.

>
> Indeed, I see that now (i.e. that the OP was creating a vector of
> *pointers* to test).
>


Yup. Looking at how to use non-copyable objects in containers, I suspect.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-11-2007

Pete Becker skrev:
> peter koch wrote:
> > (E-Mail Removed) skrev:
> >> Will the following program cause any problem, because of the missing
> >> explicit copy constructor for the 'test' class?

> >
> > Yes. You will have double deletes.

>
> No, there are no double deletes in the posted code.
>

[snip]

You are right - I only skimmed "main" and failed to notice that it was
a vector of pointers. Still, there's no excuse for not hiding operator=
and the copy constructor.

/Peter

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-11-2007
peter koch wrote:
> Pete Becker skrev:
>> peter koch wrote:
>>> (E-Mail Removed) skrev:
>>>> Will the following program cause any problem, because of the missing
>>>> explicit copy constructor for the 'test' class?
>>> Yes. You will have double deletes.

>> No, there are no double deletes in the posted code.
>>

> [snip]
>
> You are right - I only skimmed "main" and failed to notice that it was
> a vector of pointers. Still, there's no excuse for not hiding operator=
> and the copy constructor.
>


Sure there is: it's an experiment, not production code.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-14-2007
Pete Becker wrote:
>
> Yup. Looking at how to use non-copyable objects in containers, I suspect.


But dangerous usage

> test() { cPtr = new char[10] ; }

test():cPtr(new char[10]) { }

> ~test(){ delete cPtr ; }

~test(){ delete[] cPtr ; }

"(E-Mail Removed)" must at least define private copy&assign
private:
test(const test&):cPtr(0) { abort(); }
void operator= (const test&){ abort(); }

and he does not call "delete testPtr;" befor "vecTest.clear() ;"

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-14-2007
Grizlyk wrote:
> Pete Becker wrote:
>> Yup. Looking at how to use non-copyable objects in containers, I suspect.

>
> But dangerous usage
>
>> test() { cPtr = new char[10] ; }

> test():cPtr(new char[10]) { }
>
>> ~test(){ delete cPtr ; }

> ~test(){ delete[] cPtr ; }
>
> "(E-Mail Removed)" must at least define private copy&assign
> private:
> test(const test&):cPtr(0) { abort(); }
> void operator= (const test&){ abort(); }
>
> and he does not call "delete testPtr;" befor "vecTest.clear() ;"
>


No, there's nothing dangerous in the code as written. But I'm sure he's
grateful for all the advice about how code that's supposed to do
something different should be written.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Q: why are all templated copy-constructors necessary? Claudius C++ 8 07-23-2007 05:48 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is a digital filter necessary? nesppi@gmail.com VHDL 1 01-13-2006 11:37 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
cookie handling - is there a list of *necessary* cookies ?? cookie-monster Firefox 1 03-05-2005 03:40 AM



Advertisments