Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

johnnyBravo

Pointers, when declaring 'new' or not ?

This topic is 5291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi with pointers whats the difference of calling ''new'' or not eg MyClass * mc = new MyClass(); or MyClass * mc mc->whateveretc

Share this post


Link to post
Share on other sites
Advertisement
new actually allocates data to the pointer so that it points to something valid and useful. Without using new the pointer points to nothing, and at the worse it points to some data in use by some other program or code running your app.

"Life is a double edged sword, we always hope to strike though adversity with the shining side..."

jkettles16 of Venosoft

Share this post


Link to post
Share on other sites
Simply putting "MyClass *mc;" will not allow you to do anything with mc, since it is supposed to contain a memory address. The "new" operator creates a new instance of the class so that the pointer actually references something. So saying

MyClass *mc = new MyClass();

is the same as saying

MyClass myClass();
MyClass *mc = &myClass;

except that in the first case you don''t have an identifier allowing you to reference the instance of MyClass directly.

Share this post


Link to post
Share on other sites
new does two things it calls "operator new" which allocates the required amount of memory and secondly it calls an objects constructor (if one exists).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!