Programming Code Examples

Dynamic Memory Allocation for Array of Objects in C++5 min read

Dynamic memory allocation for array of objects

In the previous topic memory for array of objects was static as the memory was allocated at compile time. To allocate memory dynamically or at run time, we use an array of pointers which can be created as follows:

ClassName *array-name[size];




Memory allocated for an array of pointers is far less than memory allocated for an array of objects. We can create an object at run time using the newoperator as follows:

array-name[index] = new ClassName;

After dynamically allocating memory for an object we access the members using the -> operator as follows:

array-name[index]->member;

Below example demonstrates how to dynamically allocate memory for an array of objects:

Input and output for the above program is as follows:

This code defines a C++ class called “Student” which has private member variables for storing the student’s name, registration number, branch, and age. The class also has two public member functions: “get_details” and “show_details”.

The “get_details” function prompts the user to input the student’s name, registration number, branch, and age, and stores these values in the private member variables of the class.

The “show_details” function displays the student’s details, including their name, registration number, branch, and age.

In the main function, an array of 3 pointers to Student objects is created. A for loop is used to iterate through the array, and for each iteration, a new Student object is created and allocated memory using the “new” operator. The “get_details” function is then called for each Student object, prompting the user to enter the student’s details.

Another for loop is used to iterate through the array of Student objects and for each iteration, the “show_details” function is called to display the student’s details.

Finally, the program returns 0 to indicate successful execution.

The program can be used to store and display the details of multiple students. The use of pointers and the “new” operator allows for dynamic allocation of memory for an unknown number of Student objects.

Objects As Function Arguments

Like variables, objects can also be passed using pass-by-value, pass-by-reference, and pass-by-address. In pass-by-value we pass the copy of an object to a function. In pass-by-reference we pass a reference to the existing object. In pass-by-address we pass the address of an existing object. Following program demonstrates all three methods of passing objects as function arguments:

The code is a C++ program that defines a class “Student” with private member variables name, regdno, age and public member variable branch. The class has two public member functions: get_details and show_details.

The main function starts by creating four objects of the Student class: s1, s2, s3, and s4.

s1.branch is assigned the value “cse”, which sets the branch of s1 to “cse”.

The set_branch function is called on s2, with s1 passed as an argument. This sets the branch of s2 to the same value as s1’s branch, which is “cse”.

The set_branch function is called on s3, with s1 and an integer value 10 passed as arguments. This sets the branch of s3 to the same value as s1’s branch, which is “cse”.

The set_branch function is called on s4, with the address of s1 passed as an argument. This sets the branch of s4 to the same value as s1’s branch, which is “cse”.

The program ends with a return statement in the main function.

Notice that this code will not run as is because the set_branch functions are missing the implementation and have errors in the parameters.

Take your time to comment on this article.

Add comment