By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

How to check allocated memory size

P: n/a
Is there any way I can check the size of the Memory allocated by "new"?

Dec 8 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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


new allocates as much memory as you ask it to. If your question isn't
answered here

http://www.parashift.com/c++-faq-lit...tore-mgmt.html

post some code including an allocation with new that shows what you are
asking.

Gavin Deane

Dec 8 '05 #2

P: n/a
NewToCPP wrote:
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
Dec 8 '05 #3

P: n/a
On 8 Dec 2005 09:32:25 -0800, "NewToCPP" <he****@yahoo.com> wrote:
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.

--
Bob Hairgrove
No**********@Home.com
Dec 8 '05 #4

P: n/a
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.

Dec 8 '05 #5

P: n/a

"NewToCPP" <he****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Dec 8 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.