BlackFriday 2024! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPA-21-02 Topic 1 Question 12 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 12
Topic #: 1
[All CPA-21-02 Questions]

Which line of code inserted instead of the comment will make the following code run properly without causing memory leaks?

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Huey
4 months ago
I think the answer is A because we need to delete 'this' as well to prHueynt memory leaks.
upvoted 0 times
...
Norah
5 months ago
But wouldn't deleting the pointer twice cause a double free error?
upvoted 0 times
...
Johnetta
5 months ago
I disagree, I believe the answer is C because we need to delete the pointer twice to avoid memory leaks.
upvoted 0 times
...
Norah
5 months ago
I think the answer is D because we only need to delete the pointer once to avoid memory leaks.
upvoted 0 times
...
Clorinda
6 months ago
I don't think deleting the pointer twice is necessary. It could lead to undefined behavior. So, I still think D) is the correct answer.
upvoted 0 times
...
Zachary
6 months ago
But what about option C) ~Base() { delete ptr; delete ptr; }? Wouldn't deleting the pointer twice be more thorough in preventing memory leaks?
upvoted 0 times
...
Lashawnda
6 months ago
I also agree with user_1. Deleting the pointer in the destructor is essential to prevent memory leaks.
upvoted 0 times
...
Clorinda
6 months ago
I think the correct answer is D) ~Base() { delete ptr; } because we need to delete the pointer to prevent memory leaks.
upvoted 0 times
...
Lashandra
7 months ago
Alright, let's break this down. The code is missing the implementation of the Base class destructor, and we need to ensure that the memory allocated for the ptr member is properly released. I think option D is the way to go. Simple and effective!
upvoted 0 times
...
Carlee
7 months ago
You know, I'm still a bit confused about the whole pointer thing. Is the ptr variable a member of the Base class? I need to review my C++ fundamentals before this exam.
upvoted 0 times
...
Altha
7 months ago
Haha, I bet the exam writers are trying to trick us with those answer choices. C is clearly wrong, since deleting the pointer twice would result in a double-free error. That's a big no-no!
upvoted 0 times
...
Daniel
7 months ago
I agree with Marvel, but I'm also curious about option B. Could it be that no additional code is needed if the Base class has a proper default destructor implementation? That would be the simplest solution.
upvoted 0 times
Myong
6 months ago
I believe option C would cause memory leaks.
upvoted 0 times
...
Hana
6 months ago
But option B also seems like a valid choice.
upvoted 0 times
...
Lisha
6 months ago
I think option D is correct.
upvoted 0 times
...
...
Marvel
7 months ago
Okay, let's think this through. The code is missing the destructor implementation for the Base class, which could lead to a memory leak. I'm leaning towards option D, since deleting the pointer once should do the trick.
upvoted 0 times
...
Gaynell
7 months ago
Whoa, this question is tricky! I'm not sure I even understand the code, let alone the right answer. Memory leaks are such a pain to deal with.
upvoted 0 times
...

Save Cancel