Javatpoint Logo
Javatpoint Logo

DYNAMIC ARRAY:

A dynamic array is similar to a regular array. A dynamic array can be modifiable but only the difference is during program runtime. A contiguous block of memory is occupied by the dynamic array elements.

The size of a dynamic array cannot be changed once the creation of an array has been started. So a dynamic array with somehow different from others. Even after the dynamic array has there been filled, its size can be expanded.

A predetermined amount of memory is allocated at the time of the creation of an array. As the dynamic array expands, its memory size may be various factors at the time of requirement, so remove the case it's not for a dynamic array.

Various factors affecting the performance of dynamic array:

The performance of an array is determined by its growth factor and its initial size. Go Packers affecting the performance are as follows:

  • If a small growth factor and a small size are available in an array, then reallocating memory will keep on more often. The performance of an array can be reduced by this factor.
  • Considering another factor, if in case of a large growth factor and a large size of an array, then an array has a huge chunk of unused memory. The resize operation can take a longer time due to this factor, and for that, the performance of an array can be reduced.

The new keyword:

In C++, using the new keyword weekend allocated dynamic array in it. Within a pair of square buckets, the required number of items that are to be allocated is specified. The type name should precede this keyword. The number of items that are requested will be allocated.

Syntax:

The syntax for the new keyword is as follows:

Description of the syntax:

The pointer_var is written for the name of the pointer variable.

As per the above syntax, the written data type should be a valid C++ data type.

After that, the first item a pointer will be returned by the keyword. Then after the creation of a dynamic array, we can delete it from the statement using the delete keyword.

Some examples of dynamic array allocation:

Example: 1

Output:

Enter the number of items that you want to store:
2
Enter the 2 items that you want to store:
10 11
You entered the following things: 10 11

Explanation of the above example code:

  1. First of all, in our program, we will include the iostream header file for using its function.
  2. Then, in our program, we will include the std namespace without calling it in order to use its classes.
  3. After that, we will call the main function. Within the body of the function, the logic of the program should be added.
  4. Then we declared two variables, such as p and q, of integer type.
  5. Then, to enter the value of variable q, we have to prompt the user to print some text on the console.
  6. After that, the input from the keyboard by the user will be taken and will be assigned this value to the variable q.
  7. To keep a total of q integer and to assign it to the pointer variable *arr, you have to declare an array for it.
  8. For entering the queue number of items, we have to print a message for the user.
  9. Then, a loop will be created for the variable P to iterate over the items entered by the users.
  10. The elements entered by the user will be read and will be stored in this array arr.
  11. Then the loop for the body will be ended.
  12. After that, some text would be printed on the console.
  13. After that, we will use for loops to create a loop variable P to iterate over the array's items.
  14. Then, the values contained in the array named arr will be printed on the console.
  15. Then the body of for loop will be ended.
  16. After successfully completing the program, the value should be returned.
  17. At last, the main function of the body will be ended.

In this above-written example, the user can mention any desired size of the array at the time of its running of it. So from that, we can understand that the size of an array can be determined at the time of the allocation.

Initialization of allocated dynamic array:

Initialization of an array to 0 is an easy process.

The syntax for the allocation:

Description of the above syntax:

The above syntax shows the number of elements from the length that is to be added to the array.

This place should be empty because the array will initialize to 0. With the help of an initializer list, we can initialize a dynamic array. No, we will create an example that demonstrates this array.

Example:

Output:

Entered Array elements are:
9
6
12
2
13

Explanation of the above-written code:

  1. First of all, in our program, we will include the iostream header file for using its function.
  2. Then, in our program, we will include the std namespace without calling it in order to use its classes.
  3. After that, we will call the main Within the body of the function, the logic of the program should be added.
  4. Then we declared one variable, such as p of integer type.
  5. With the help of the initializer list, we will declare an array named array. 5 integers can be held inside the array. The important point is in between the initializer list and the array length; there is no use of the = operator.
  6. Some text will be printed. The meaning of the endl keyword in c++ is the end of a line. The cursor by this keyword is moved to the next sentence.
  7. A for loop will be used to iterate over the array element.
  8. The content of the array named array will be printed on the console.
  9. Here the body of for loop will be ended.
  10. So, upon the successful completion of the program, it should return its value of it.
  11. At last, here, the main function will be ended.

Array resizing:

During the time of allocation, the length of a dynamic array is set. Once it has been allocated, there is no built-in function for resizing the array inside c++.

By the allocation of a new array, with copying the elements further erasing the old array, we can overcome this challenge. In this process, many errors can occur, so we have to be aware of this.

Deletion of an array dynamically:

Once the purpose of an array is completed from the computer memory, the dynamic array should be deleted. For this action, the delete statement helps in accomplishing. The released memory space created after the deletion of the array can be used to keep another set of data. However, once the program terminates, the dynamically allocated array can be deleted automatically even if from the computer memory we don't use the array.

Some important points:

Instead of using delete, we have to use delete[] for the deletion of the dynamic array from the computer memory. Rather than one variable to delete multiple variables [], instruct the CPU to do it. But while dealing with the dynamic array, it may cause problems if we use delete in place of delete[].

Data corruption, memory leaks, crashes, etc., are examples of this problem.

Example:

Output:

Amount of numbers that you will type
3
Type these 3 required numbers
1 2 3
The typed number as follows: 1 2 3

Explanation of the above example code:

  1. First of all, in our program, we will include the iostream header file for using its function.
  2. Then, in our program, we will include the std namespace without calling it in order to use its classes.
  3. After that we will call the main function. Within the body of the function, the logic of the program should be added.
  4. Then we declared two variables, such as p and q, of integer type.
  5. Then, to enter the value of variable q, we have to prompt the user to print some text on the console.
  6. After that, the input from the keyboard by the user will be taken and will be assigned this value to the variable q.
  7. To keep a total of q integer and to assign it to the pointer variable *arr, you have to declare an array for it.
  8. For entering the q number of items, we have to print a message for the user.
  9. Then for loop will be created for the variable P to iterate over the items entered by the users.
  10. The elements entered by the user will be read and will be stored in this array arr.
  11. Then for loop body will be ended.
  12. After that, on the console, some text would be printed.
  13. After that we will use for loops for the creation of a loop variable P to iterate over the items of the array.
  14. Then, the values contained in the array named arr will be printed on the console.
  15. Then the body of for loop will be ended.
  16. After that, an empty line will be printed on the console.
  17. The memory to the array arr will be freed up.
  18. When the program is completed successfully, the program will return the value.
  19. At last, the main function will be ended.

Summary:

In this above context, we have summarized these points as follows:

  • There is a fixed size of the regular size array. Once the regular size array is declared then we can't change their size.
  • During the time of compilation, the size of the memory is determined with these types of arrays.
  • But excluding these, the type of dynamic array is completely different. During runtime, the size of this array can be changed.
  • Using the new keyword in c++, we can declare the dynamic array.
  • For specifying the number of items, we use the square bracket for storing in a dynamic array.
  • After the completion, we can free up the memory with the help of the delete operator.
  • For all array elements we use the delete operator along with [] to free up the memory.
  • For resizing of c++ array, there is no built-in array mechanism.

Next TopicSTRCMP() IN C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA