In C ++ you can easily declare an object or variable like this:
tipo_da_variável nome_da_variável;
This type of declaration is the easiest to use, but you can also use new
to allocate memory dynamically and then deallocate with delete.
Is it true that dynamically allocating objects leaves the program faster, ie does it take up less memory and CPU?
Should I always dynamically allocate objects?
If the answer to these 2 questions is no, then could you exemplify some cases where the use of dynamically allocated memory is justifiable? Or explain to me the usefulness of dynamically allocating memory?
I've heard that allocating memory dynamically is something that should be done when we do not know how much memory we'll need, but I also did not understand this argument right, after all my compiler accepts without error the code:
int num1;
cin >> num1;
char palavra[num1];
In the code above, the size of the array will depend on the value that the user type, that is, the start, we do not know how much memory the program will use and even then, required new
+ delete