New Delete
In object-oriented programming (OOP) with C++, pointers play a crucial role in managing memory and accessing objects dynamically. Here’s a brief explanation of pointers in the context of OOP in C++:
Definition:
A pointer is a variable that holds the memory address of another variable. In C++, pointers can be used to store addresses of objects created dynamically on the heap.
Example 1:
int main() { int var1 = 11; int var2 = 22; cout << &var1 << endl; cout << &var2 << endl; int *ptr; ptr = &var2; *ptr = 5000; cout << *ptr << endl; cout << var2 << endl; }
This code demonstrates the use of pointers to manipulate variables and access their memory addresses.
- Variable Declaration and Initialization:
int var1 = 11; int var2 = 22;
Two integer variables, var1 and var2, are declared and initialized with values 11 and 22, respectively.
- Printing Memory Addresses:
cout << &var1 << endl; cout << &var2 << endl;
The memory addresses of var1 and var2 are printed using the address-of operator (&). This reveals the locations in memory where these variables are stored.
- Pointer Declaration and Assignment:
int *ptr; ptr = &var2;
A pointer variable ptr of type int is declared. The address of var2 is assigned to the pointer ptr.
- Using Pointer to Modify Variable:
*ptr = 5000;
The value pointed to by ptr is changed to 5000. Since ptr points to the address of var2, this also modifies the value of var2.
- Printing Pointer Value and Modified Variable:
cout << *ptr << endl; cout << var2 << endl;
The value pointed to by ptr (5000) and the modified value of var2 are printed. Both values should be the same, as the pointer was used to modify the content of var2.
The output of the code might look like this:
0x7fff5fbff628 // Memory address of var1 0x7fff5fbff624 // Memory address of var2 5000 // Value pointed to by ptr 5000 // Modified value of var2
The exact memory addresses and values may vary depending on the system and compiler.
Dynamic Memory Allocation:
OOP often involves the creation of objects using the new operator for dynamic memory allocation. Pointers are used to store the address of the dynamically allocated objects.
Deleting Dynamic Objects:
When dynamic objects are no longer needed, they should be explicitly deleted to avoid memory leaks. The delete operator is used.
Example 2:
#include <iostream> using namespace std; int main() { int *p; p = new int; *p = 10; cout << p << endl; delete p; }
This code demonstrates dynamic memory allocation and deallocation for an integer variable. Here’s an explanation of the code:
#include <iostream> using namespace std; int main() { // Declare a pointer variable int *p; // Allocate memory for an integer on the heap p = new int; // Assign a value to the dynamically allocated integer *p = 10; // Print the address of the dynamically allocated memory cout << p << endl; // Deallocate the dynamically allocated memory delete p; return 0; }
Explanation:
- Pointer Declaration:
int *p;
Declares a pointer variable p that will be used to store the address of a dynamically allocated integer.
- Dynamic Memory Allocation:
p = new int;
Allocates memory on the heap to store an integer and assigns the address of the allocated memory to the pointer p.
- Assigning a Value:
*p = 10;
Dereferences the pointer p and assigns the value 10 to the dynamically allocated integer.
- Printing the Address:
cout << p << endl;
Prints the address of the dynamically allocated memory. This address is on the heap and may vary each time the program runs.
- Memory Deallocation:
delete p;
Deallocates the memory previously allocated with new. This step is crucial to prevent memory leaks.
Example 3:
#include <iostream> using namespace std; int main() { // Declare two pointer variables int *p1, *p2; // Allocate memory for the first integer on the heap p1 = new int; *p1 = 10; // Print the location of the pointer and the content of the allocated memory cout << &p1 << endl; // location of the pointer cout << "Memory location " << p1 << "\n"; cout << " contains " << *p1 << endl; // Allocate memory for the second integer on the heap p2 = new int; *p2 = 10; // Print the location of the second pointer and the content of the allocated memory cout << &p2 << endl; // location of the pointer cout << "Memory location " << p2 << "\n" << " contains " << *p2 << endl; // Deallocate the dynamically allocated memory delete p1; delete p2; return 0; }
This code demonstrates dynamic memory allocation for two integer variables using pointers. Here’s an explanation of the code:
- Pointer Declaration:
int *p1, *p2;
Declares two pointer variables, p1 and p2, which will be used to store the addresses of dynamically allocated integers.
- Dynamic Memory Allocation (p1):
p1 = new int; *p1 = 10;
Allocates memory on the heap for an integer using new, assigns the address to p1, and assigns the value 10 to the dynamically allocated integer.
- Printing Information (p1):
cout << &p1 << endl; // location of the pointer cout << "Memory location " << p1 << "\n"; cout << " contains " << *p1 << endl;
Prints the location of the pointer variable p1 and the content of the memory it points to.
- Dynamic Memory Allocation (p2):
p2 = new int; *p2 = 10;
Allocates memory on the heap for another integer using new, assigns the address to p2, and assigns the value 10 to the dynamically allocated integer.
- Printing Information (p2):
cout << &p2 << endl; // location of the pointer cout << "Memory location " << p2 << "\n" << " contains " << *p2 << endl;
Prints the location of the pointer variable p2 and the content of the memory it points to.
- Memory Deallocation:
delete p1; delete p2;
Deallocates the dynamically allocated memory using delete to avoid memory leaks.
Example 4:
#include <iostream> using namespace std; class CRectangle { int *width, *height; public: CRectangle(int, int); //constructor ~CRectangle(); //destructor int area() { return (*width * *height); } }; CRectangle::CRectangle(int a, int b) { width = new int; height = new int; *width = a; *height = b; } CRectangle::~CRectangle() { delete width; delete height; } int main() { CRectangle rect(3, 4), rectb(5, 6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }
This code defines a class CRectangle for rectangles using dynamic memory allocation for width and height. Here’s a breakdown of the code:
- Class Definition (CRectangle):
The class CRectangle represents rectangles, with private members width and height stored as dynamic integer pointers. - Constructor (CRectangle::CRectangle):
The constructor allocates memory for width and height using new and initializes them with the provided values. - Destructor (CRectangle::~CRectangle):
The destructor deallocates the memory for width and height using delete. - Member Function (area):
The area function calculates and returns the area of the rectangle using the values stored in width and height. - Main Function:
Creates instances of CRectangle (rect and rectb) with dynamic memory allocation for width and height.
Calls the area function to calculate and display the area of each rectangle.
The destructors are automatically called when the instances (rect and rectb) go out of scope, freeing the dynamically allocated memory.