Thread: Overloading Assigment Operator. Freeing Memory.

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Overloading Assigment Operator. Freeing Memory.

    There is an exercise I'm having trouble with in Teach Yourself C++
    in 21 days.

    It says this:

    Provide an operator= for SimpleCircle.

    And gives this answer:

    Code:
    class SimpleCircle{
      public:
      //..
      SimpleCircle &operator=(const SimpleCircle &rhs);
      //..
      private:
        int *itsRadius;
    }
    
    SimpleCircle &SimpleCircle::operator=(const SimpleCircle &rhs){
      if(this == &rhs) return *this;
      
      delete itsRadius;
      itsRadius = new int;
      *itsRadius = rhs.GetRadius();
      return *this;
    }
    Why is the author calling delete on 'itsRadius' only to re-allocate
    memory on the next line? The memory was originally allocated
    in the constructor. Can't I still use that?

    I didn't have the delete-call in my answer (below)... Why would
    I need it? If I'm assigning a value to another variable, wouldn't
    I only need to change the value that's at the already-allocated
    memory?

    Code:
    SimpleCircle &SimpleCircle::operator=(const SimpleCircle &rhs){
      if(this == &rhs) return *this;
      
      *itsRadius = rhs.GetRadius();
      return *this;
    }
    It's on page 805, Question 7 (if anybody has the book)...

    -
    Last edited by Cheeze-It; 11-29-2002 at 08:12 PM.
    Staying away from General.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The only guess I can provide is -- Is there a form of the constructor, or is there any other way, that the pointer might be initialized to 0? It seems silly in the first place that they'd use dynamic memory allocation for a single integer that represents a radius, unless, perhaps, they are planning on using it as a base class for an ellipse, where they might have 2 values -- one for the major and one for the minor axis length, or maybe even plan on deriving an ellipsoid as well, and for some reason want the axes all in that array, but that just seems silly.

    In my opinion, this seems like an incredibly dumb use of dynamic memory allocation in the first place, but I may be missing something.

    Anyways -- if there is some possibility that the pointer can be set to 0, they have the delete in there to serve as both a check and a deallocation. It would be invalid to dereference itsRadius if the pointer value was 0, so they'd have to make an if statement to check if the value was 0. Since they want to reallocate memory as well (for some unknown reason, maybe if i had the book I could tell you), delete implicitly does a check to see if it points to 0.

    Again, this seems really dumb on behalf of the author because I can honestly see no good reason they'd be using dynamic memory allocation instead of just an integer. If you know why, then post the reasoning, and I might be able to give a better answer.

    EDIT: And the rationalization I gave earlier appears false as well because he declares it as private instead of protected.

    So even more-so than before, this just seems like extremely poor use of dynamic memory allocation to me.
    Last edited by Polymorphic OOP; 11-29-2002 at 06:35 PM.

  3. #3
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Polymorphic: The reason that itsRadius is declared on the free store is because the particular chapter in the book discusses the difference between deep and shallow copies. That is just being used an example to prevent overcomplicating things.

    Ethic: I do have the book, (although it's on page 810 in my version), and as far as I can tell there really isn't a reason to delete itsRadius and then reallocate it, so, unless someone cares to correct me, I would say that it's an error in the book. It's not a big error, the code will still work, it's just odd.
    Last edited by Nippashish; 11-29-2002 at 09:37 PM.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Could you post the code for the constructor??? Iīll have to guess like Polymorphic OOP that (probably) itsRadius has been set to NULL and therefore must be deleted first (itīs okey to delete a pointer assigned to NULL).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  3. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  4. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM