Thread: Object's array on the stack vs array on the heap

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Object's array on the stack vs array on the heap

    Is there any benefit to having this:

    OPTION 1:
    Code:
    class stackArray{    
    static const int STACK_SIZE = 3;
    public:
        stackArray(){
            item = new int[STACK_SIZE];
            item[0] = 100;
            item[1] = 101;
            item[2] = 102;
        };
        int *item;
    };
    over this:

    OPTION 2:
    Code:
    class stackArray{    
    static const int STACK_SIZE = 3;
    public:
        stackArray(){
            item[0] = 100;
            item[1] = 101;
            item[2] = 102;
        };
        int item[STACK_SIZE];
    };

    I would think that option 2 could cause dangling pointers but when I test it, it isn't the case. I tested option 2 with the following code:

    Code:
    stackArray createObject(){    
    stackArray instance1;
        return instance1;
    }
    
    
    int main (){
        cout << createObject().item[0] << endl;
        cout << createObject().item[1] << endl;
        cout << createObject().item[2] << endl;
        return 0;
    }
    And it printed out the array the same as option 1 with no garbage values.
    Last edited by Vespasian_2; 03-18-2019 at 01:02 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your option 1 is problematic because you didn't also define the destructor to delete[], and then you didn't define or disable copying. But if you had done these, it would be okay, just that it would have been better to have a std::vector<int> member instead.

    Your option 2 is fine: there are no pointers involved (other than by conversion) so I don't see why you would worry about dangling pointers.

    The advantage of option 1 is of course that it has a dynamic array so the array could be resized at some point, but since STACK_SIZE is a constant that turns out to be not an advantage in this case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Thumbs up

    Quote Originally Posted by laserlight View Post
    Your option 1 is problematic because you didn't also define the destructor to delete[], and then you didn't define or disable copying. But if you had done these, it would be okay, just that it would have been better to have a std::vector<int> member instead.

    Your option 2 is fine: there are no pointers involved (other than by conversion) so I don't see why you would worry about dangling pointers.

    The advantage of option 1 is of course that it has a dynamic array so the array could be resized at some point, but since STACK_SIZE is a constant that turns out to be not an advantage in this case.
    Got it. Thanks again Laserlight, quality response as always.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static, Stack, Heap Array and time
    By 26friends26 in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2011, 04:42 PM
  2. Deleting an array from the heap
    By DarkMasterBosel in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2009, 12:13 AM
  3. Trying to Free Object On Heap As Array Throwing Debug Asserts
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2007, 10:05 PM
  4. C++ Object heap stack
    By vasanth in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 05:47 PM
  5. Managing a heap using an array.
    By rahuls in forum C Programming
    Replies: 3
    Last Post: 03-20-2003, 02:49 PM

Tags for this Thread