How to check allocated memory size

V

Victor Bazarov

NewToCPP said:
Is there any way I can check the size of the Memory allocated by "new"?

Generally speaking, you asked it to allocate a particular object, right?
That object has a size. You can query it by saying 'sizeof(*ptr)', where
'ptr' is the pointer you got from the 'new'. If you used 'new[]', then
you need to multiply it by the value of the expression in the brackets.

Of course, due to some variations in memory management implementations,
"true" allocation will be different from the "theoretical" one. In order
to learn the "true" allocation, you need to use either the implementation-
specific or the platform-specific means, whichever is available. There is
no way in the Standard C++ to find out how much "true" memory was used to
accommodate the objects created by 'new' or 'new[]'.

V
 
B

Bob Hairgrove

Is there any way I can check the size of the Memory allocated by "new"?

What Victor said...Also, be aware that "new" (and "delete") can be
overloaded by a class to do something entirely different from the
default implementations.
 
N

NewToCPP

Victor & Gavin,

Thanks for the replies.

Class ABC
{
public:
int* p;
ABC {p = new int[10]; }
~ABC { delete [] p; }
};

ABC* a;

a = new ABC;

In this example constructor of ABC is creating more memory. When I did
the new on ABC is there any way to find out how much memory we used. I
mean I want to know if I can find it using any methods or something
like that.
 
M

Mike Wahler

NewToCPP said:
Victor & Gavin,

Thanks for the replies.

Class ABC
{
public:
int* p;
ABC {p = new int[10]; }
~ABC { delete [] p; }
};

ABC* a;

a = new ABC;

In this example constructor of ABC is creating more memory. When I did
the new on ABC is there any way to find out how much memory we used. I
mean I want to know if I can find it using any methods or something
like that.

std::cout << sizeof *a + 10 * sizeof *a->p << '\n';

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top