How to check allocated memory size

Discussion in 'C++' started by NewToCPP, Dec 8, 2005.

  1. NewToCPP

    NewToCPP Guest

    Is there any way I can check the size of the Memory allocated by "new"?
     
    NewToCPP, Dec 8, 2005
    #1
    1. Advertising

  2. NewToCPP

    Guest

    NewToCPP wrote:

    > Is there any way I can check the size of the Memory allocated by "new"?


    new allocates as much memory as you ask it to. If your question isn't
    answered here

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html

    post some code including an allocation with new that shows what you are
    asking.

    Gavin Deane
     
    , Dec 8, 2005
    #2
    1. Advertising

  3. NewToCPP wrote:
    > Is there any way I can check the size of the Memory allocated by "new"?
    >


    Generally speaking, you asked it to allocate a particular object, right?
    That object has a size. You can query it by saying 'sizeof(*ptr)', where
    'ptr' is the pointer you got from the 'new'. If you used 'new[]', then
    you need to multiply it by the value of the expression in the brackets.

    Of course, due to some variations in memory management implementations,
    "true" allocation will be different from the "theoretical" one. In order
    to learn the "true" allocation, you need to use either the implementation-
    specific or the platform-specific means, whichever is available. There is
    no way in the Standard C++ to find out how much "true" memory was used to
    accommodate the objects created by 'new' or 'new[]'.

    V
     
    Victor Bazarov, Dec 8, 2005
    #3
  4. On 8 Dec 2005 09:32:25 -0800, "NewToCPP" <> wrote:

    >Is there any way I can check the size of the Memory allocated by "new"?


    What Victor said...Also, be aware that "new" (and "delete") can be
    overloaded by a class to do something entirely different from the
    default implementations.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 8, 2005
    #4
  5. NewToCPP

    NewToCPP Guest

    Victor & Gavin,

    Thanks for the replies.

    Class ABC
    {
    public:
    int* p;
    ABC {p = new int[10]; }
    ~ABC { delete [] p; }
    };

    ABC* a;

    a = new ABC;

    In this example constructor of ABC is creating more memory. When I did
    the new on ABC is there any way to find out how much memory we used. I
    mean I want to know if I can find it using any methods or something
    like that.
     
    NewToCPP, Dec 8, 2005
    #5
  6. NewToCPP

    Mike Wahler Guest

    "NewToCPP" <> wrote in message
    news:...
    > Victor & Gavin,
    >
    > Thanks for the replies.
    >
    > Class ABC
    > {
    > public:
    > int* p;
    > ABC {p = new int[10]; }
    > ~ABC { delete [] p; }
    > };
    >
    > ABC* a;
    >
    > a = new ABC;
    >
    > In this example constructor of ABC is creating more memory. When I did
    > the new on ABC is there any way to find out how much memory we used. I
    > mean I want to know if I can find it using any methods or something
    > like that.


    std::cout << sizeof *a + 10 * sizeof *a->p << '\n';

    -Mike
     
    Mike Wahler, Dec 8, 2005
    #6
    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. Laura Heinzmann

    How to check memory size allocated to JVM?

    Laura Heinzmann, Feb 16, 2005, in forum: Java
    Replies:
    1
    Views:
    8,233
    John McGrath
    Feb 16, 2005
  2. Replies:
    5
    Views:
    668
    Matt Wharton
    Dec 9, 2004
  3. mandark_br

    Size of Allocated Memory

    mandark_br, May 6, 2004, in forum: C Programming
    Replies:
    3
    Views:
    584
    Alex Monjushko
    May 6, 2004
  4. Shivanand Kadwadkar
    Replies:
    83
    Views:
    5,011
    Keith Thompson
    Jan 8, 2011
  5. Gene
    Replies:
    0
    Views:
    465
Loading...

Share This Page