Thread: Static objects allocated with new

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Static objects allocated with new

    How are static objects managed when they are allocated with new? Where do these static object reside; in the heap or in the static memory?

    I'm using a singleton as per the pattern described by the Gang of Four to implement a GameMap class, and I found it interesting the fact that the private object holding the sole class instance is allocated with new, but never explicitly deallocated with delete.

    I confess I cannot see a way I could delete it explicitly. Certainly not in the class destructor. So it seems obvious (with my limited knowledge) that the language takes care of that for me.

    But I'm still curious as to how this is done since C++ doesn't normally do this kind of resource management. And most particularly possible implications, if any, this might have on a singleton class that contains RAII-based data members.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Got a particular code example to go along with the question? As a guess, I'd say that the memory, appropriately sized (but uninitialized) for the given object, is reserved ahead of time by the compiler and resides alongside the rest of the static data. The new call is probably a call to placement new which initializes the object (calling any necessary constructor) within the memory location that has already been reserved at compilation time. There is no need to call delete on such an object because the memory was reserved ahead of time and does not come from the heap. Again, that's a guess, maybe someone else has a thought on this... or maybe be able to explain things better.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by hk_mp5kpdw View Post
    Got a particular code example to go along with the question?
    Not at this moment, no. But I didn't deem it necessary, since I'm doing a straight, no nonsense, implementation of the Singleton Pattern (single-threaded).

    I can come up with the actual code later today. But it won't add much, I reckon.

    Quote Originally Posted by hk_mp5kpdw View Post
    As a guess, I'd say that the memory, appropriately sized (but uninitialized) for the given object, is reserved ahead of time by the compiler and resides alongside the rest of the static data.
    It can't be reserved since the size can only be really known at runtime. More so on this case where the singleton pattern is implementing a vector which dimensions are only known after reading from a database. Meanwhile there's no placement new. Just a straight call to new.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'd imagine a static variable allocated with new resides on the heap. Often static variables allocated with new are not deleted when their destructors are trivial and the programmer is assuming that the OS will cleanup the memory upon program completion. A reason delete might not be used is because it is difficult to control the order of construction (and therefore destruction) of static and global objects, so to avoiding deleting an object before another static or global object is done using it the delete is just skipped entirely.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Everything allocated with new is stored on the heap.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    And if the destructor of the allocated object needs to be called on program close, then just use a static std::auto_ptr instead of a static pointer.
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem still exists with an auto_ptr, though. If another static/global object needs to use that object when it is destructed, you cannot guarantee which will be destroyed first. The other object might be destroyed second and it won't be able to clean itself up properly.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You make sure your singletons don't depend on each other for destruction. The problem in your posed scenario is making interdependencies between singletons in the first place.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by QuestionC View Post
    Everything allocated with new is stored on the heap.
    unless new is overloaded

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    I usually do something like this (not compiled) no need for new/delete

    Code:
    class Single
    {
    public:
    static Single &instance() 
    {
      static Single single;
    
      return single;
    }
    
    private:
    
    Single() {}
    };

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Static objects are by definition not allocated with new. If you assign the return value of new to a static pointer, then the static pointer lies in static memory, and the object allocated wherever new put it.

    The GoF singleton is never deleted, its destructor never executed. The resources are reclaimed by the OS, hopefully. This is important to remember because:
    1) You cannot use it on systems where the OS doesn't reclaim a process's resources. (Don't know if there are any left. I don't really think so.)
    2) You cannot have destruction logic.

    The Phoenix singleton attempts to overcome these problems.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by CornedBee View Post
    1) You cannot use it on systems where the OS doesn't reclaim a process's resources. (Don't know if there are any left. I don't really think so.)
    2) You cannot have destruction logic.

    The Phoenix singleton attempts to overcome these problems.
    what with my implementation above? is that the phoenix thing?

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It is interesting that you return a reference, instead of a pointer. I've seen that advise before even when applied to the GoF pattern (which allocates with new) because it encourages clients to not try and delete the object. I plan to do that from now on, regardless.

    Meanwhile your implementation is the so called Meyers' one and addresses the problem of destroying the singleton. The phoenix singleton has something to do with singletons that reference other resources and need to ensure those resources are still there when destroyed. Naturally, it can be used for similar problems where destroying logic must be implemented, or so I think.

    Tremendous help. Thanks everyone. For a moment there I suspected C++ was being sneaky about resource management and it did implement an automatic one even if only for static storage.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. windows .dll vs. linux .so - global static objects
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 11-23-2010, 01:29 PM
  2. static keyword and headers. help me understand.
    By cmay in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 03:19 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  5. Deleting dynamically allocated objects
    By Daniel Jurnove in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2002, 03:30 AM