Thread: Copy Constructor Question

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10

    Copy Constructor Question

    Code:
    heap::heap(const heap &aHeap)
    {
      delete []   heapArr;
      heapArr =NULL;     
      cout << heapArr <<endl;
      heapArr = new HeapNode[aHeap.maxSize];
      if (heapArr != NULL)
      {
        maxSize = aHeap.maxSize;
        curSize = aHeap.curSize;
        for (int a = 0; a <= curSize; a++)
        {
          heapArr[a] = aHeap.heapArr[a];
        }
      }
      else
        maxSize = 0;
    }
    I've been told that I need the red line because

    "you may actually be copying over an existing heap. Think of the situation where a function returns a heap - you could potentially assign the return value to an existing heap that has values in it"

    and that the solution is the following code:
    Code:
     
    if (heapArr != NULL)
    {
          delete [] heapArr
           heapArr = NULL;
    }
    Now I've tested this(get a fault) and have some questions. If this constructor is not being used to assign the return value of a heap and is used to create a new heap why would the value in heapArr be NULL? It hasn't been initialized to anything let alone NULL. No other constructor has been called to do so, right?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    "you may actually be copying over an existing heap. Think of the situation where a function returns a heap - you could potentially assign the return value to an existing heap that has values in it"
    If I'm understanding what this is trying to say then I interpet it to be something like the following:
    Code:
    heap foo;
    /* Does something that gives foo some values */
    foo = SomeFunctionThatReturnsAHeap();
    In this case the copy constructor for foo would not be called. It would be the assignment operator. I can't really think of an instance where their example is true. Constructors are called only one during the life of an object and thats during initalization.

    Edit: Where does the seg fault occur? Is it happening at the first delete?
    Last edited by Thantos; 02-13-2005 at 08:47 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    Quote Originally Posted by Thantos
    If I'm understanding what this is trying to say then I interpet it to be something like the following:
    Code:
    heap foo;
    /* Does something that gives foo some values */
    foo = SomeFunctionThatReturnsAHeap();
    In this case the copy constructor for foo would not be called. It would be the assignment operator. I can't really think of an instance where their example is true. Constructors are called only one during the life of an object and thats during initalization.
    I'm not sure what she means but she may mean return by reference.
    I don't know but it may be more like this:
    Code:
    heap foo;
    bool success=false;
    /* Does something that gives foo some values */
    success = SomeFunctionThatchangesAHeap(foo);
    Last edited by Z-Ender; 02-13-2005 at 08:53 PM.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Nope that is just an assignment as well. success recieves the value that SomeFunctionThatchangesAHeap() returns.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    Well damn. I don't understand how my computer science professor could be so wrong. I'll have to ask her to go more in detail of what she means.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > you may actually be copying over an existing heap.
    But it's a constructor!
    There is no "existing" to worry about.

    Besides, you've got contrary evidence because the damn thing keeps crashing on you, so you've gotta decide who to believe
    - your tutor
    - your book
    - your compiler
    - other people

    I think your tutor is mixed up about
    Code:
    heap myheap;   // calls default constructor
    heap newheap(oldheap);  // calls the constructor you've written
    heap2 = heap1; // an as-yet unwritten operator = function
    Try various ways of creating a new heap, and see which bits of code cause your constructor to be called.

    > if (heapArr != NULL)
    Which still misses the point.
    Pick a random pointer, and there could be any one of 2^32 possible bit-patterns stored in that pointer. Only ONE of those values is NULL, and only a handful of those will ever be valid pointers as returned by new. All the rest will blow up your code if you try and delete them.

    > I don't understand how my computer science professor could be so wrong.
    Professors who actually have a clue are remarkably rare based on the evidence of many past posts which basically say "but my prof said <<insert false statement here>>"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The people who have a clue usually get better jobs than teaching programming classes...

    At least that's my theory.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    Well you're all right of course. I just tested it and used cout statements to tell me if the copy constructor was called. It wasn't and now i have to convince a computer science professor that she doesn't know what she is talking about even though she has given this assignment at least 5 times before me.

    This is gonna be hard, any suggestions?
    Last edited by Z-Ender; 02-14-2005 at 03:01 PM.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    As a side note:
    Code:
    if (heapArr != NULL)
    {
          delete [] heapArr
           heapArr = NULL;
    }
    The test for != NULL is unnecessary, because the C++ language guarantees that a NULL pointer can be 'deleted' safely (i.e. it will do nothing).

    >>any suggestions?
    Well, perhaps just be civil and tell her that after doing some experimentation you determined that the copy constructor is never called?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > This is gonna be hard, any suggestions?
    Create a really simple class based on what you have so far - with a default constructor, and a constructor which takes a parameter.
    These constructors just call cout to indicate when they are called.

    Then show the code and the results to your prof and ask for an explanation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Salem has the best approach. It allows you to watch them fumble around trying to come up with an answer all while not actually accusing them of anything

  12. #12
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    That might just ........ her off and cause her to grade me harder. Oh well I guess.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, you just need to act like you're genuinely puzzled why it's not working. Make it look as if you honestly think it's your fault.

    You can laugh afterwards, out of sight and earshot.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I still say you go with the old you are an idiot and then laughing at your prof, this ussually results in a very good grade. Sometimes making flyers and posting them saying that "so and so prof is an idiot do not take this class" this is also a method I have used several times.
    Woop?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > That might just ........ her off and cause her to grade me harder
    Look at it this way, if they're getting this fundamental stuff so badly wrong, what value are you attaching to the rest of the stuff they're "teaching" you? I'm beginning to think this "prof" found her qualification on ebay or something.

    If you're shelling out your own money (or debt) for this course, you've gotta make sure they're for real otherwise it's just a waste.

    Discuss your findings with some of the other students (especially the smarter ones) to see if they've also spotted that the prof is clueless. She can't then single you out if everyone else also knows. Safety in numbers and all that

    It's hard enough to learn C++ once, without having to learn and unlearn a bunch of crap beforehand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 04:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 04:21 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM