Size of memory Pointed-to

Discussion in 'C Programming' started by Winsk, Oct 7, 2007.

  1. Winsk

    Winsk Guest

    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 ?
    Winsk, Oct 7, 2007
    #1
    1. Advertising

  2. "Winsk" <> wrote 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
    Malcolm McLean, Oct 7, 2007
    #2
    1. Advertising

  3. 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
    Richard Heathfield, Oct 7, 2007
    #3
  4. Winsk

    santosh Guest

    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.
    santosh, Oct 8, 2007
    #4
  5. santosh <> writes:
    [...]
    > 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) <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"
    Keith Thompson, Oct 8, 2007
    #5
    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:
    4,166
  2. Replies:
    0
    Views:
    430
  3. Frodo Baggins
    Replies:
    12
    Views:
    654
    dbtid
    Jan 3, 2007
  4. Paul
    Replies:
    6
    Views:
    835
    Remo D.
    Nov 29, 2007
  5. bravetanveer

    Size of list pointed by key, in a hash

    bravetanveer, Mar 3, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    115
    A. Sinan Unur
    Mar 3, 2006
Loading...

Share This Page