Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / October 2005



Tip: Looking for answers? Try searching our database.

Where is the error?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Iforget - 27 Oct 2005 20:55 GMT
I have a class containing a simple pointer.  I have a vector containing
several of the aforementioned classes.  Because of the pointer, this
class contains its own copy and assignment operators.  The constructor
for this class sets the initial pointer value to 0.  The copy and
assignment operators of this class delete the old object before pointing
to the new object.  I check for a non-zero pointer value before
attempting a delete operation.  The problem I have is that sometimes, I
am getting an object with a non-zero pointer value that does not point
to anything during the creation of the vector.  It does not occur all
the time, but depends on the vector size or whether I am running the
same code several times in a row within the program.  This is CW 9.5
running on 10.3.9.

Thanks for any suggestions,
Ed
Phil Wasson - 27 Oct 2005 22:28 GMT
> I have a class containing a simple pointer.  I have a vector containing
> several of the aforementioned classes.  Because of the pointer, this
[quoted text clipped - 7 lines]
> the time, but depends on the vector size or whether I am running the
> same code several times in a row within the program.

So, what does your class do if you assign one instance to another? You
have two objects with the same pointer, right? Say you then assign
again to one of those; it disposes of the pointer, but the other
instance still has that. I'm pretty sure that if you insert into a
vector, the objects within are shuffled around using assignment, so bad
things can happen in a case like yours. That's the same reason why you
can't store STL auto_ptr objects in standard containers, too.
Iforget - 27 Oct 2005 22:39 GMT
> > I have a class containing a simple pointer.  I have a vector containing
> > several of the aforementioned classes.  Because of the pointer, this
[quoted text clipped - 15 lines]
> things can happen in a case like yours. That's the same reason why you
> can't store STL auto_ptr objects in standard containers, too.

My mistake for being unclear.  Pointing to the new object means creating
a new heap object filled using a deep copy, e.g.,

A::A(A const& source) {
  if(pointer_) {
     delete pointer_;
  }
  pointer_ = new B(*source.pointer_);
  return;
}

Ed
Iforget - 27 Oct 2005 23:07 GMT
> > > I have a class containing a simple pointer.  I have a vector containing
> > > several of the aforementioned classes.  Because of the pointer, this
[quoted text clipped - 28 lines]
>
> Ed

oops, should be

A::A(A const& source) {
   if(pointer_) {
      delete pointer_;
   }
   if(source.pointer_) {
      pointer_ = new B(*source.pointer_);
   }
   else {
     pointer_ = 0;
   }
   return;
}

Ed
Allen Brunson - 28 Oct 2005 00:50 GMT
> My mistake for being unclear.  Pointing to the new object means creating
> a new heap object filled using a deep copy, e.g.,
>
> [copy constructor snipped]

you still haven't answered the question posed to you, to wit: what does this
class' assignment operator look like?  if you didn't write one, then the
compiler will generate one for you.  since your object contains a pointer, the
compiler-generated assignment operator will do the wrong thing.

if your answer is "i don't use the assignment operator," you're incorrect,
because std::vector will call it on your behalf.
Iforget - 28 Oct 2005 15:15 GMT
> > My mistake for being unclear.  Pointing to the new object means creating
> > a new heap object filled using a deep copy, e.g.,
[quoted text clipped - 8 lines]
> if your answer is "i don't use the assignment operator," you're incorrect,
> because std::vector will call it on your behalf.

The assignment operator is defined by me and it is basically the same as
the copy constructor. However, it checks for equality before doing
anything and returns *this with a reference.

Ed
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.