I saw this code:
int n;
cin>>n;
int c[n];
What's the difference compared to this
int *c=new int[n];
I saw this code:
int n;
cin>>n;
int c[n];
What's the difference compared to this
int *c=new int[n];
The first is not valid in c++ because the size needs to be known at compile time since your data will be stored on the stack plus it has a limited scope.
The latter allocates memory for your object on the heap and will persist until manually deleted or the program execution stops.
In the first example:
int n;
cin >> n;
int c[n];
This code simply declares a variable n, accepts an integer value from the user which is stored in n, and declares an integer array of size n
.
The problem here, is that you cannot have an array of variable length in your code if your compiler does not support it, because this is not allowed in c++. However, modern compilers, like the later version of the gcc compiler, support variable sized arrays, and therefore, allow variable arrays to overcome the obstacle of c++ not supporting them.
I do not know all the versions of the gcc compiler that support variable arrays, but I've tried gcc version 4.6.3, and it supports variable arrays.
Now, moving on to your second example,
int *c = new int[n];
This is a completely different concept. Here, int *c
Allocates memory to hold a single element of type int. Then new int [n]
allocates a block of n elements in memory of type int, with the address of the first element stored in c. Therefo, c points to an array of 5 ints. For further reading, visit: Dynamic Memory.
In the first example you instantiate a thing called Variable Length Array. VLAs are not defined in C++ Standard so they don't belong to the language. They are sometimes enabled by the various compilers as an extension.
VLAs have automatic storage duration which means such an array is allocated at the beginning of a code block and deallocated at the end.
Typically they are allocated on the stack (if they fit) but it's not guaranteed. So again typically they will have smaller allocation/deallocation cost compared to dynamic memory allocation (2nd example) which takes place on the heap.
Also typically VLAs allow using sizeof
on them and sizeof(VLA)
typically returns array_size * sizeof(type)
. This is true for gcc
with compilation flag -std=c++14
applied.
You probably noticed a lot of typically - the thing is, as VLAs are not defined by the C++ Standard, you have to dig into their specification for the compiler you use. So best practice for C++ - don't use VLAs.
In your second example you perform dynamic memory allocation. After this is completed c
points to the beginning of newly allocated block of memory of size n * sizeof(int)
bytes.
c
has dynamic storage duration meaning it is allocated by you and must be deallocated by you (in this case using delete[] c
;
As mentioned above cost of allocation/deallocation on the heap is higher.
In C++ you should try to use std::vector<T>
wherever possible. It's much safier, can grow as you add new elements and is not terribly slower.