Array size

Discussion in 'C++' started by BrianJones, Jul 11, 2004.

  1. BrianJones

    BrianJones Guest

    In java, to work out the size of an array, say it was called a, you would do
    a.length which might return 10 as the number of elements.

    Is there a similar thing you can do in C++? I'd initially thought of using
    strlen(a) since the array I am working with is of type char. However, I
    found this to return a value drastically smaller than the number of elements
    it actually contained

    Thanks
    Ben
     
    BrianJones, Jul 11, 2004
    #1
    1. Advertising

  2. BrianJones wrote:

    > In java, to work out the size of an array, say it was called a, you would do
    > a.length which might return 10 as the number of elements.
    >
    > Is there a similar thing you can do in C++? I'd initially thought of using
    > strlen(a) since the array I am working with is of type char. However, I
    > found this to return a value drastically smaller than the number of elements
    > it actually contained




    Not for built in arrays. You must use standard library containers like
    vector that have a size() member function. Also for regular applications
    you should use standard library containers anyway and do not bother
    yourself with low level stuff.






    Regards,

    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jul 11, 2004
    #2
    1. Advertising

  3. BrianJones wrote:
    > In java, to work out the size of an array, say it was called a, you would do
    > a.length which might return 10 as the number of elements.
    >
    > Is there a similar thing you can do in C++? I'd initially thought of using
    > strlen(a) since the array I am working with is of type char. However, I
    > found this to return a value drastically smaller than the number of elements
    > it actually contained



    If you have the definition of the array handy you can do this:


    template <typename T, unsigned N>
    inline unsigned Length( T (&x)[N] )
    {
    return N;
    }

    int Array[ 230 ];
    extern int ExternArray[]; // don't know the length

    #include <iostream>

    int main()
    {
    std::cout << Length( Array ) << "\n";
    std::cout << Length( ExternArray ) << "\n"; // error
    }

    If you pass around arrays and you need the length, then std::vector is
    the way to go.
     
    Gianni Mariani, Jul 11, 2004
    #3
  4. BrianJones wrote:
    > [...]
    > I'd initially thought of using
    > strlen(a) since the array I am working with is of type char. However, I
    > found this to return a value drastically smaller than the number of elements
    > it actually contained


    Perhaps you have the character '\0' inside your array of chars.
    It servers as an end-of-string marker for strlen().

    hth,
    - J.
     
    Jacek Dziedzic, Jul 11, 2004
    #4
  5. BrianJones

    Arijit Guest

    "BrianJones" <> wrote in message news:<ccro8u$n2p$>...
    > In java, to work out the size of an array, say it was called a, you would do
    > a.length which might return 10 as the number of elements.
    >
    > Is there a similar thing you can do in C++? I'd initially thought of using
    > strlen(a) since the array I am working with is of type char. However, I
    > found this to return a value drastically smaller than the number of elements
    > it actually contained
    >
    > Thanks
    > Ben


    C style strings are terminated by a null character,'\0'. strlen
    actually checks the position of this '\0'. If your array contains
    no '\0',using strlen can actually cause your program to crash. As a
    hack, you might consider placing a sentinel '\0' at the end of the array.
    Then strlen will return the size of the array - 1. But you should really
    consider using a vector.

    -Arijit
     
    Arijit, Jul 12, 2004
    #5
  6. BrianJones

    Arijit Guest

    "BrianJones" <> wrote in message news:<ccro8u$n2p$>...
    > In java, to work out the size of an array, say it was called a, you would do
    > a.length which might return 10 as the number of elements.
    >
    > Is there a similar thing you can do in C++? I'd initially thought of using
    > strlen(a) since the array I am working with is of type char. However, I
    > found this to return a value drastically smaller than the number of elements
    > it actually contained
    >
    > Thanks
    > Ben


    C style strings are terminated by a null character,'\0'. strlen
    actually checks the position of this '\0'. If your array contains
    no '\0',using strlen can actually cause your program to crash. As a
    hack, you might consider placing a sentinel '\0' at the end of the array.
    Then strlen will return the size of the array - 1. But you should really
    consider using a vector.

    -Arijit
     
    Arijit, Jul 12, 2004
    #6
  7. BrianJones

    Anonymous Guest

    You should be using vector in those cases. Your program will work as per the
    suggestions, but when you have store other objects, there wont be any way for
    you to get the size unless you keep a track of the things put in the array.
    Using vector is more "C++ way",compared to the "C" way you are doing.

    --
    Use our news server 'news.foorum.com' from anywhere.
    More details at: http://nnrpinfo.go.foorum.com/
     
    Anonymous, Jul 12, 2004
    #7
  8. BrianJones wrote:

    > In java, to work out the size of an array, say it was called a, you would do
    > a.length which might return 10 as the number of elements.
    >
    > Is there a similar thing you can do in C++? I'd initially thought of using
    > strlen(a) since the array I am working with is of type char. However, I
    > found this to return a value drastically smaller than the number of elements
    > it actually contained
    >
    > Thanks
    > Ben
    >
    >


    Here are some general guidelines:
    1. When working with a collection of characters,
    prefer std::string to char * or char [].

    2. Prefer std::vector to an array.

    3. Length of a string is different than the length
    of an array (or the capacity of the container).
    I could have the string "Hello" in a container
    that has a capacity of 64. The length of the
    string is much smaller than the capacity of
    the container.

    4. The language does not know the length of an
    array that a pointer points to.

    5. When storing strings to a file, decide whether
    you want to specify length then text or text
    terminated by a sentinel value.

    6. Read the FAQ. Understand the FAQ. Read the
    FAQ for the C language as well.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jul 12, 2004
    #8
    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. Zygmunt Krynicki
    Replies:
    1
    Views:
    642
    Ivan Vecerina
    Oct 11, 2003
  2. Denis Palas
    Replies:
    2
    Views:
    725
    Daniel Pitts
    Dec 19, 2006
  3. Replies:
    5
    Views:
    581
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    529
    Bob Hairgrove
    Apr 10, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,733
    Michael Jung
    May 25, 2008
Loading...

Share This Page