amount of memory allocated to a pointer

Discussion in 'C Programming' started by pratap, Sep 12, 2007.

  1. pratap

    pratap Guest

    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 ?
    pratap, Sep 12, 2007
    #1
    1. Advertising

  2. pratap

    Mark Bluemel Guest

    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.
    Mark Bluemel, Sep 12, 2007
    #2
    1. Advertising

  3. 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
    Richard Heathfield, Sep 12, 2007
    #3
  4. pratap

    pratap Guest

    On Sep 12, 2:50 pm, Richard Heathfield <> 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! !-)
    pratap, Sep 12, 2007
    #4
  5. 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 <>
    André Gillibert, Sep 12, 2007
    #5
  6. "pratap" <> schrieb im Newsbeitrag
    news:...
    > On Sep 12, 2:50 pm, Richard Heathfield <> 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
    Joachim Schmitz, Sep 12, 2007
    #6
  7. pratap

    Army1987 Guest

    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
    Army1987, Sep 12, 2007
    #7
  8. pratap

    jacob navia Guest

    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...
    jacob navia, Sep 12, 2007
    #8
  9. pratap

    CBFalconer Guest

    pratap wrote:
    > Richard Heathfield <> 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
    CBFalconer, Sep 12, 2007
    #9
  10. pratap

    Chris Hills Guest

    In article <>, CBFalconer
    <> writes
    >pratap wrote:
    >> Richard Heathfield <> 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 /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Sep 12, 2007
    #10
  11. 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];


    Consider that those lines are syntax errors in C. Perhaps you meant to
    post to <news:comp.lang.c++> 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.
    Martin Ambuhl, Sep 12, 2007
    #11
  12. "Martin Ambuhl" <> wrote in message
    news:...
    > 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];

    >
    > Consider that those lines are syntax errors in C. Perhaps you meant to
    > post to <news:comp.lang.c++> 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).

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Sep 13, 2007
    #12
  13. On 2007-09-13 12:54, Malcolm McLean wrote:
    > "Martin Ambuhl" <> wrote in message
    > news:...
    >> pratap wrote:
    >>> how could i find out how much memory is blocked(or has been allocated
    >>> to a pointer)
    >>>
    >>> consider,
    >>> int *p=new int;


    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.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 13, 2007
    #13
  14. pratap

    CBFalconer Guest

    Erik Wikström wrote:
    > Malcolm McLean wrote:
    >> "Martin Ambuhl" <> wrote in message
    >>> pratap wrote:
    >>>
    >>>> how could i find out how much memory is blocked(or has been
    >>>> allocated to a pointer)
    >>>>
    >>>> consider,
    >>>> int *p=new int;

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

    --
    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
    CBFalconer, Sep 13, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    619
    Matt Wharton
    Dec 9, 2004
  2. Andrew

    Test if pointer points to allocated memory

    Andrew, Jan 21, 2004, in forum: C Programming
    Replies:
    34
    Views:
    940
    Daniel Haude
    Jan 26, 2004
  3. Ankit Raizada

    memory allocated to a pointer

    Ankit Raizada, Aug 3, 2005, in forum: C Programming
    Replies:
    16
    Views:
    532
    Christian Kandeler
    Aug 4, 2005
  4. Malcolm McLean
    Replies:
    2
    Views:
    409
    CBFalconer
    Sep 13, 2007
  5. Gennady Bystritsky
    Replies:
    4
    Views:
    413
    Gennady Bystritsky
    Jul 27, 2009
Loading...

Share This Page