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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:06:21+00:00 2026-05-30T08:06:21+00:00

Why does it crash when I get to the copy constructor function? In the

  • 0

Why does it crash when I get to the copy constructor function?

In the copy procedure that you will find in my class definitions, I’m making sure that the Queue that will be created as a copy of some other original Queue is empty before copying begins: let’s say Queue q1 is not empty and I want to turn q1 into q2. I want to empty the contents of q1 before copying the contents of q2 to q1..

#include <iostream>
#include <string>
#include <cassert>

using namespace std;

class Dnode
{
    public:
       Dnode(int);
       int  n;
       Dnode*   l, *r;
};

Dnode::Dnode(int tx)
{
    n = tx;
    l = r = NULL;
}

class Queue // reminder: insertions at the rear, deletions at the front
{
    public:
       Queue();
       void enqueue(int x);
       int dequeue(void);
       bool empty(void) const;
       void display(void) const;
       Queue(const Queue&); //copy constructor

    private:
       Dnode*   front, *back;
       void copy(Dnode*);
       void free();

};

Queue::Queue()
{
    front = back = NULL;
}


void Queue::enqueue(int x)
{
    Dnode*  d = new Dnode(x);
    if (empty())
        front = back = d;
    else
    {
        back->r = d;
        d->l = back;
        back = d;
    }
}

int Queue::dequeue(void)
{
    assert(! empty());
    Dnode*  temp = front;
    front = front->r;
    if (front == NULL)
        back = NULL;
    else    front->l = NULL;
    int x = temp->n;
    delete temp;
    return x;
}


bool Queue::empty(void) const
{
    return front == NULL;
}

void Queue::display(void) const
{
    for (Dnode* d = front; d != NULL; d = d->r)
        cout << d->n << " ";
    cout << endl;
}

void Queue::copy(Dnode* dn) // "dn" will be "Front" of Queue being copied
{                           // this procedure will be called in Copy Constructor  
    Dnode* temp=front;      // found underneath this procedure
    while(front!=back)      
    {                       
        front=front->r;
        delete temp;
        temp=front;
    }

    delete temp;

    front=back=temp=NULL;

    if(dn!=NULL)
    {
        while(dn->r!=NULL)
        {
            enqueue(dn->n);
            dn=dn->r;
        }
        enqueue(dn->n);
    }
}

Queue::Queue(const Queue& x)
{
    copy(x.front);
}

int main()
{
    Queue   q;
    if (q.empty()) cout << "q empty" << endl;

    for (int i = 0; i < 10; i++) q.enqueue(i);

    q.display();

    int x = q.dequeue();

    cout << "x is " << x << endl;

    q.display();

    Queue q1(q); //<----program crashes when we get here

    q1.display();
}
  • 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-30T08:06:23+00:00Added an answer on May 30, 2026 at 8:06 am

    You’re confusing a copy constructor with an assignment operator. In an assignment operator, you have to delete the current queue and replace it with a copy of the second parameter. But this is a copy constructor. There is no current queue. Your members are uninitialized. So when you begin by trying to delete the existing queue, you’re messing with uninitialized pointers.

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

Sidebar

Related Questions

Does anyone knows how to get crash logs from customers? Instead of manually asking
My understanding is that with crash dump of debug build you can get the
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>
One of the crash reporter frameworks I've found does it like this: If there
For some reason the following doesn't crash like my program does, but I'm pretty
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most
IBCocoaSimulator crashes when IB cannot find the framework, I assume that happens to you
Related to my other question How can I get the address (acctual function pointer)

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.