malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

malloc(sizeof(int)) means you are allocating space off the heap to store an int. You are reserving as many bytes as an int requires. This returns a value you should cast to int *. (A pointer to an int.) As some have noted, typical practice in C is to let implicit casting take care of this. malloc(sizeof(int*)) means you are allocating space off the heap … Read more

Double pointer array in c++

You probably well know that double* is a pointer to a double element. In the same way, double** is a pointer to a double* element, which is itself a pointer. Again, double*** is a pointer to a double** element, and so on. When you instanciate an array to a type T, you usually do new T [size];. For example, for an array of double, you write new double[size];. If your type T is … Read more

Static array vs. dynamic array in C++

Local arrays are created on the stack, and have automatic storage duration — you don’t need to manually manage memory, but they get destroyed when the function they’re in ends. They necessarily have a fixed size: Arrays created with operator new[] have dynamic storage duration and are stored on the heap (technically the “free store”). They can … Read more