Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer New | Dynamic Memory
C++ Intermediate | Mobile-Friendly
course content

Cursusinhoud

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
New

Let’s imagine we need a new variable (e.g., x) during the execution of the code and want to allocate the memory for x in a heap (dynamic memory). If we just create x, our variable and static code memory will have no connections, and we can’t handle x.

To have the opportunity to work with the variable, we should create the pointer in the stack (code memory) for our variable. In such a way, you will have access to x for changing it:

In your code, use the operator new to allocate the memory for the variable within the heap and the type of the desired variable (we have no names here). new returns the address of the created variable:

python

We can also simultaneously declare the pointer x, which is dereferenced to access the variable:

python

In the images, we gave the name to our dynamic variable to simplify the explanation. Actually, variables created in a heap have no name, only an address and a pointer to it.

question-icon

Declare the pointer a, which is dereferenced to access the variable:

int =;

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

course content

Cursusinhoud

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
New

Let’s imagine we need a new variable (e.g., x) during the execution of the code and want to allocate the memory for x in a heap (dynamic memory). If we just create x, our variable and static code memory will have no connections, and we can’t handle x.

To have the opportunity to work with the variable, we should create the pointer in the stack (code memory) for our variable. In such a way, you will have access to x for changing it:

In your code, use the operator new to allocate the memory for the variable within the heap and the type of the desired variable (we have no names here). new returns the address of the created variable:

python

We can also simultaneously declare the pointer x, which is dereferenced to access the variable:

python

In the images, we gave the name to our dynamic variable to simplify the explanation. Actually, variables created in a heap have no name, only an address and a pointer to it.

question-icon

Declare the pointer a, which is dereferenced to access the variable:

int =;

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt