0

Hi,

I have been building objects on the stack using the following constructor:

classname node(arg1, arg2);

Which worked fine.

But have now learned it is the heap I need to use.

So I need to use the new command I believe, but my syntax is wrong:

classname *ptr = new node(arg1, arg2);

But when I try this, it fails to compile.

Can you help?

Many thanks !

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Carrots
0

Si I have it working now using this code:

classname *ptr = new classname(arg1, arg2);

I'm a little confused now as I don't see 'node' in the pictures below, just 'ptr' using Visual Studio 2005.

Making the object on the stack:

Autos:

[IMG]http://i33.tinypic.com/9fyvyt.jpg[/IMG]

Locals:

http://i34.tinypic.com/291gorr.jpg


Making the object on the heap:

Autos:

[IMG]http://i36.tinypic.com/wve6hy.jpg[/IMG]

Locals:

[IMG]http://i33.tinypic.com/344uwxz.jpg[/IMG]

This is the first time I've ever specified I want to use the heap instead of the stack.

Now I don't see 'node' in the heap screen shots.

Is it proper/normal practice to only access the object through the pointer now?

But what if the pointer drops off the stack? How will I later find the object to delete it later?

Really appreciate any guidance.

Thanks!

Edited by Carrots: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.