473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to check allocated memory size

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

Dec 8 '05 #1
5 12818

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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Randell D. | last post by:
Folks, I've heard of buffer overflows being used/abused by hackers and believe one method to reduce this from happening is to check the length of my form data before writing it to my MySQL...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
12
by: Sabiyur | last post by:
Hi All, Is there any way to find out how much memory is allocated for a given pointer. For example: int *p=new int; I know, compiler keeps the information of how much bytes are allocated...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
13
by: pratap | last post by:
how could i find out how much memory is blocked(or has been allocated to a pointer) consider, int *p=new int; or int *p=new int; suppose i dont know the right hand side of the statement...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.