473,486 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Size of memory Pointed-to

how to find out how much memory is blocked(or has been allocated
to a pointer).

consider,
int *p=new int;
or
int *p=new int[100];

suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
If there are no standard methods or routines why would this be so ?

Oct 7 '07 #1
4 1679
Winsk said:
how to find out how much memory is blocked(or has been allocated
to a pointer).

consider,
int *p=new int;
or
int *p=new int[100];
Both are syntax errors. I suspect you're slightly lost. Were you looking
for comp.lang.c++? Still, stick around, because this is a common C
question too, albeit with...
suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)
.... p = malloc(n * sizeof *p) rather than p = new int[n], but it's the same
deal really.
(definitely sizeof(p) would not give me the amount of memory
allocated.)
Right.
i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
See that n? At the time you're allocating the memory, you do actually know
how much memory you are allocating. If you will need this information
later, Don't Forget It! Store it somewhere.
Are there any methods or standard routines to measure the amount of
memory?
Remembering, at the time you allocate.
If there are no standard methods or routines why would this be so ?
Because it's so easy to remember, at the time you allocate.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 7 '07 #2
"Winsk" <no****@nospam.comwrote in message
>
If there are no standard methods or routines why would this be so ?
There aren't, though often it is provided as an extension called msize() or
similar.

The real reason is backwards compatibility. It wasn't included in the first
version of C, and it is hard to change an allocator to support it if the
information wasn't stored. There is also a debate about whether the amount
requested or the amount in the block should be returned - the first is more
useful as it acts as a free array size, the second easier to implement.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 7 '07 #3
Winsk wrote:
how to find out how much memory is blocked(or has been allocated
to a pointer).

consider,
int *p=new int;
new is a C++ keyword.
or
int *p=new int[100];

suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
If there are no standard methods or routines why would this be so ?
The size of static objects can be found by using sizeof. As for dynamic
objects, in C at least, you need to specify their size and/or number
when you do the allocation. Usually this information should be stored
somewhere, usually in a size_t object, so that it can be retrieved
later.

Although the Standard library keeps track of dynamic allocations it
provides no way for the user code to access it's information, so you
have to this youself.

Oct 8 '07 #4
santosh <sa*********@gmail.comwrites:
[...]
Although the Standard library keeps track of dynamic allocations it
provides no way for the user code to access it's information, so you
have to this youself.
The library doesn't even necessarily keep track of sizes of dynamic
allocations. Given:

char *s = malloc(42);

the implementation doesn't necessarily store the value 42 *anywhere*.
For example, the allocation size might be rounded up internally to 48
or 64 bytes. The implementation only has to store enough information
to be able to deallocate the storage when you call free() or
realloc().

If you want to know the size of an allocated object, you just have to
remember it when you allocate it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3719
by: Khaled Omar | last post by:
I am running a web application with application protection level set to high, so it runs in a separate dllhost.exe process, the process takes around 14,000 K of memory I wanna know if this is fine...
6
2638
by: Dima | last post by:
How can I know size of avaible memory in heap? For example : .... .... // size = N cout << "Size of Heap = " << SizeOfHeap() << endl; int* i = new int; // size = N - sizeof(int) cout << "Size...
36
4280
by: xixi | last post by:
hi, we are using db2 udb v8.1 on windows, i try to use the configuration advisor to get recommendation on the parameter setting, at first, i give the target memory for DB2 is 80% of the physical...
19
26669
by: Jerry | last post by:
I am wondering what is the maximum size of memory that malloc() could handle. Is there any limitation on that? Where am I supposed to get this kind of information? Thank you everybody.
7
1386
by: alekatz | last post by:
Hi everybody, I have a listbox with movie names, each time a movie is selected, an image of the dvd cover is showed up in a picturebox. The problem I get is that while navigating through the...
1
8032
by: Larry Bud | last post by:
Wondering what I should be looking at when looking for a memory leak.. Using Process Explorer by SysInternals, and they have a Working Set and a Virtual Size memory column. Which one should I be...
11
397
by: rodrigo.gloria | last post by:
How am I supposed to release memory of a struct I have created: #include ..... struct MyData { int size; char *buffer ; }; MyData *allocate_mydata(char *str)
6
18255
by: Tarun | last post by:
Hi coders, How can check the total RAM size and the number of RAM modules and their respective sizes using C#? Regards, Tarun
7
5597
by: Raman | last post by:
Hi All, Could any one tell me, how can I determine/Change size of heap on per- process basis on Unix based systems. Thanks. Regards
9
5686
by: dspfun | last post by:
Hi! I have stumbled on the following C question and I am wondering wether the answer is implementation specific or if the answer is always valid, especially question b). BRs! ...
0
7099
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
7175
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...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
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,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.