What is the difference from an Array compared to Dynamic Array ?
The main difference is that a dynamic array is created on the heap. A dynamic array's size can be determined at runtime.
The difference in the below:
int x = 10;
int array1[10];
int *array2 = new char[x];
Is that array2 is pointing to the first element of an array and not the actual full array.
Note:
assert(sizeof(array1) == 40);
assert(sizeof(array2) == 4);
Memory on the heap that is created with new should eventually be destroyed using delete.
Since array2 is created on the heap, and is an array you will need to delete it with delete[] though.
Note: You can actually create a pointer to a full array and not just the first element:
int array1[10];
int *pointerToFirstElement = array1;
int (*pointerToFullArray)[10] = &array1;
int sizeOfFirstPointer = sizeof(pointerToFirstElement);
int sizeOfSecondPointer = sizeof(pointerToFullArray);
assert(sizeOfFirstPointer == sizeOfSecondPointer);//==4 since both are pointers
However what they point to have different sizes:
int sizeOfFirst = sizeof(*pointerToFirstElement);
int sizeOfSecond = sizeof(*pointerToFullArray);
assert(*sizeOfFirst == 4);
assert(*sizeOfSecond == 40);
A dynamic array is created from heap memory at run time and can be dynamically resized/freed as necessary with the new/delete keywords. An array is statically defined at compile time and will -always- take that amount of memory at all times.