Jump to content
  • Advertisement


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


Quick question about scope

This topic is 6193 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 all! Do variables and objects that are declared on the free store (heap) have scope? In other words, can I create a variable on the free store in my class constructor, and set a member pointer of the class to point to that variable, without the variable going out of scope as soon as the constructor terminates? Thanks! Gareth

Share this post

Link to post
Share on other sites
That is the purpose of dynamically allocated memory - to bring an object into scope dynamically, use it for as long as you want, and then free it dynamically as well.

If you use the free store (heap) ... then the object has scope until one of the following:

1. The program terminates.
2. You free the memory.

It is very important that you free the memory in the same basic way you alloc it ... as follows:

1. allocate with malloc() or realloc(), free with free()
2. allocate with new, free with delete
3. allocate with new[], free with delete[]

it''s been so long since i used calloc i can''t remember any rules for it .. i think you use free.

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!