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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:12:06+00:00 2026-05-16T16:12:06+00:00

As per req i have to create stl queue on heap i have created

  • 0

As per req i have to create stl queue on heap
i have created stl queue on heap in constructor of my class as per below code

queue<int> *myqueue;
myqueue=new queue<int>();

Now in destructor i want to destroy it:
so i have written code

while(!myqueue->empty())
{
    myqueue->pop();
}

please tell me is it right way to destroy it or there is any other mean to avoid memory leak .Please note that
delete myqueue is giving segmentation fault.

  • 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-16T16:12:07+00:00Added an answer on May 16, 2026 at 4:12 pm

    All STL containers have a member clear() that can be used to erase their content. All STL containers will call this member in their destructor, so you won’t have to do it manually. So all you have to do is to ensure that the queue itself is destroyed. for dynamically (with new) allocated objects, that’s done by invoking delete: delete myqueue;.

    That said: I find it dubious that you would want to create a queue dynamically, instead of making it an ordinary class member:

    class myclass {
    public: 
      // whatever
    private: 
      std::queue<int> myqueue_;
    };
    

    (Note that std::queue will always allocate memory for its content dynamically, even if the queue itself is not dynamically allocated.)

    If you have to absolutely create the queue dynamically, make sure you’re following the Rule of Three. The crash you describe is what might happen when you copy around classes with dynamically allocated objects that don’t have a copy constructor/assignment operator.

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

Sidebar

Related Questions

Per a client's request I have written a communication class that inherits from webclient.
Per the code below, I am getting the following message. I am fairly certain
if i want to implement am http server. i create new actor per request.
I have the following code which builds a query on the Req Trans table
Per a great answer from another question I have begun mounting global resources (css/js/images)
As per question says. Specifically i want the html.beginform() to have the runat=server attribute.
As per http://www.w3schools.com/cssref/sel_last-child.asp When i am trying to apply :last-child for my code, the
As per Scott Meyers, to prevent repetition of code in the const version of
Per documentation, Doctrine_Record after saving should set id of newly created record as object
per my client request I have been requested to return a dataset. basically it's

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.