Re: finding array length

Discussion in 'C++' started by WANG Cong, Feb 7, 2009.

  1. WANG Cong

    WANG Cong Guest

    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;
    > }


    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]);
    }
    WANG Cong, Feb 7, 2009
    #1
    1. Advertising

  2. WANG Cong

    James Kanze Guest

    On Feb 7, 9:31 am, WANG Cong <> 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;
    > > }


    > 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:
    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
    James Kanze, Feb 7, 2009
    #2
    1. Advertising

  3. * 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
    Alf P. Steinbach, Feb 8, 2009
    #3
  4. WANG Cong

    James Kanze Guest

    On Feb 8, 2:21 am, "Alf P. Steinbach" <> 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:
    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
    James Kanze, Feb 8, 2009
    #4
    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. Mitchua
    Replies:
    5
    Views:
    2,738
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,957
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Tom
    Replies:
    3
    Views:
    210
    salsablr
    Dec 20, 2004
  4. Tuan  Bui
    Replies:
    14
    Views:
    473
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. sly
    Replies:
    10
    Views:
    161
    Anno Siegel
    Sep 15, 2005
Loading...

Share This Page