Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Dynamische Zuweisung | Dynamische Speicherzuweisung
C++ Zeiger und Referenzen

bookDynamische Zuweisung

Dynamic memory allocation involves using operators new and delete. These operators allow you to allocate memory for variables and arrays at runtime, providing greater flexibility than static memory allocation.

  • new: operator that is used to dynamically allocate memory for an object or an array of objects during runtime;

  • delete: operator that is used to deallocate memory that was previously allocated with the new operator.

Note
Note

A pointer pointing to dynamically allocated memory is typically stored on the stack, but the memory it points to is allocated on the heap.

To create an integer variable dynamically you have to use a pointer along with the new keyword.

int *dynamicInteger = new int;

To free the dynamically allocated memory, you use the delete operator:

delete dynamicInteger;

Make it a rule for yourself: when you use new to allocate memory, always use delete to free it up later.

Dynamic Allocated Arrays

When allocating memory for arrays dynamically, use the new[] operator, and when releasing memory for dynamically allocated arrays, use the delete[] operator.

Note
Note

Failure to delete dynamically allocated memory can result in memory leaks, where the program retains memory that is no longer in use.

Aufgabe

Swipe to start coding

Imagine you are working as a teacher who needs to calculate the average grade of a group of students.

You will also work with pointers and functions that manipulate dynamically allocated arrays.

  1. Create a dynamically allocated array grades to store students' grades.
  2. Use the fillRandomGrades function to fill the array with random grades between 60 and 100.
  3. Complete the calculateAverage function so that it:
    • Initializes the variable sum to 0.
    • Iterates through the array using a for loop.
    • Adds each grade to sum.
    • Returns the average grade by dividing the total sum by the number of students.
  4. Remember to release the allocated memory using delete[] at the end of the program to avoid memory leaks.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookDynamische Zuweisung

Swipe um das Menü anzuzeigen

Dynamic memory allocation involves using operators new and delete. These operators allow you to allocate memory for variables and arrays at runtime, providing greater flexibility than static memory allocation.

  • new: operator that is used to dynamically allocate memory for an object or an array of objects during runtime;

  • delete: operator that is used to deallocate memory that was previously allocated with the new operator.

Note
Note

A pointer pointing to dynamically allocated memory is typically stored on the stack, but the memory it points to is allocated on the heap.

To create an integer variable dynamically you have to use a pointer along with the new keyword.

int *dynamicInteger = new int;

To free the dynamically allocated memory, you use the delete operator:

delete dynamicInteger;

Make it a rule for yourself: when you use new to allocate memory, always use delete to free it up later.

Dynamic Allocated Arrays

When allocating memory for arrays dynamically, use the new[] operator, and when releasing memory for dynamically allocated arrays, use the delete[] operator.

Note
Note

Failure to delete dynamically allocated memory can result in memory leaks, where the program retains memory that is no longer in use.

Aufgabe

Swipe to start coding

Imagine you are working as a teacher who needs to calculate the average grade of a group of students.

You will also work with pointers and functions that manipulate dynamically allocated arrays.

  1. Create a dynamically allocated array grades to store students' grades.
  2. Use the fillRandomGrades function to fill the array with random grades between 60 and 100.
  3. Complete the calculateAverage function so that it:
    • Initializes the variable sum to 0.
    • Iterates through the array using a for loop.
    • Adds each grade to sum.
    • Returns the average grade by dividing the total sum by the number of students.
  4. Remember to release the allocated memory using delete[] at the end of the program to avoid memory leaks.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
single

single

some-alt