Prepare
Practice
Interview
Aptitude
Reasoning
English
GD
Placement papers
HR
Current affairs
Engineering
MCA
MBA
Online test
Login
Delete operator is used to release dynamically allocated memory in CPP
Home
>>
Category
>>
C++ (MCQ) questions and answers
>>
Memory Management
Q. Which of the following operator is used to release the dynamically allocated memory in CPP?
- Published on 17 Jul 15
a.
remove
b.
free
c.
delete
d.
both b and c
ANSWER: delete
Related Content
Constructor and Destructor (
10
)
Operator Overloading (
11
)
Inheritance (
16
)
Polymorphism and Abstract Classes (
12
)
C++ basic concepts (
39
)
Exception Handling (
22
)
File Handling (
11
)
Functions (
20
)
Memory Management (
6
)
Pointers (
3
)
Discussion
Ramesh
-Posted on 23 Oct 15
delete operator is used to release the dynamically allocated memory in CPP. The new operator allocates memory dynamically, it should be released otherwise resource will be captured unnecessary.
Example.
class MyClass {
private:
int n;
float *ptr;
public:
MyClass() {
cout << "Enter total number of students: ";
cin >> n;
ptr = new float[n];
cout << "Enter marks of students." << endl;
for (int i = 0; i
cout << "Student" << i+1 << ": ";
cin >> *(ptr + i);
}
}
~MyClass() {
delete[ ] ptr;
}
void Show() {
cout << "\nDisplaying Marks of students." << endl;
for (int i = 0; i
cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
}
}
};
int main() {
MyClass obj;
obj.Show();
return 0;
}
When the object goes out of scope then, destructor is automatically called.
➨
Post your comment / Share knowledge
Required!
Required!
Invalid Email Id!
Required!
Enter the code shown above:
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)
MCQs
English
Tutorials
Download
▲