Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dynamic Allocation of the Array | Dynamic Memory Allocation
C++ Pointers and References
course content

Зміст курсу

C++ Pointers and References

C++ Pointers and References

1. Pointers Fundamentals
2. Pointer Arithmetic
3. References Fundamentals
4. Dynamic Memory Allocation

bookDynamic Allocation of the Array

Before we explore why dynamic allocation is necessary, let's quickly recap the characteristics of static and dynamic arrays:

  • Fixed Size: Once declared, the size of a static array is fixed and cannot be changed during runtime;
  • Memory Allocation at Compile Time: The memory required for a static array is allocated at compile time.
  • Resizable: Dynamic arrays allow for resizing during runtime, providing flexibility to adapt to changing program requirements;
  • Memory Allocation at Runtime: Memory for dynamic arrays is allocated during program execution.

The Limitations of a Static Approach

Consider the program that prompts the user to input performance scores for each day that has passed in current month.

Unfortunately, we can't achieve this using a static array:

cpp

main

copy
12345678910
#include <iostream> #include <ctime> int main() { std::time_t currentTime = std::time(nullptr); int day_passed = std::localtime(&currentTime)->tm_mday; int arr[day_passed]; std::cout << day_passed << std::endl; }

Note

This will generate a compilation error because day_passed is not a constant expression it depends on the runtime value of the current day of the month.

So instead of static array we have to use a dynamic allocated array.

Завдання
test

Swipe to show code editor

  • Create a dynamic array with a size equivalent to the number of days that have passed.
  • Free allocated memory.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 3
toggle bottom row

bookDynamic Allocation of the Array

Before we explore why dynamic allocation is necessary, let's quickly recap the characteristics of static and dynamic arrays:

  • Fixed Size: Once declared, the size of a static array is fixed and cannot be changed during runtime;
  • Memory Allocation at Compile Time: The memory required for a static array is allocated at compile time.
  • Resizable: Dynamic arrays allow for resizing during runtime, providing flexibility to adapt to changing program requirements;
  • Memory Allocation at Runtime: Memory for dynamic arrays is allocated during program execution.

The Limitations of a Static Approach

Consider the program that prompts the user to input performance scores for each day that has passed in current month.

Unfortunately, we can't achieve this using a static array:

cpp

main

copy
12345678910
#include <iostream> #include <ctime> int main() { std::time_t currentTime = std::time(nullptr); int day_passed = std::localtime(&currentTime)->tm_mday; int arr[day_passed]; std::cout << day_passed << std::endl; }

Note

This will generate a compilation error because day_passed is not a constant expression it depends on the runtime value of the current day of the month.

So instead of static array we have to use a dynamic allocated array.

Завдання
test

Swipe to show code editor

  • Create a dynamic array with a size equivalent to the number of days that have passed.
  • Free allocated memory.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 3
toggle bottom row

bookDynamic Allocation of the Array

Before we explore why dynamic allocation is necessary, let's quickly recap the characteristics of static and dynamic arrays:

  • Fixed Size: Once declared, the size of a static array is fixed and cannot be changed during runtime;
  • Memory Allocation at Compile Time: The memory required for a static array is allocated at compile time.
  • Resizable: Dynamic arrays allow for resizing during runtime, providing flexibility to adapt to changing program requirements;
  • Memory Allocation at Runtime: Memory for dynamic arrays is allocated during program execution.

The Limitations of a Static Approach

Consider the program that prompts the user to input performance scores for each day that has passed in current month.

Unfortunately, we can't achieve this using a static array:

cpp

main

copy
12345678910
#include <iostream> #include <ctime> int main() { std::time_t currentTime = std::time(nullptr); int day_passed = std::localtime(&currentTime)->tm_mday; int arr[day_passed]; std::cout << day_passed << std::endl; }

Note

This will generate a compilation error because day_passed is not a constant expression it depends on the runtime value of the current day of the month.

So instead of static array we have to use a dynamic allocated array.

Завдання
test

Swipe to show code editor

  • Create a dynamic array with a size equivalent to the number of days that have passed.
  • Free allocated memory.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Before we explore why dynamic allocation is necessary, let's quickly recap the characteristics of static and dynamic arrays:

  • Fixed Size: Once declared, the size of a static array is fixed and cannot be changed during runtime;
  • Memory Allocation at Compile Time: The memory required for a static array is allocated at compile time.
  • Resizable: Dynamic arrays allow for resizing during runtime, providing flexibility to adapt to changing program requirements;
  • Memory Allocation at Runtime: Memory for dynamic arrays is allocated during program execution.

The Limitations of a Static Approach

Consider the program that prompts the user to input performance scores for each day that has passed in current month.

Unfortunately, we can't achieve this using a static array:

cpp

main

copy
12345678910
#include <iostream> #include <ctime> int main() { std::time_t currentTime = std::time(nullptr); int day_passed = std::localtime(&currentTime)->tm_mday; int arr[day_passed]; std::cout << day_passed << std::endl; }

Note

This will generate a compilation error because day_passed is not a constant expression it depends on the runtime value of the current day of the month.

So instead of static array we have to use a dynamic allocated array.

Завдання
test

Swipe to show code editor

  • Create a dynamic array with a size equivalent to the number of days that have passed.
  • Free allocated memory.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 3
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt