Skip to main content
added 90 characters in body
Source Link
Alan
  • 47k
  • 20
  • 117
  • 138

BecauseIn C++, the length of an array needs to be known at compile time, in order to allocate the arrayit on the stack, the compiler needs to know the size of the array during compiletime.

WhatIf you wantneed to allocate an array whose size you do instead is:not know at compile time, you'll need to allocate it on the heap, using operator new[]

int size = menu.size;
int *list = new int[size];

But since you've new'd memory on the heap, you need to ensure you properly delete it when you are done with it.

delete[] list;

Because in order to allocate the array on the stack, the compiler needs to know the size of the array during compiletime.

What you want to do instead is:

int size = menu.size;
int *list = new int[size];

But since you've new'd memory on the heap, you need to ensure you properly delete it when you are done with it.

delete[] list;

In C++, the length of an array needs to be known at compile time, in order to allocate it on the stack.

If you need to allocate an array whose size you do not know at compile time, you'll need to allocate it on the heap, using operator new[]

int size = menu.size;
int *list = new int[size];

But since you've new'd memory on the heap, you need to ensure you properly delete it when you are done with it.

delete[] list;
Source Link
Alan
  • 47k
  • 20
  • 117
  • 138

Because in order to allocate the array on the stack, the compiler needs to know the size of the array during compiletime.

What you want to do instead is:

int size = menu.size;
int *list = new int[size];

But since you've new'd memory on the heap, you need to ensure you properly delete it when you are done with it.

delete[] list;