This is not a linked list, but a construct known as dynamic memory. If you want to create an array normally, you would write something like
int myArray[3] = {
1, 2, 3
};
Notice that when I write the size of myArray
(3), I use a constant expression; If I were to write
int x = 3;
int myArray[x] = {
1, 2, 3
};
It wouldn’t work, because x
is a variable, not a constant expression. Note that even if x
is a const
, this still wouldn’t work. This is because before the program is run, memory needs to be allocated for the arrays.
When you say int * myData = new int[3]
, you can create memory on the fly, without constant expressions. In this case, it would be perfectly fine to do this
int x = 3;
int * myData = new int[x];
However, myData
is a pointer, not an array, so the common sizeof(x) / sizeof(*x)
trick doesn’t work here.
Make sure that after your dynamic memory is no longer of use, you write delete[] myData
. This de-allocates the memory block and allows other memory to be stored in its place.
This memory is a block of memory just like an array. A linked list, however, is totally different, and would take a while to explain. Check out the Wikipedia on it.