LearnFrenzy
Sign in Take a free test

Smart Pointers · Predict the Output · Question 11

unique_ptr managing an array Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <memory>

int main() {
    auto marks = std::make_unique<int[]>(4);         // value-initialised: all 0
    marks[1] = 80;
    marks[3] = 95;
    int total = 0;
    for (int i = 0; i < 4; ++i) total += marks[i];
    std::cout << marks[0] << ' ' << total << '\n';
    return 0;
}
  • A 0 175 Correct
  • B garbage 175
  • C 80 175
  • D 0 95
Correct Answer: Option A

make_unique<int[]>(n) value-initialises the elements to 0, and unique_ptr<T[]> provides operator[] and calls delete[].

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links