| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Kitty No spam" |
| Date: |
05 Dec 2004 11:45:06 AM |
| Object: |
simple question |
Given a integer array. How can I know the total size of this array? Thanks.
.
|
|
| User: "Jonathan Turkanis" |
|
| Title: Re: simple question |
05 Dec 2004 12:50:53 PM |
|
|
"Kitty" <No spam> wrote in message news:41b34916$1_3@rain.i-cable.com...
Given a integer array. How can I know the total size of this array? Thanks.
Here's another way, which could be described as "cutting a daisy with a
daisy-cutter":
#include <iostream>
#include <boost/range/detail/sizer.hpp>
int main()
{
int arr[100];
std::cout << "Size in bytes: " << sizeof(arr) << '\n';
std::cout << "Number of elements: " << sizeof( boost::sizer(arr) ) << '\n';
}
Jonathan
.
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: simple question |
05 Dec 2004 12:05:25 PM |
|
|
"Kitty" <No spam> wrote:
Given a integer array. How can I know the total size of this array?
Thanks.
#include <iostream>
int main()
{
int arr[100];
std::cout << "Size in bytes: " << sizeof(arr) << '\n';
std::cout << "Number of elements: " << sizeof(arr)/sizeof(*arr) << '\n';
}
.
|
|
|
| User: "Jon Bell" |
|
| Title: Re: simple question |
05 Dec 2004 01:09:21 PM |
|
|
In article <covil5$ltu$05$1@news.t-online.com>,
Rolf Magnus <ramagnus@t-online.de> wrote:
"Kitty" <No spam> wrote:
Given a integer array. How can I know the total size of this array?
Thanks.
#include <iostream>
int main()
{
int arr[100];
std::cout << "Size in bytes: " << sizeof(arr) << '\n';
std::cout << "Number of elements: " << sizeof(arr)/sizeof(*arr) << '\n';
}
Kitty should beware that if she passes the array to a function, this
method will not work inside the function. Inside the function, all you
have is a pointer to the array, so sizeof(arr) simply returns the size of
the pointer itself. In fact, inside the function there is no way to find
out the "actual" size of the array, except by passing it to the function
as a separate parameter. This is one reason why std::vector should be
preferred to arrays, except in situations that require arrays for some
good reason.
--
Jon Bell <jtbellm4h@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
.
|
|
|
|
| User: "Kitty No spam" |
|
| Title: Re: simple question |
05 Dec 2004 12:18:18 PM |
|
|
thanks.
"Rolf Magnus" <ramagnus@t-online.de>
???????:covil5$ltu$05$1@news.t-online.com...
"Kitty" <No spam> wrote:
Given a integer array. How can I know the total size of this array?
Thanks.
#include <iostream>
int main()
{
int arr[100];
std::cout << "Size in bytes: " << sizeof(arr) << '\n';
std::cout << "Number of elements: " << sizeof(arr)/sizeof(*arr) << '\n';
}
.
|
|
|
|
|
| User: "Bryan Hackney" |
|
| Title: Re: simple question |
05 Dec 2004 12:08:45 PM |
|
|
Kitty wrote:
Given a integer array. How can I know the total size of this array? Thanks.
sizeof( array ) / sizeof( int )
It's a compile time thing.
.
|
|
|
|

|
Related Articles |
|
|