-5

What is the difference between:

int myArray[5];

and

int* myArray = new int[5];
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
Wild Goat
  • 3,509
  • 12
  • 46
  • 87

5 Answers5

6

int myArray[5] is a bunch of five integers with automatic (or static) storage duration (local memory, often classified as "in stack"). Local memory gets cleared in C++ when the specific scope is exited.

int* myArray = new int[5] is a bunch of five integers on with dynamic storage duration (dynamic memory, often classified as "in heap"). Dynamic memory won't get cleared when the specific scope is exited (myArray has to be an int pointer to store the location of your dynamically created memory).

View the following example:

void foo(){
    int myArray[5];
}

void bar(){
    int* myArray_dynamic = new int[5];
}

int main(){
    foo();
    bar();
}

foo will use stack memory, so when foo returns/exits the memory will get freed automatically. However, the dynamic allocated memory, which location is stored in myArray_dynamic in bar won't get freed, as the compiler will only free the memory of myArray_dynamic, not the memory that's stored at its value.

This will create a memory leak, so for every use of new or new[] there has to be a call of delete or delete[] (except you are working with smart pointers, but that's for another question).

The correct version of bar is

void bar(){
    int* myArray_dynamic = new int[5];
    delete[] myArray_dynamic;
}

The primary reason to pick one or the other is that dynamic allocation is slower, but can be any size (automatic arrays must have a fixed compile-time size), and also that space on the stack is limited, and if you run out, very bad things happen.

Community
  • 1
  • 1
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 1
    Might not be on "the stack". Say "automatic or static storage duration" instead; similarly, heap is an implementation detail: say "free store" or "dynamic storage duration" instead. – Lightness Races in Orbit Feb 13 '12 at 21:58
  • 3
    Most people I know say "on the stack" to refer to automatic storage duration. Whether it's actually on an actual stack is, as you point out, an implementation detail; we don't care one way or the other whether there's an actual stack there. Unless you're discussing a context where the underlying implementation actually matters, saying "on the stack" is a quick and well-understand shorthand for what you actually mean. While it may not always be the most technically correct choice of wording, it is usually the *best* choice. – Darryl Feb 13 '12 at 22:26
  • @Darryl: But we don't know that the object _has_ automatic storage duration. The trap has been fallen into. – Lightness Races in Orbit Feb 13 '12 at 23:00
  • If I write this: `void foo(){ int myArray[5]; }` I don't have to explicitly free `myArray`, and I say it's on the stack. Pretty much everyone I know says the same thing. We all know what we mean. Whether it's actually on a stack structure is irrelevant. – Darryl Feb 13 '12 at 23:12
2

The first is an array that's a block of memory allocated either statically or as an automatic variable on the stack during the execution of a function ... it really depends on the context in which its declared/defined.

The second won't compile :-)

To be serious, you really want:

int* myArray = new int[5];

which means we've declared a pointer-type variable that points to an array of integers, and the array of integers is allocated dynamically by the C++ runtime on the heap by the call to new, which is a segment of memory allocated by the OS for your process to dynamically allocate variables in.

Jason
  • 31,834
  • 7
  • 59
  • 78
2

The difference is the lifetime.

int myArray[5];

This reserves storage for an array 5 of int. If myArray is declared at block scope, the array is discarded at the end of the block where it is declared.

int* myArray = new int[5];

This dynamically allocates an array 5 of int, the array exists until it is freed with delete [].

ouah
  • 142,963
  • 15
  • 272
  • 331
0

What is the difference

One is valid.

The other is not.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

In the second you must write

int* myArray = new int[5];

new retuns pointer to the area dynamically allocated in heap.

mikithskegg
  • 806
  • 6
  • 10