2. The heap is faster than the stack, as it's dynamic. Which also means you can remove old or add new elements to the vector -- arrays on the stack are slower and they are fixed-size -- you can't add a new element to an automatic array; whereas with a dynamic container, such as a vector, being dynamic, you can add and remove elements. For example if you create a vector of 10 elements but need 15 at some point; you can use std::vector::push_back(value) to create a new element.
To add something: a std::vector is not really efficient if you are going large amounts of memory or if you know that the size of your array is going to change several time. For these a std::deque or a std::list are better
char 8 myCharArray; //This creates a pointer that handles or points to memory that we are going to allocate.
myCharArray = newchar[1000]; //This is the same as saying malloc(sizeof(char) * 1000). It allocates enough space for 1000 chars which comes out to be since each character is one byte, 1000 bytes.
1- which advantages do i get if i implement them as vector?
A vector mirrors the syntax and semantics of an array. But they differ in that it uses heap storage rather than the stack; there is a small stack allocation. A vector can provide range checking on the index. A vector can grow or shrink.
2- which benefit do i get if the data are stored in the heap and not in the stack?
The stack is a very limited resource. Just because the stack grows dynamically on the commonplace desktop OS', doesn't mean it's generally true. DOS apps have a 2K stack by default.
Heap usage can grow and shrink. Stack usage only grows (until the a pop occurs).
You have to copy stack objects that are returned, whereas heap objects are used in place.
A vector is similar to an array except it allows dynamic allocation to become easier. The stack allocation is similar to that of the array stack allocation. Did you guys ever know that the standard way of writing a pointer was once char myPointer[] This is also the reason why int main(int argc, char * argv[]) is allowed. argv[][] is not allowed for various reason I will not go into.
Stack should almost always never be used except for small and conventional reason. more latar
Aren't 2D arrays really just single-dimensional arrays with added syntactic sugar? I'm fairly sure int myArray[10][10];
is the same as int myArray[100];?
In which case char screen_buffer[COLUMNS][ROWS]
is the same as char screen_buffer[COLUMNS * ROWS]
which I think ~= char* screen_buffer = malloc((sizeof(char) * COLUMNS * ROWS));.
Of course! It's a pointer to a pointer, what am I thinking? char** pStr;
If you accessed it as a 1-Dimensional array then you wouldn't be dereferencing the second pointer. Ahhh, I get it now.