0

Suppose I already know the size of the array to be created.Let it be N.

int A[N]; int *A = new int[N]; 

Which way is better/fast ? Why ?

3
  • 6
    Don't forget std::array<int, N>. Commented Jun 25, 2015 at 1:41
  • 1
    Note that int A[N]; is only valid c++ if N is a const. VLAs are not standard c++. Commented Jun 25, 2015 at 1:42
  • Note that int *A = new int[N]; leaks memory unless you call delete [] A; Commented Jun 25, 2015 at 2:05

5 Answers 5

2
int a[N]; 

If N is not a constexpr, this is only legal under certain compilers, usually with language extensions enabled, or a C++14 compiler. ("Variable Length Arrays")

Note this is considered "C syntax" and the modern, preferred way to do this is:

std::array<int, N> a; 

This creates an array of N integers on the stack. If N * sizeof(int) is >= the remaining stack size, you may have a problem.

However: because it is a local object, it has automatic lifetime - it will go away when a goes out of scope. Allocating memory on the stack is also very cheap.

Second variant:

int* a = new int[N]; 

does not require MSVC+language-extensions nor a C++14 compiler, this allocates memory from the heap equivalent to sizeof(int) * N.

It does not get automatic lifetime - you will need to delete[] the memory when you are done with it. Allocating on the heap can be expensive, especially in multithreaded applications.

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

Comments

0

There may be cases where the memory needs of a program can only be determined during runtime. Then you should use :

int *A = new int[N]; //it's dynamic memory. Mean it can be change at runtime. 

Otherwise You should use :

int A[N]; 

Comments

0

compile-time array: C++ 11 has a new container named std::array, like boost::array, which is similar to standard container and can replace the traditional C array.
runtime array: vector is a good choice, you can reserve its size when created. other sequence containers can also be an array.

Comments

0

When N is very large you should use dynamic memory allocation otherwise it will give stack over flow exception.

Comments

0

Which is faster:

int A[N]; is faster because it can be stored on the runtime stack, and when entering the function it will just change the stack pointer by a larger number to allocate room.

int *A = new int[N]; is slower because you have to take the time to find a large-enough consecutive block of memory. Also, it is less likely to be on an easily-accessible part of the computer's storage.

For more information, see this related stack overflow question.

Which is better:

Regarding which one is "better" overall, it depends on the specific case. See this stack overflow question for more details.

4 Comments

Why allocating memory in the heap is slower than allocating memory in the stack ?
@DeepakY4-YrBTechComputerS Because if there isn't enough consecutive memory to allocate, then your program might have to wait to do garbage collection to make more room.
C++ doesn't have garbage collection
You're right, @MattMcNabb. I meant defragmentation, but that was a mistake since C++ doesn't have that either. I modified my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.