doubt regarding new operator and memory allocation

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.

1
2
3
4
5
6
7
8
9
10
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?
Well, you are printing &a, which is the memory address of the *pointer*, not what it is pointing at. Maybe you meant to print just a?
Because &a is the address of the pointer, not the address a points to.
thanks for the comments... i understood and I changed it to print (void*)a now.
Still with line7 uncommented memory doesn't increase. but when i comment it, i can see memory increasing... that's what i'm concerned about.
Hm, that's odd...I have no idea why that would be happening really...

Maybe your compiler realizes you aren't doing anything with a so it doesn't allocate any new memory? *shrug*
this guy "cout" internally does a delete or something like that??
thats the only reason i cud find...
Nah, that would be totally random. Your compiler also doesn't optimize the new out, otherwise you wouldn't have an increase in memory useage. Have you tried casting a to an int?
Topic archived. No new replies allowed.