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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:47:40+00:00 2026-06-03T04:47:40+00:00

I am trying to create a copy constructor for my Binary tree. My problem:

  • 0

I am trying to create a copy constructor for my Binary tree.

My problem:

I can see the values of the source tree getting copied into the target tree but when it comes to writing the values out, there are no values in the copied tree and it crash’s my program.

Error Message:

Unhandled exception at 0x0097a43c in binTree.exe: 0xC0000005: Access violation reading location 0xccccccec.

Code:

// Main Method

    int main(int argc, char **) {
    ifstream fin6("input_data.txt");
    ofstream out9("copied_tree.txt");

    if(!fin6.is_open()) 
    {
        cout << "FAIL" << endl;
        return 1;
    }

    BinaryTreeStorage binaryTreeStorage2;

    // read in values into data structure
    binaryTreeStorage2.read(fin6);

    BinaryTreeStorage binaryTreeStorage3 = binaryTreeStorage2;

    // output values in data structure to a file
    binaryTreeStorage3.write(out9);

    fin6.close();
    out9.close();

    // pause
    cout << endl << "Finished" << endl;
    int keypress; cin >> keypress;
    return 0;
}

// Copy Constructor

BinaryTreeStorage::BinaryTreeStorage(BinaryTreeStorage &source)
{
    if(source.root == NULL)
        root = NULL;
    else
        copyTree(this->root, source.root);
}

// Copy Tree Method

void BinaryTreeStorage::copyTree(node *thisRoot, node *sourceRoot)
{
    if(sourceRoot == NULL)
    {
        thisRoot = NULL;
    }
    else
    {
        thisRoot = new node;
        thisRoot->nodeValue = sourceRoot->nodeValue;
        copyTree(thisRoot->left, sourceRoot->left);
        copyTree(thisRoot->right, sourceRoot->right);
    }
}
  • 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-06-03T04:47:42+00:00Added an answer on June 3, 2026 at 4:47 am

    If you change the value of a pointer (not the pointee) in a function, you have to pass a reference to that pointer:

    void BinaryTreeStorage::copyTree(node *& thisRoot, node *& sourceRoot)
    

    If you pass a pointer into a function, this pointer is passed by value. If you change the value of the pointer (the address it stores), this change is not visible outside the function (this is what happens when you call new). So, to make the change visible outside the function, you have to pass a reference to the pointer you want to modify.

    This question explains it in detail.

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

Sidebar

Related Questions

I am trying to create a deep copy of my binary tree data structure
I'm trying to create a generic container which has a copy-constructor. I'm having trouble
I'm trying to create a directory and copy a file (pdf) inside a Parallel.ForEach
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Trying to create a macro which can be used for print debug messages when
Trying to create a background-image slideshow and am getting this error... This is the
I am trying to create a copy-paste javascript (it will work when user paste
Ok so I am trying create a login script, here I am using PHP5
Trying to create a black line in my view to separate text blocks but

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.