Archived

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

Quick question about scope

This topic is 5974 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