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 4059272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:11:13+00:00 2026-05-20T15:11:13+00:00

This is an interview question I saw from here: http://www.careercup.com/question?id=1707701 Want to know more

  • 0

This is an interview question I saw from here:
http://www.careercup.com/question?id=1707701

Want to know more about this .thanks

  • 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-20T15:11:13+00:00Added an answer on May 20, 2026 at 3:11 pm

    Shallow copy causes a problem (primarily) when you have “remote ownership”. The most common form is a pointer to data that’s owned by the object so when the object is destroyed the data it owns gets destroyed as well. One place many people run into this is the inevitable string class:

    // warning: bad code. Do *not* use.
    class mystring { 
        char *data;
    public:
        mystring() : data(NULL) {}
        mystring(char *init) {
            data = new char[strlen(init)+1];
            strcpy(data, init);
        }
    
        ~mystring() { delete [] data; }
    };
    
    int main() { 
         mystring s("This is a string");
    
         mystring t;
    
         t = s;
    }
    

    This will compile just fine. Under these exact circumstances, it might even seem to run fine as well. That doesn’t mean it’s really right though. When we assign s to t, the pointer gets copied, but what it points at does not get copied. We have two objects both containing pointers to the same buffer. When one of them gets destroyed, it deletes the associated buffer for the data. Then we have a dangling pointer — it’s still trying to refer to the buffer, but the buffer no longer exists. When the second object gets destroyed, it’ll try to free the same memory again — but since it’s already been freed, that leads to undefined behavior (I.e., anything can happen, and something bad usually will).

    There are two common alternatives to shallow copying. One is a “deep” copy, in which we overload the copy constructor and assignment operator for the class, and in them when we copy/assign the object, we allocate a new buffer and copy the contents of the old buffer to the new one.

    The second is reference counting. Instead of a “raw” pointer to the buffer, we use a “smart” pointer to the buffer. The smart pointer keeps track of how many objects refer to the buffer and only frees the buffer itself when nothing refers to it any more.

    Neither is entirely perfect: deep copying can be slow and use a lot of memory, especially if there’s a lot of data involved. Reference counting can be slow in multi-threaded environments — since the reference count can be accessed from multiple threads, you have to protect it to assure only one thread modifies it a time (which is usually at least an order of magnitude slower than a normal increment/decrement).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I got this in an interview question -- the question was more about what
Hi i was asked in an interview about this question. I did google a
Here is an interview question that I saw on some forum. I've been trying
This is an interview question I saw somewhere and came up with this: import
I was asked this interview question so thought I would post it here to
This is an interview question I saw on some site. It was mentioned that
I saw this in an interview question , Given a sorting order string, you
This is an interview question : How to serialize a graph ? I saw
I saw this question in a programming interview blog. If pairwise sums of n

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.