2

For below singleton class where would be the memory taken from (Stack or Global memroy)

class Singleton
{
    public:

        static Singleton* get()
        {
            static Singleton instance;
            return &instance;
        }
};
4
  • Take care if you use multithreaded program. You should protect your static call. Commented Jan 31, 2014 at 15:17
  • i am more interested in singleton class memory allocation ,not for the static variable's memory allocation ..as i have seen in above link .. Commented Jan 31, 2014 at 15:18
  • 1
    @Ashwin actually, this is guaranteed to be thread-safe Commented Jan 31, 2014 at 15:20
  • I don't understand why you are returning a pointer: if it cannot be null, it should be a reference. Commented Jan 31, 2014 at 18:43

1 Answer 1

3

instance will be located in static storage (or global as you call it).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.