The C++ new keyword is a fundamental aspect of memory management in C++, and when used with struct, it enables dynamic creation of structured data types at runtime. This article delves into the intricacies of using the new keyword with struct in C++, providing a comprehensive guide that balances technical depth with accessibility.
Understanding the new Keyword in C++
The new keyword in C++ is used to allocate memory dynamically on the heap. It returns a pointer to the newly allocated memory, which can be used to access and manipulate the data stored there. The general syntax of the new keyword is:
type* pointer_name = new type;
Defining a struct in C++
Before we dive into using new with struct, let's first understand what a struct is. A struct in C++ is a user-defined data type that allows us to combine data items of different kinds. Here's a simple example:

struct Student {
std::string name;
int age;
double gpa;
};
Using new with struct
Now that we have a basic understanding of both new and struct, let's see how we can use them together. The new keyword can be used to dynamically allocate memory for a struct, allowing us to create instances of our structured data type at runtime. Here's how you can do it:
Student* student_ptr = new Student;
In this example, a new Student struct is created on the heap, and a pointer to it is stored in the student_ptr variable.
Initializing Structs with new
Often, you'll want to initialize the members of your struct when you create it. You can do this by providing values in the parentheses after the new keyword:

Student* student_ptr = new Student{"John Doe", 20, 3.5};
In this case, the name, age, and gpa members of the Student struct are initialized with the provided values.
Working with Pointers to Structs
When you use new to create a struct, you're working with a pointer to that struct. To access the members of the struct, you'll need to dereference the pointer:
student_ptr->name = "Jane Smith";
student_ptr->age = 22;
student_ptr->gpa = 3.8;
Alternatively, you can use the arrow operator (->) to access the members of the struct:

std::cout << student_ptr->name << " is " << student_ptr->age << " years old and has a GPA of " << student_ptr->gpa << std::endl;
Deleting Dynamically Allocated Memory
When you're done using a struct created with new, it's crucial to delete the memory to prevent memory leaks. You can do this with the delete keyword:
delete student_ptr;
If your struct contains dynamically allocated memory (like a dynamically allocated array), you'll need to use delete[] to delete it:
int* arr = new int[5];
...
delete[] arr;
Structs and the Rule of Three/Five/Zero
When working with dynamically allocated structs, it's essential to understand the Rule of Three, Five, and Zero. These rules help prevent issues related to copying and deleting objects. We won't delve into the details here, but it's crucial to be aware of these rules when working with dynamically allocated memory.
That concludes our comprehensive guide on using the new keyword with struct in C++. By understanding and applying these concepts, you'll be well on your way to mastering memory management in C++.






















