Using object initialisation, stack, heap and new unique pointer.

  • koko82
    Likes 0

    Problem Description

    Hi

    I’ve been looking at different ways to instantate a class within cocos2dx. It looks like there are 3 ways to do this, but want to find out the best way of doing this. If the heap vs stack is better, also in I found out you can use smart pointers, is this something that can be used within cocos2dx without issue? Also it looks like its better to use unique_pointer as you dont need to worry about deleteing this as it does it for you?

    Level_Engine *test1 = new Level_Engine(); // Heap
    Level_Engine test2; // Stack
        
    test1->a = 1;
    test2.a = 2;
        
    std::unique_ptr<Level_Engine> test3; // Heap
        
    test3->a;
    
    

    Also can you also chose between stack and heap for certain sprites and function calls. I am guessing functions could be kepy in Heap for fast execution and sprites better in Stack as more memory required. For example :

    cocos2d::Vector<cocos2d::Sprite*> _SpritesStore; // Stack
    void TouchesEnded(cocos2d::Touch* touches, cocos2d::Event* event); // Stack
    
    or use...
    
    cocos2d::Vector<cocos2d::Sprite> _SpritesStore; // Using heap instead
    void TouchesEnded(cocos2d::Touch touches, cocos2d::Event event); // Using heap instead
    

     

  • Sonar Systems admin
    Likes 0

  • koko82
    Likes 0

    Thanks for link, I am looking at this as point of view for cocos2dx. 

  • Sonar Systems admin
    Likes 0

    Use stack as Cocos handles most of the stuff so use *

  • koko82
    Likes 0

    The * pointer usually indicates that it is heap. Is this different on cocos2dx and means its stack, then its a little confusing if so? 

  • Sonar Systems admin
    Likes 0

    Sorry i meant heap lol

Login to reply