Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Getting mem...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 13 Topic 9567 of 9830
Post > Topic >>

Re: Getting memory size

by "Bob" <nospam@[EMAIL PROTECTED] > May 2, 2008 at 04:43 AM

Nimish wrote:

> Hi all
> 
> How can I find the size of memory allocated to the pointer.
> For eg.
> 
> int main()
> {
>              int b[100];
>              int *a = b;
>              ....
>              ....
>              return 0;
> }
> 
> In this function I want to get size of memory allocated to "a"
> pointer.
> How can this be done.
> 

A pointer includes no accessible information about the total size of
the memory block it points at (or the number of elements in an array).


You need to explicitly track that yourself.

For example,

    size_t  elements_of_a = 100;
    size_t  memory_usage_of_a = elements_of_a*sizeof(int);

A technique can be used for arrays is

int main()
{
      int b[100];
      size_t elements_of_b = sizeof(b)/sizeof(b[0]);
           // will yield 100
}

but that does not work for pointers.

int main()
{
      int b[100];
      int *a = b;
      size_t elements_of_a = sizeof(a)/sizeof(a[0]);
          //  will yield sizeof(int *)/sizeof(int)
}

For example, on a 32 bit computer with 8-bit bytes and an int
that is 4 bytes sizeof(int *) will [often] be 4 and sizeof(int)
will also be 4, so elements_of_a will be 1.   The value may be
different on other machines, but will not be related to the
number of elements in b.

If you employ standard containers (std::vector, etc) the container
does keep track of the number of elements it contains.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 13 Posts in Topic:
Getting memory size
Nimish <nimish.bhargav  2008-05-01 13:11:49 
Re: Getting memory size
red floyd <no.spam@[EM  2008-05-02 03:16:06 
Re: Getting memory size
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-02 04:43:59 
Re: Getting memory size
Ron Natalie <ron@[EMAI  2008-05-02 04:43:40 
Re: Getting memory size
Jack Klein <jackklein@  2008-05-02 04:43:52 
Re: Getting memory size
Ulrich Eckhardt <dooms  2008-05-02 04:43:35 
Re: Getting memory size
Francis Glassborow <fr  2008-05-02 04:43:59 
Re: Getting memory size
Ian Collins <ian-news@  2008-05-02 04:43:25 
Re: Getting memory size
"Bob" <nospa  2008-05-02 04:43:55 
Re: Getting memory size
Mathias Gaunard <loufo  2008-05-02 04:43:26 
Re: Getting memory size
Nimish <nimish.bhargav  2008-05-02 12:44:58 
Re: Getting memory size
Michiel.Salters@[EMAIL PR  2008-05-02 12:43:56 
Re: Getting memory size
Francis Glassborow <fr  2008-05-03 16:10:22 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:35:43 CDT 2008.