Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Creating objects in C++

Options
  • 30-01-2001 11:32am
    #1
    Registered Users Posts: 1,481 ✭✭✭


    In C++, what's the difference between creating an object with
    Foo myFooInstance(0, 0);
    
    and
    Foo* myFooInstance = new Foo(0, 0);
    
    I know the second one returns a pointer, but apart from this what's the difference?


Comments

  • Registered Users Posts: 2,660 ✭✭✭Baz_


    What difference are you expecting?? (just a question not being smart.)

    I'm not sure how much you know about c++ so I don't want to patronize you with stuff you already know but...

    the first instance declared is stored in the stack area of memory and the second instance is stored on the heap.

    The methods and members of the two instances are also accessed slightly differently from each other

    ie
    (1)
    myFooInstance.[method]
    (2)
    myFooInstance->[method]

    Not sure if that helps at all but if not please clarify what info you want.


  • Closed Accounts Posts: 218 ✭✭Void


    Baz is right, here's a different way of explaining it though:

    Foo myFooInstance(0,0);
    This will create an instance which is only visible in the current context. Once the context is exited, myFooInstance(0,0) will be destroyed (calling it's destructor etc).

    On the other hand:
    Foo* myFooInstance = new Foo(0, 0);
    This makes a "permanent" instance, and returns a pointer. As long as you have a pointer to this instance, you can access it (think of linked lists). The drawback is you will have to manually call delete() on each instance (not doing this causes memory leaks etc).





  • Registered Users Posts: 1,481 ✭✭✭satchmo


    <font face="Verdana, Arial" size="2">the first instance declared is stored in the stack area of memory and the second instance is stored on the heap</font>

    Yeah that's the difference I'm talking about. I'm coming from Java and I've pretty much figured out the differences between the two and how to program in C++, but I couldn't figure this out.

    The main problem with Java is it sometimes hides too much from you, so my knowledge here is a bit hazy. As far as I can remember, the stack memory is limited so it's probably better to use the heap memory in general. Am I right? Which do you use when?


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    right, so you'd use the first one for temporary or short-lived objects, and the second for more persistent ones.

    gotcha. thanks guys.


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    no prob


  • Advertisement
  • Moderators, Social & Fun Moderators Posts: 10,501 Mod ✭✭✭✭ecksor


    <font face="Verdana, Arial" size="2">Originally posted by Jazz:
    Yeah that's the difference I'm talking about. I'm coming from Java and I've pretty much figured out the differences between the two and how to program in C++, but I couldn't figure this out.</font>

    In Java, all objects are allocated on the heap, so they persist after you leave the block/function you created them in as long as you maintain a reference to them.
    <font face="Verdana, Arial" size="2">
    The main problem with Java is it sometimes hides too much from you, so my knowledge here is a bit hazy. As far as I can remember, the stack memory is limited so it's probably better to use the heap memory in general. Am I right? Which do you use when?
    </font>

    What do you mean when you say it hides too much from you? Examples?

    Use heap memory for dynamic allocation. For example, it's not always practical to allocate something on the stack, you may not be able to determine until runtime if you need it, or how many of a certain object you need (lists, trees, etc). Also, allocate an object on the heap if you need an object to persist outside of it's current function/block and maintain or pass back a reference to the object after creating it.

    Allocating on the stack is faster though, an application needs to do an sbrk() system call (or equivilent) to increase the size of the processes heap, and this has an overhead. You must also explicitly free up the memory (using 'delete' in C++) when you are finished with it if you allocate on the heap (unless your language supports garbage collection), whereas on the stack it will just disappear when you leave the current scope as the stack unwinds.


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    All I mean when I say Java hides too much is that the programmer doesn't have to worry about any sort of lower-level system stuff like this.
    Now that's not a bad thing at all, indeed if anything it makes life easier, but after a while I started to get frustrated when I wanted to learn more about these sorts of things. Which is why I'm trying to figure out C++.

    It's clearer now though, thanks.


Advertisement