I know of 2 ways to dynamically allocate memory. I understand how “new” works, but I don’t understand how “static cast” works:
double* A1;
double** A2;
//1D array
A_1 = new double[size];
A_1 = static_cast <double*> (new double[c]);
//2D array
A_2 = reinterpret_cast <double**> (new double[r]);
for(i = 0; i < r; i++)
A_2[i] = static_cast <double*> (new double[c]);
I don’t understand how static and reinterpret are working.
While new is the keyword for allocating a new object, static_cast and reinterpret_cast serve a different purpose and, as others have pointed out by now, do not allocate memory.
A short explanation is that static_cast converts between pointers to compatible classes or between compatible non-pointer types. So you could cast a Vehicle pointer to a Car pointer or the other way around, but there is no check at runtime that your Vehicle is actually a Car when you cast it. dynamic_cast on the other hand features a runtime check.
reinterpret_cast simply converts the pointer even if the types are not compatible.
There are plenty of resources where you can find a more in-depth explanation, like http://www.cplusplus.com/doc/tutorial/typecasting/