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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:34:48+00:00 2026-06-15T17:34:48+00:00

Have a look at the following code. In this code at the start of

  • 0

Have a look at the following code. In this code at the start of execution the memory usage is 1020K. but at the end of execution the memory usage is 1144K. can somebody help me identify the memory leak. If func() is called five times the memory usage goes to 1500K+. If we don’t use the list the memory usage does not increase.

void func();

int _tmain(int argc, _TCHAR* argv[])
{
    func();
    return 0;
}

void func()
{
    list<char*> list1;
    list<char*>::iterator iter;
    char* val;
    for(int i=0; i<100000; i++)
    {
        val = new char[20];
        for(int j=0; j<20;j++)
        {
            val[j] = 'A';
        }
        val[19] = '\0';
        list1.push_back(val);
    }

    iter = list1.begin();
    for(int k=0; k<100000;k++, iter++)
    {
        delete[] *iter;
        *iter = NULL;
    }
    val = NULL;

    list1.clear();
    list1.empty();
}
  • 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-15T17:34:48+00:00Added an answer on June 15, 2026 at 5:34 pm

    I see no memory leak here, but I do see a lot of very poor code.

    I see some hints in your code that you’re running under Windows, and when I gaze in to my crystal ball I think I see you checking Task Manager to detect memory leaks. Using Task Manager to detect memory leaks is like using broadsword to do surgery. TM might be able to give you a hint that there might be a large memory leak present in the system, but it is too far removed from your program and too coarse to be definitive. Instead of using TM to determine that there are or are not memory leaks, you need to use a tool dedicated to that job. Visual Studio has such a tool built-in.

    When I use these buil-in facilities in your code:

    #include <cstdlib>
    #include <list>
    using std::list;
    
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    
    
    void func();
    
    int main(int argc, char* argv[])
    {
        func();
        return 0;
    }
    
    void func()
    {
        list<char*> list1;
        list<char*>::iterator iter;
        char* val;
        for(int i=0; i<100000; i++)
        {
            val = new char[20];
            for(int j=0; j<20;j++)
            {
                val[j] = 'A';
            }
            val[19] = '\0';
            list1.push_back(val);
        }
    
        iter = list1.begin();
        for(int k=0; k<100000;k++, iter++)
        {
            delete[] *iter;
            *iter = NULL;
        }
        val = NULL;
    
        list1.clear();
        list1.empty();
    
        _CrtDumpMemoryLeaks();
    }
    

    …I see:

    Detected memory leaks!
    Dumping objects ->
    {142} normal block at 0x00000000000778C0, 24 bytes long.
     Data: < x       x      > C0 78 07 00 00 00 00 00 C0 78 07 00 00 00 00 00 
    {141} normal block at 0x0000000000077840, 16 bytes long.
     Data: <(       h       > 28 F7 1A 00 00 00 00 00 68 F7 1A 00 00 00 00 00 
    Object dump complete.
    

    There are two memory leaks reported for a total of 40 bytes, so there is no broad memory leak here. Moreover, these are probably false positives, reporting such things as static memory allocations by the CRT and should be ignored.

    However, as I said I do see a lot of very poor code. This isn’t a cause of memory leaks here, but they easily could be in real code.

    1. You use a lot of magic numbers. Such as with for(int i=0; i<100000; i++), where instead you should be iterating the list (for( list<char*>::iterator it = list1.begin(); it != list1.end(); ++it)), or at least asking the list how many elements it has (for( size_t i = 0; i < list1.size(); ++i ))

    2. You are using dynamic allocation/deallocation. You should instead be using RAII in cases where you really do need dynamic allocation, but avoiding dynamic allocation altogether whenever possible. In your case, instead of having a list<char*>, you should have a list<std::string> instead.

    3. You are using hand-written loops, in places where you could be using algorithms provided by the Standard Library. Code you never write is the most reliable code. Instead of the inner loop initializing a char array, just do std::string s(20,'A'), and instead of the outer loop setting each list member, use something like copy or transform.

    Edit Here’s a re-farctored version of your function that addresses some of these issues I mention above:

    #include <string>
    using std::string;
    #include <algorithm>
    using std::generate_n;
    #include <iterator>
    using std::back_inserter;
    
    void func()
    {
        static const size_t NumStrings = 10000;
        typedef list<string> strings;
    
        // Populate the list
        strings list2;
        generate_n(back_inserter(list2), NumStrings, []() -> string
        {
            static const size_t NumChars = 19;
            static const char InitChar = 'A';
            return string(NumChars, InitChar);
        });
    
        // Clear the list
        list2.clear();
    
        // Done
        _CrtDumpMemoryLeaks();
    }
    

    The call to generate_n uses a C++11 lambda, but could easily be refactored to use a functor or something else entirely.

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

Sidebar

Related Questions

Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Have a look at the following code you are not required to read the
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
Have a look here: In the following code, what would be the type of
am new here. i have a slight problem; PLease look at the following code
I have following HTML code. writer input field is text filed. but I want
i was dealing with the following code,& got confused,please have a look at it

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.