Implement a C++ Program to Demonstrate 'this' Pointer and Dynamic Memory Allocation
🔍 Understanding `this` Pointer and Dynamic Memory Allocation in C++
C++ is a powerful language that provides features for both low-level and high-level programming. Two key features every C++ developer must understand are the `this` pointer and dynamic memory allocation.
In this blog post, we will:
- Understand what the `this` pointer is
- Explore dynamic memory allocation using `new` and `delete`
- Implement a C++ program that demonstrates both concepts
✅ What is the `this` Pointer?
The `this` pointer is an implicit pointer available in all non-static member functions. It points to the calling object of the class.
Use cases of `this` pointer:
- To resolve naming conflicts between class members and function parameters
- To return the current object from a member function
- In operator overloading and constructor chaining
✅ What is Dynamic Memory Allocation?
Dynamic memory allocation in C++ allows us to allocate memory at runtime using:
- `new` operator – allocates memory on the heap
- `delete` operator – frees the memory allocated with `new`
This is especially useful when the size of the data is not known at compile time.
🧑💻 C++ Program: Demonstrating `this` Pointer and Dynamic Memory Allocation
#include <iostream>
using namespace std;
class Student {
private:
string name;
int* marks; // Pointer for dynamic memory allocation
public:
// Constructor
Student(string name, int mark) {
this->name = name; // Using 'this' to resolve ambiguity
marks = new int; // Dynamic memory allocation
*marks = mark;
}
// Function to display data
void display() {
cout << "Student Name: " << this->name << endl;
cout << "Marks: " << *this->marks << endl;
}
// Destructor to free allocated memory
~Student() {
delete marks; // Free the dynamically allocated memory
cout << "Memory deallocated for " << name << endl;
}
};
int main() {
Student s1("Vivek", 85); // Creating object using constructor
s1.display(); // Displaying student details
return 0;
}
🖨️ Output
Student Name: Vivek
Marks: 85
Memory deallocated for Vivek
🧠 Code Explanation
- The class `Student` contains a `name` and a pointer `marks`.
- In the constructor, we use the `this` pointer to assign values to class members (especially useful when parameter names are the same as member names).
- Memory for `marks` is allocated at runtime using `new`.
- The destructor uses `delete` to free memory when the object is destroyed.
- The `display()` function uses `this` pointer to access and print class members.
🚀 Benefits of Using `this` Pointer and Dynamic Memory
- Prevents naming conflicts.
- Makes code clean and understandable.
- Allows flexible memory usage at runtime.
- Prevents memory leaks when `delete` is used properly.
⚠️ Important Notes
- Always pair `new` with `delete` to avoid memory leaks.
- Avoid using `this` pointer unless necessary to avoid cluttering code.
📝 Conclusion
The `this` pointer and dynamic memory allocation are **essential tools in C++** for writing efficient and flexible object-oriented code. By understanding how to use them correctly, you can build powerful applications that handle memory responsibly and maintain object integrity.
Comments
Post a Comment