Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:48:46+00:00 2026-05-28T13:48:46+00:00

From what I know smart pointer should be equilvalent to raw pointer with the

  • 0

From what I know smart pointer should be equilvalent to “raw” pointer with the difference that it is safe. Ok, but if I have regular pointer:

int* p = new int[10];
fill(p, p + 10, 0);//this will work for regular pointer but not for smart one.  

Same with hand written loop:

for(int i = 0; i < 10; ++i)
{
*p[i] = 0;
}

This is not possible (I think) for smart poiner. So the question is, how can I initialized array to which pointer I have stored in one of smart pointers (let’s assume shared_ptr)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:48:47+00:00Added an answer on May 28, 2026 at 1:48 pm

    First off, it might be easier just to use std::vector<int>. If your array has an unchanging size, though, then perhaps std::vector<int> is indeed better replaced with a smart pointer.

    With that out of the way, your first choice should be a std::unique_ptr, specifically the array specialization: std::unique_ptr<int[]>. (If you don’t, the smart pointer will use delete instead of delete[] on your pointer, leading to undefined behavior.) Your code would become:

    std::unique_ptr<int[]> p(new int[10]);
    std::fill(p.get(), p.get() + 10, 0);
    

    As you can see, smart pointers have a get() method that returns the underlying pointer.

    From here, if you need to use a std::shared_ptr, things become a big dangerous (do to unfortunate oversight, as far as I know). That oversight is that std::shared_ptr has no array specialization:

    {
        std::shared_ptr<int> x(new int[10]);
    } // oops! calls delete x.get(); instead of delete [] x.get(); ... UB!
    

    However, std::shared_ptr can easily correct this like so:

    {
        std::shared_ptr<int> x(new int[10], std::default_delete<int[]>());
    } // correctly uses delete [] x.get()
    

    From this point, the code is the same:

    std::shared_ptr<int> p(new int[10], std::default_delete<int[]>());
    std::fill(p.get(), p.get() + 10, 0);
    

    Note that std::shared_ptr provides a constructor to construct from a std::unique_ptr, which properly uses the deleter. So this is safe:

    std::unique_ptr<int[]> p(new int[10]);
    std::shared_ptr<int> p2(std::move(p)); // okay, uses std::default_delete<int[]>()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know about Virtualization in detail. But start from basics, like what
I know from reading Microsoft documentation that the "primary" use of the IDisposable interface
I know from this question that Firefox 3.0 and up stores its cookies in
I know from wikipedia for example that exception handling is used in an application
I know from here that I can pass an ignore option in the jQuery.validate
Let's say I have N pictures of an object, taken from N know positions.
This may well have come up before but the following code is taken from
From several posts inside stackoverflow and outside, I have come to know how to
I know this question must have been covered endless of times, but I've searched
if you know the start and end positions in string from where to begin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.