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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:39:49+00:00 2026-05-23T20:39:49+00:00

I wrote a small queue class in C++, on Windows. Just to test, I’m

  • 0

I wrote a small queue class in C++, on Windows.

Just to test, I’m allocating 1,0000,000 ints and reading them back.

The queue is doing its job correctly and is very fast, but the problem is that after complete deallocation, there is some memory usage reported by the task manager. For example, I still get 2MB of memory although all those integers lead to 400MB+ of memory.

I thought it was due to the operating system not performing some sort of GC or whatever it is, but now I found that this value (2MB) becomes 13MB when I allocate 100,000,000 integers (10x the previous number). I cannot find the leak.

Here is the code; as you can see, I deallocate everything:

#pragma once

#include <exception>

template <class T>
class colist_item
{

public:

    colist_item() { }
    ~colist_item() { }

    T value;
    colist_item *next;

};

template <class T>
class colist
{

public:

    colist() { this->m_root = NULL; this->m_count = 0; }
    ~colist() { }

    void enqueue(T value);
    T dequeue();
    T *peek();
    int count();

private:
    colist_item<T> *m_root;
    int m_count;

};

template <class T>
void colist<T>::enqueue(T value)
{
    if (this->m_root == NULL) {
        this->m_root = new colist_item<T>();
        this->m_root->value = value;
        this->m_root->next = NULL;
    } else {
        colist_item<T> *tempitem = new colist_item<T>();
        tempitem->value = value;
        tempitem->next = this->m_root;

        this->m_root = tempitem;
    }

    this->m_count++;
}

template <class T>
T colist<T>::dequeue()
{
    if (this->m_root == NULL) {
        throw std::exception();
    } else {
        T retval = this->m_root->value;
        colist_item<T> *next = this->m_root->next;
        delete this->m_root;
        this->m_root = next;

        this->m_count--;
        return retval;

    }
}

template <class T>
T *colist<T>::peek()
{
    if (this->m_root == NULL) {
        return NULL;
    } else {
        T retval = this->m_root->value;
        return &retval;
    }
}

template <class T>
int colist<T>::count()
{
    return this->m_count;
}

Main:

#include <iostream>
#include <limits>
#include "colist.h"
#include <time.h>

const int kNumItems = 100000000;

using namespace std;

void puttest(colist<int> *list)
{
    for(int i = 0; i < kNumItems; i++)
        list->enqueue(i);
}

void readtest(colist<int> *list)
{
    for(int i = 0; i < kNumItems; i++)
        list->dequeue();
}

int main(int argc, char *argv[])
{
    colist<int> list;

    cout << "Testing with : " << kNumItems << " elements" << endl;

    clock_t start = clock();
    puttest(&list);
    clock_t end = clock();

    double ms = ((end - start));
    cout << "puttest: " << ms << endl;

    start = clock();
    readtest(&list);
    end = clock();

    ms = ((end - start));
    cout << "readtest: " << ms << endl;

    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
  • 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-23T20:39:50+00:00Added an answer on May 23, 2026 at 8:39 pm

    OK a couple of bugs.

    No destructor.

    You are not leaking any memory because you remove all the items. But if you did not remove them all manually then your class would leak when it goes out of scope. Add a destructor that just deletes all the items on the list.

    Dequeue is not exception safe:

        T retval = this->m_root->value;
        colist_item<T> *next = this->m_root->next;
        delete this->m_root;   // If T is badly designed it could potentially throw an exception.
        this->m_root = next;   // Now you have m_root pointing at a bad object. The idea is to
                               // make sure an exception can not damage your internal structure
                               // of your object. Thus remove it from the chain before you call
                               // delete.
    
       // Like this
       T               retval    = m_root.value;
       colist_item<T>* oldhead   = m_root;
       m_root                    = oldhead->next;
    
       // Internal structure updated.
       // Safe to do dangerous tasks.
       delete oldhead;
    

    Use the initializer list when constructing

    colist() { this->m_root = NULL; this->m_count = 0; }
    
    // should be
    
    colist():  m_root(NULL), m_count(0) {}
    

    You don’t need to use this everywhere.

    I know it is encouraged in java but in C++ it is usually discouraged but recognized as a style thing. Personally I find its use cluttering.

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

Sidebar

Related Questions

We wrote a small Windows class library that implements extension methods for some standard
I recently wrote a small tool to generate a class for each tier I
I wrote a small sparse matrix class with the member: std::map<int,std::map<int,double> > sm; The
I just wrote a small module in my Rail 3.0.0 application lib folder: module
I wrote a small snippet in jQuery to just put a search word into
I wrote a small class that creates a report object containing 3 arrays. At
I wrote a small C++ program in VS2k8. When I launch it from windows
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I wrote a small PHP application that I'd like to distribute. I'm looking for
I wrote a small sample of code in C# to capture selected text from

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.