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

  • SEARCH
  • Home
  • 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 7534117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:55:27+00:00 2026-05-30T05:55:27+00:00

Consider this code: using namespace std; int* get() { unique_ptr<int> p (new int[4]); return

  • 0

Consider this code:

using namespace std;

int* get()
{
    unique_ptr<int> p (new int[4]);
    return p.get();
}
int main(int argc, char **argv)
{
    int *arr1=get();
    int* arr2=get();
    for(int i=0;i<4;i++)
    {
        arr1[i]=i;
        arr2[i]=i*2;
    }
    for(int i=0;i<4;i++)
        cout << arr1[i];
    return 0;
}

arr1 and arr2 point to the same area of memory.
So they share the same values.
I don’t understand why, when I call arr2=get() :

unique_ptr<int> p (new int[4]);

This object shouldn’t be created again? It isn’t deleted because still reachable by arr1.
How to get two arrays of different memory areas?

  • 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-30T05:55:29+00:00Added an answer on May 30, 2026 at 5:55 am

    I am fairly sure you are playing with undefined behavior which is bad.

    the data being pointed to was destroyed when the unique pointer was destroyed, the fact the values are the same, and the same slot was chosen is luck.

    for pointers to array type use a vector

    std::vector<int> get()
    {
        return std::vector<int>(4);
    }
    
    int main()
    { 
        std::vector<int> arr1=get();
        std::vector<int> arr2=get();
        return 0;
    }
    

    for normal single value pointers then you can return a unique_ptr;

    std::unique_ptr<int> get(){
        return std::unique_ptr<int>(new int(0));
    }
    :::
    std::unique_ptr<int> ptr=get();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; int main() { bool lock =
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider this code: using Microsoft.Office.Interop.Word; ApplicationClass _application = new ApplicationClass(); Can I get the
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider this: #include <iostream> using namespace std; class A{ protected: void some_function(int params) {
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;

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.