Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: finding array length

Reply
Thread Tools

Re: finding array length

 
 
WANG Cong
Guest
Posts: n/a
 
      02-07-2009
Jung, William wrote:

> how can i found how array length of messagelist in a for loop?
>
> do I write:
> for ( int i = 0; i < sizeof(messagelist); i ++ ){
> //do something to messagelist[i];
> }


That depends on where this loop is and how you get 'messagelist',
if it is inside a function and 'messagelist' is passed as an
argument, then this is wrong, because arrays will become pointers
when they are taken as parameters.

You can try C++ reference, something like this:

template <typename T>
size_t array_size(T & ar)
{
return sizeof(ar) / sizeof(ar[0]);
}


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      02-07-2009
On Feb 7, 9:31 am, WANG Cong <(E-Mail Removed)> wrote:
> Jung, William wrote:
> > how can i found how array length of messagelist in a for loop?


> > do I write:
> > for ( int i = 0; i < sizeof(messagelist); i ++ ){
> > //do something to messagelist[i];
> > }


> That depends on where this loop is and how you get
> 'messagelist', if it is inside a function and 'messagelist' is
> passed as an argument, then this is wrong, because arrays will
> become pointers when they are taken as parameters.


> You can try C++ reference, something like this:


> template <typename T>
> size_t array_size(T & ar)
> {
> return sizeof(ar) / sizeof(ar[0]);
> }


What's wrong with the more or less standard idiom:

template< typename T, std::size_t N >
std::size_t
sizee( T (&array)[ N ] )
{
return N ;
}

This has the advantage that it won't compile in contexts where
the array has already been converted to a pointer (and all
information concerning its length has been lost).

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-08-2009
* James Kanze:
>
> What's wrong with the more or less standard idiom:
>
> template< typename T, std::size_t N >
> std::size_t
> sizee( T (&array)[ N ] )
> {
> return N ;
> }


Mainly three things:

* It doesn't provide a size measure usable at compile time.
(This is easy to fix by using the return sized-type
idiom plus a little macro to hide the ugliness).

* In C++98 it doesn't work with array of local type.
(This is apparently impossible to fix, must wait for C++0x).

* It's more work to implement than just writing a local sizeof.
(This work can involve convincing someone that it's worth having
this function in some project-wide header.)

Now, if SomeOne could a find a way to detect whether a type is local or not...

However, as far as I know that's impossible in C++98.


Cheers & hth.,

- Alf
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-08-2009
On Feb 8, 2:21 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * James Kanze:
> > What's wrong with the more or less standard idiom:


> > template< typename T, std::size_t N >
> > std::size_t
> > sizee( T (&array)[ N ] )
> > {
> > return N ;
> > }


> Mainly three things:


> * It doesn't provide a size measure usable at compile time.
> (This is easy to fix by using the return sized-type
> idiom plus a little macro to hide the ugliness).


Good point. I've never needed is as a compile time constant,
but I can imagine that such cases exist.

In C++0x, of course, the function would be declared constexpr,
and could be used in integral constant expressions.

> * In C++98 it doesn't work with array of local type.
> (This is apparently impossible to fix, must wait for C++0x).


This is a problem with templates in general. And this one has
caused me to move type definitions out of the function, and into
the unnamed namespace.

> * It's more work to implement than just writing a local sizeof.
> (This work can involve convincing someone that it's worth having
> this function in some project-wide header.)


If you do this just once, maybe. I have this in a project-wide
header already. In my case, I didn't have to convince anyone,
I just did it. But in most organizations, you're right---the
politics can represent some effort.

The problem is that you need it anyway if you're doing any
generic programming. (I know, most organizations tend to avoid
templates, because of the problems they cause, but you never
know.) A version of global functions begin, end and size which
are overloaded to handle both built-in arrays and standard
containers, so that you're generic code works with both. (I.e.
in generic code, you don't write container.size(), but size(
container ). So that it can be made to work with types other
than the standard containers.)

> Now, if SomeOne could a find a way to detect whether a type is
> local or not...


> However, as far as I know that's impossible in C++98.


I think it will still be impossible in C++0x. Hopefully,
however, it won't matter---you should be able to do everything
with a local type that you can do with other types. (I don't
have access to a copy of the CD on this machine, however, to
verify.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching values from array and finding length sly Perl Misc 10 09-15-2005 01:20 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Allocating an array.array of a specific length in pyrex Chris Lambacher Python 0 06-08-2005 06:03 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments