0
int numList1[10];

int *numList2 = new int[10];

I have seen too many solutions from others used the second way to assign array, do they work as the same?

  • 1
    The first one is on the stack the second one is on the heap and will therefore leak memory unless `delete` is used, but I advise you to read some good C++ learning resource since this is very basic C++ – Lala5th Apr 12 '22 at 00:36
  • 2
    use std::array for the first one and std::vector for the second one – pm100 Apr 12 '22 at 00:40
  • Handy reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Apr 12 '22 at 00:47
  • 1
    Your terminology is wrong. In neither case in an array being assigned. In the first an array is being defined. In the second a pointer is being defined and being initialized. [You cannot actually assign an array in C++](https://stackoverflow.com/questions/18962441/assign-array-to-array), but you can with `std::array` and `std::vector` – user4581301 Apr 12 '22 at 00:50
  • 1
    thank you for the comments. It is so painful to study c++ from the internet, they always jump to another unknown section suddenly and I can't catch it... – Sloth Cheung Apr 12 '22 at 01:04

0 Answers0