amount of memory allocated to a pointer

P

pratap

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

Mark Bluemel

pratap said:
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[100];

This doesn't look like C to me. Did you mean to post to comp.lang.c++?
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?

Not in C and I don't believe C++ is any different.
If there are no standard methods or routines why would this be so ?

I had a former colleague who used to say that he would answer "what" and
"how" questions, but not "why" questions...

Basically, the language simply doesn't work that way.
 
R

Richard Heathfield

Mark Bluemel said:
pratap wrote:


Not in C and I don't believe C++ is any different.

Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>
 
P

pratap

Mark Bluemel said:



Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

one more way is using _msize(p)
this is even more easier than remembering! !-)
 
A

André Gillibert

pratap wrote about getting the size of an allocated memory block:
one more way is using _msize(p)
this is even more easier than remembering! !-)

1) This is neither standard nor portable.
2) On implementations where this is available, it works with malloc'ed
blocks ONLY.
3) Even on these implementations, it DOESN'T work. It tends to give sizes
larger than the allocated size; Giving the effective size of allocation
(counting alignments), and not the requested size.
4) It transforms C in BASIC.
With typical C functions accepting a pointer memory block as parameter as
well as some size information, you can pass memory you've allocated from
anywhere, you can write your own memory pool or sub-heap of malloc's heap,
you can manually cut your memory blocks in pieces. Programmers are very
used to have the freedom of memory use.
Using _msize is removing this freedom.

Lazyiness has many disadvantages.
 
J

Joachim Schmitz

pratap said:
one more way is using _msize(p)
this is even more easier than remembering! !-)
If your implementation happens to have that. It's non-standard.

Bye, Jojo
 
A

Army1987

[snip]

one more way is using _msize(p)
this is even more easier than remembering! !-)

It is not standard C, and AFAIK not even standard C++. Probably it
is a compiler extension, but it won't work on others compilers. In
standard C the only way to know how big is the array containing the
object to which a pointer points is remembering it in the first
place.

BTW, you should snip parts of the post you're replying to which
are not relevant to your reply, and this almost always includes
signatures (the part which begins with "-- \n").
 
J

jacob navia

pratap said:
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[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 ?

If you use counted structures the problem disappears. Instead of
char ^pointers you use counted string and counted vectors.

typedef struct tagVector {
size_t len;
size_t elementSize;
void *data;
} VECTOR;

Then at any moment you know the size of your object. Some "malloc"
implementations use this structure for their blocks, returning just a
pointer to the data. Then, they can know at any moment the size of the
block. You can do the same.

This is easier than remembering the length associated with each block in
a notebook at your side...
 
C

CBFalconer

pratap said:
.... snip ...

one more way is using _msize(p)
this is even more easier than remembering! !-)

A minor problem being that _msize() doesn't exist.
 
C

Chris Hills

CBFalconer said:
A minor problem being that _msize() doesn't exist.

It quite plainly does exist because he is using it.

What you might mean is that _msize() is not part of the standard C
library. Which is far more helpful and may tell the OP something he did
not know.
 
M

Martin Ambuhl

pratap said:
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[100];

Consider that those lines are syntax errors in C. Perhaps you meant to
post to <where obscurantism is spoken.
I suspect the answer is the same in C++ as in C: if you want to know how
much space you have allocated, remember it. This is not one of those
cases where laziness pays.
 
M

Malcolm McLean

Martin Ambuhl said:
pratap said:
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[100];

Consider that those lines are syntax errors in C. Perhaps you meant to
post to <where obscurantism is spoken.
I suspect the answer is the same in C++ as in C: if you want to know how
much space you have allocated, remember it. This is not one of those
cases where laziness pays.
Not really. In C++ approved style is to use container classes for all your
arrays and other structures. These have length / size methods. All the
memory management is done for you; it is seldom necessary to call new,
unless implementing your own custom container, and malloc() is there only
for compatibility with C, it shouldn't be used in straight C++.

(comp.lang.c++ added).
 
C

CBFalconer

Erik said:
At least sizeof(int)
or
int *p=new int[100];

At least sizeof(int) * 100

The standard only guarantees that enough memory to contain the
type is allocated, it does not place any restrictions on
allocating more. That is up the the allocator.

This is about C++. This newsgroup is c.l.c, and the discussion is
off-topic here. This is why crossposts between these newsgroups
should never be created. F'ups set.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top