Hi friends,

I know this is such a freak code, but out of curiosity I did it to see memory usage increase using new operator.

int main()
{
    char* a;

    for (;;)
    {
        //cout<<&a<<endl;
        a = new char[10];
    }
}

As you see line7 is commented. When i run this program, obviously a linear increase in memory usage. I could verify it from windows task manager.
But if I uncomment the line7, I could see in the console that same address is getting printed, which means that I don't see any increase in memory. Why is that?

You're printing the address of the pointer, not the value of the pointer. cout will interpret cout<< a as a C-style string though, so you need to cast it to void* to get the correct value:

cout<< (void*)a <<endl;

I'm sorry, I didn't get what void* does.

anyway memory is still not increasing when the cout is there...

Did you try Narue's suggestion?

Yes, I tried it. Now i get incremented addresses in each loop. but i dont see memory increasing.

I'm sorry, I didn't get what void* does.

It makes a a generic pointer. If you don't do something like this, cout will try to print a string of characters rather than an address.

anyway memory is still not increasing when the cout is there...

Then you did it wrong. Which is mildly humorous and somewhat sad seeing as how all you needed to do was copy and paste my line into your loop. :icon_rolleyes:

@narue: I changed the code like this. Hope this is what you meant :(

int main()
{
    char* a;
    /*   for (;;) {
       a = getName();
       //cout<<a;
       }
    */
    for (;;)
    {
        cout<<(void*)a<<endl;
        a = new char[10];
    }
}

Well my concern is not about the address. I just wanted to know why memory usage is not getting increased. I think this cout does a delete internally, or something like that???

That code (as you post it) should, at some point, trigger an exception (bad_alloc). That exception effectively means that you've run out of memory. At that point you will not see the memory use (as your program is no longer in existence if you do not catch the exception). How does the following work for you?

#include <iostream>

int main () {
    char * a = 0;
    try {
        for (;;) 
            a = new char[1024];
    } catch (std::bad_alloc ba) {
        std::cerr << "Caught bad alloc. Waiting for input..." << std::endl;
        std::cin.get ();
    }
    return 0;
}

If I do that, I end up with a program that has consumed 2.4G of memory and is waiting for my input.

@L7Sqr :
I tried your suggestion

int main()
{
    char* a;

    try
    {
        for (;;)
        {
            //cout<<(void*)a<<endl;
            a = new char[1024];
        }
    }
    catch (std::bad_alloc ba)
    {
        std::cerr << "Caught bad alloc. Waiting for input..." << std::endl;
        std::cin.get ();
    }
}

The exception is getting caught. But if I uncomment the line 9, it doesn't throw any exception.

Try using bigger table. For every 1 kb you use, you are outputting line to stdout and then you are flushing it (endl). If you try to do this for about 10^6 times it will take some time, allocating memory is a LOT faster then outputting text using cout<< <<endl.

Hi friends,

Thanks for all your support. I found it with the help of another colleague.
I can see memory increasing even if cout statement is there. But it was very slow increase, atleast some 20 times slower. I think this is exactly what is mentioned in Zjarek's post.

--
Regards,
Subith

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.