Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to check allocated memory size

Reply
Thread Tools

How to check allocated memory size

 
 
NewToCPP
Guest
Posts: n/a
 
      12-08-2005
Is there any way I can check the size of the Memory allocated by "new"?

 
Reply With Quote
 
 
 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-08-2005

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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-08-2005
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
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-08-2005
On 8 Dec 2005 09:32:25 -0800, "NewToCPP" <> 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

 
Reply With Quote
 
NewToCPP
Guest
Posts: n/a
 
      12-08-2005
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.

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-08-2005

"NewToCPP" <> wrote in message
news: 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to check whether malloc has allocated memory properly in case ifmalloc(0) can return valid pointer Shivanand Kadwadkar C Programming 83 01-08-2011 08:18 AM
Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer Gene C Programming 0 12-20-2010 05:33 AM
How to check memory size allocated to JVM? Laura Heinzmann Java 1 02-16-2005 04:37 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM



Advertisments