0

What is the difference between declaring a new integer array by using int someInts[3], versus using int* someInts = new int[3]?

4 Answers 4

6

There are 2 main differences:

  1. The first will allocate a memory on the stack, that will be unavailable once the function returns.
    The second will allocate a memory on the freestore, which will be available until deleted.

  2. The first someInts is an array of ints, you cannot assign new address to it.
    The second is a pointer to int, so you may assign a new address to it.

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

8 Comments

Depending on your perspective, it may not be helpful to distinguish between "stack" and "heap", as these are not concepts that the C++ language concerns itself with.
@OliCharlesworth - Thanks for the comment. What term would you use to point out the first difference I mentioned?
@OliCharlesworth On the contrary the RAII idiom is a huge concern of the c++ design. And to say that the the freestore is not a R in RAII is disingenious.
Take a look at @Jerry's answer. The keywords are "automatic" and "allocated" (these are known as "storage durations").
@CaptainGiraffe: RAII is of course an important concept in modern C++. But it has nothing to do with the terms "stack" vs. "heap"; the language standard doesn't concern itself with these implementation-specific details.
|
3

The difference that's generally important (especially when you're dealing with something other than ints) is that with when you use the latter (int *someints = new int[3];) you have to explicitly delete the data when you're done using it.

Most of the time, you want to use std::vector<int> someints(3); instead. This will (normally) allocate the space for the data similarly, but it'll automatically delete that space when the variable goes out of scope (including, for example, leaving the scope via an exception being thrown, which is much more difficult to handle correctly when you allocate/free the memory manually).

1 Comment

+1: First answer that manages to explain the difference without referring to implementation-specific detail (stack vs. heap).
0

Declaring int* someInts = new int[3] allocates the memory on the heap. Declaring int someInts[3] allocates it on the stack.

Comments

0

When you do someInts[3], you allocate memory on the stack, this means that it will delete itself (good) but if you want to access after the function has ended you'll run into trouble as it will already have been deleted. IE:

int* returnPointerThingy(){
    int someInts[3];
    someInts[0] = 3;
    someInts[1] = 2;
    someInts[2] = 1;
    return someInts
}

This will return a null pointer since someInts has been deleted. If you try to access something in someInts god help you.

If this is similar to what you which to do you want to use the new keyword. It will allow you to allocate something on the "Heap" and it will live after the function it was declared in has ended. Thus this:

int* returnPointerThingy(){
    int* someInts = new int[3];
    someInts[0] = 3;
    someInts[1] = 2;
    someInts[2] = 1;
    return someInts
}

Will not return a null pointer and you will be able to use the values stored by someInts. However this comes with a drawback, you must delete someInts like this:

delete [] someInts

So you don't end up with memory leaks, when the heap gets taken up by pointers and such.

It depends on your situation for which to use as both are better in their own situations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.