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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:18:05+00:00 2026-05-26T04:18:05+00:00

Hi everyone: Here i have created a queue from two stacks: You add to

  • 0

Hi everyone: Here i have created a queue from two stacks: You add to the one and remove from the other – when you want to remove the first stack dumps all its data into the second one, and it works perfectly – BUT
whenever i try to execute this loop without the bottom for loop or cin
the program receives a segmentation fault, i mean the most bottom for loop doesn’t even execute but take it out and see what happens. Could this be some sort of buffer overflow
and Gcc needs time to manage the memory?

=====================================================================

    struct Node
    {
        int DataMember;
        Node* Next;
    };

    class Que
    {
        public:
            Que();
            ~Que();
            void Add(int);
            void Pop();
            int getSize();
            void Purge();
        private:
            Node* Head;
            bool StackOrQue; //True = Que False = Stack
            int Size;
            int Remove();
            void Reverse();
    };

    void Que::Purge()
    {
        while(Head != NULL)
            Pop();

        if(StackOrQue)
            StackOrQue = false;
    }

    int Que::getSize()
    {
        return Size;
    }

    Que::Que()
    {
        Head = NULL;
        Size = 0;
        StackOrQue = false;
    }

    Que::~Que()
    {
        Head = NULL;
    }

    void Que::Add(int q)
    {
        if(StackOrQue)
            Reverse();
        Size += 1;
        Node* Temp = new Node;

        Temp->DataMember = q;

        Temp->Next = Head;
        Head = Temp;
    }

    int Que::Remove()
    {
        int i = Head->DataMember;
        Node* Temp = Head->Next;
        delete Head;
        Size -= 1;
        Head = Temp;
        return i;
    }

    void Que::Pop()
    {
        if(!StackOrQue)
            Reverse();
        cout << Remove();
    }

    void Que::Reverse()
    {
        Que TempStack;
        int k = Size;
        for(int i = 0; i < k; i++)
            TempStack.Add(this->Remove());
        delete this;
        *this = TempStack;

        if(!StackOrQue)
            StackOrQue = true;
        else
            StackOrQue = false;
    }

=====================================================================

Que q;
char a = NULL;

while(a != 'x')
{
    q.Purge();
    q.Add(1);
    q.Add(2);
    q.Add(3);
    q.Add(4);
    q.Add(5);
    q.Add(6);
    q.Add(7);
    q.Add(8);
    int size = q.getSize();
    for(int i = 0; i < size; i++)
        q.Pop();
    //cin >> a;
    for(int i = 0; i < 0; i++)
        ;
}

Thanks in-advance

  • 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-26T04:18:05+00:00Added an answer on May 26, 2026 at 4:18 am
        delete this;
        *this = TempStack;
    

    There are some extreme corner cases in which delete this; actually does the right thing. This is not one of them. Specially since your Queue is placed in the stack, and you further try to delete it. If you intend to call the destructor instead do this->~Queue(), however after a manual destruction the only sensible thing to do next is a placement new. Assigning to *this is almost always a bad idea (if you bring inheritance into the picture, you have just caused a slice object to be created and more problems ahead the road). Also, your class should be implementing a copy constructor and an assignment operator, to correctly handle the resources allocated.

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

Sidebar

Related Questions

The below code is from my other questions that I have asked here on
Hey everyone, here is the site in question: http://www.myvintagesecret.com/ I have a bunch of
hey everyone, here is the site SEE BELOW I have a slight jquery problem
First question here so hello everyone. The requirement I'm working on is a small
I have created a simple HttpModule which removes whitespaces from response before sending it
I have this scenario: One client sends a message into a msmq queue instance
hii every one i have created a data entry screen like this in which
Aloha everyone, I have created a small bash shell script which I wanted to
I have created a simple custom installer as described here . My new installer
I have created a composite control with sample details as follows. Basically, the first

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.