Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > amount of memory allocated to a pointer

Reply
Thread Tools

amount of memory allocated to a pointer

 
 
pratap
Guest
Posts: n/a
 
      09-12-2007
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 ?

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      09-12-2007
pratap wrote:
> 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.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-12-2007
Mark Bluemel said:

> pratap wrote:


<snip>

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


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
 
Reply With Quote
 
pratap
Guest
Posts: n/a
 
      09-12-2007
On Sep 12, 2:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> Mark Bluemel said:
>
> > pratap wrote:

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

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

 
Reply With Quote
 
André Gillibert
Guest
Posts: n/a
 
      09-12-2007
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.

--
You can contact me at <(E-Mail Removed)>
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-12-2007
"pratap" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> On Sep 12, 2:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> Mark Bluemel said:
>>
>> > pratap wrote:

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

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

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


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-12-2007
On Wed, 12 Sep 2007 10:01:45 +0000, pratap wrote:
>> > pratap wrote:


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


[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").
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      09-12-2007
pratap wrote:
> 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...


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-12-2007
pratap wrote:
> Richard Heathfield <(E-Mail Removed)> wrote:
>

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

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


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

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Chris Hills
Guest
Posts: n/a
 
      09-12-2007
In article <(E-Mail Removed)>, CBFalconer
<(E-Mail Removed)> writes
>pratap wrote:
>> Richard Heathfield <(E-Mail Removed)> wrote:
>>

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

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

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



--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ http://www.velocityreviews.com/forums/(E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
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
[C/Linux] How to get amount of allocated memory from within a process (heap size) Gennady Bystritsky Ruby 4 07-27-2009 06:56 PM
Re: amount of memory allocated to a pointer Malcolm McLean C++ 2 09-13-2007 08:52 PM
memory allocated to a pointer Ankit Raizada C Programming 16 08-04-2005 03:03 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Test if pointer points to allocated memory Andrew C Programming 34 01-26-2004 01:10 PM



Advertisments