When to dynamically allocate memory?

Asked

Viewed 501 times

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 "."

2 answers

6


tipo_da_variável nome_da_variável;

This type of statement is the easiest to use

Yes, but it is wrong. Not initializing a value is an error.

This form is called automatic allocation.

It is true that dynamically allocating objects makes the program faster

No, in general it is the opposite, one should avoid dynamic allocation as much as possible without causing other problems.

makes it occupy less memory and CPU?

These are different things, but dynamic allocation will always consume more memory, invariably. The work to allocate is usually monumental compared to automatic allocation. And if it is not, the release will be monumental. You can’t categorically state why C and C++ let you manage memory in different ways.

I must always dynamically allocate objects?

No, it’s much more complicated to manage.

could exemplify some cases that dynamically allocated memory usage is justifiable?

I think that is already answered in several questions:

After all my compiler accepts the code without errors

Working is different than being right.

Carro Fiat 147 caindo aos pedaços trafegando pelas ruas

In the code above, the size of the array will depend on the value that the user type, IE, at first, we do not know how much memory the program will use and even so

No problem, can make automatic allocation set at runtime, this does not make the allocation be dynamic.

-4

It is important to remember that the object code will be generated. It is possible to perform this analysis. I have read a lot about the architecture of digital games, including there is a book written by the "lead Programmer" of the recognized Naughtydog, in which he talks about it, especially when it will be develop for consoles.

Pointers can give you more flexibility in coding techniques and methods, maybe.

  • 3

    Where this answers the questions presented on the page?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.