4
In C++ you can easily declare an object or variable like this:
tipo_da_variável nome_da_variável;
This type of statement is the easiest to use, but you can also use the new
to dynamically allocate memory and then de-locate with delete.
It is true that dynamically allocating objects makes the program faster, that is, it occupies less memory and CPU?
I must always dynamically allocate objects?
If the answer to these 2 questions above is no, then could you exemplify some cases that dynamically allocated memory usage is justifiable? Or explain to me the usefulness of dynamically allocating memory?
I have heard that allocating memory dynamically is something that should be done when we do not know how much memory we will need, but I also did not understand this argument right, after all my compiler accepts the code without errors:
int num1;
cin >> num1;
char palavra[num1];
In the code above, the size of the array will depend on the value that the user enters, ie, at first, we do not know how much memory the program will use and even then, it was not necessary to use new
+ delete
The "pre-created" objects in QT are always dynamically allocated, to access them use "->" and not "."
– silash35