Size of memory Pointed-to

W

Winsk

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

Malcolm McLean

Winsk said:
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.
 
R

Richard Heathfield

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

santosh

Winsk said:
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.
 
K

Keith Thompson

santosh said:
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.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top