array size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Baloff

    array size


    Hello

    why am I getting 6 outputs instead of just 5 from this code?

    thanks

    #include <iostream>
    #include <string>

    using namespace std;

    int main(){
    string a[]=
    {
    "blue", "green", "red", "black", "white"
    };
    for(int i=0; i< (sizeof(a)-1); ++i)
    cout << "item [" << i << "]= " << a[i] << endl;
    }



    item [0]= blue
    item [1]= green
    item [2]= red
    item [3]= black
    item [4]= white
    item [5]=

    Program received signal SIGSEGV, Segmentation fault.
    0x40095847 in std::operator<< <char, std::char_trait s<char>, std::allocator< char> > () from /usr/lib/libstdc++.so.5
    (gdb)
  • Shezan Baig

    #2
    Re: array size


    Baloff wrote:[color=blue]
    >
    > int main(){
    > string a[]=
    > {
    > "blue", "green", "red", "black", "white"
    > };
    > for(int i=0; i< (sizeof(a)-1); ++i)[/color]


    Sizeof(a) returns the *size* of the array, not the number of elements
    in the array. For example, if the size of std::string is 4 bytes, then
    sizeof(a) above would be 4*5 = 20.

    So you are looping from 0 to 19, but once you hit i = 5, you are
    already past the array (this is why it segfaults).

    What you want is the number of elements, not the size of the array.
    You can get this by dividing the sizeof the array by the sizeof each
    element.

    Change your loop condition to:

    for(int i=0; i< sizeof a/sizeof *a; ++i)


    Hope this helps,
    -shez-

    Comment

    • Srini

      #3
      Re: array size

      > Hello[color=blue]
      >
      > why am I getting 6 outputs instead of just 5 from this code?
      >
      > thanks
      >
      > #include <iostream>
      > #include <string>
      >
      > using namespace std;
      >
      > int main(){
      > string a[]=
      > {
      > "blue", "green", "red", "black", "white"
      > };
      > for(int i=0; i< (sizeof(a)-1); ++i)
      > cout << "item [" << i << "]= " << a[i] << endl;
      >
      > }
      >
      > item [0]= blue
      > item [1]= green
      > item [2]= red
      > item [3]= black
      > item [4]= white
      > item [5]=
      >
      > Program received signal SIGSEGV, Segmentation fault.
      > 0x40095847 in std::operator<< <char, std::char_trait s<char>, std::allocator< char> > > () from /usr/lib/libstdc++.so.5[/color]

      Ever tried printing the value of sizeof(a)?? Its 20. No wonder you're
      getting a segfault. Learn what sizeof means and learn to use STL
      iterators.

      Srini

      Comment

      • velthuijsen@hotmail.com

        #4
        Re: array size

        > why am I getting 6 outputs instead of just 5 from this code?[color=blue]
        >
        > thanks
        >
        > #include <iostream>
        > #include <string>
        >
        > using namespace std;
        >
        > int main(){
        > string a[]=
        > {
        > "blue", "green", "red", "black", "white"
        > };
        > for(int i=0; i< (sizeof(a)-1); ++i)
        > cout << "item [" << i << "]= " << a[i] << endl;
        > }
        >
        >
        >
        > item [0]= blue
        > item [1]= green
        > item [2]= red
        > item [3]= black
        > item [4]= white
        > item [5]=
        >
        > Program received signal SIGSEGV, Segmentation fault.
        > 0x40095847 in std::operator<< <char, std::char_trait s<char>, std::allocator< char> > () from /usr/lib/libstdc++.so.5[/color]

        You get only 6 outputs since the program crashes (if it wouldn't do
        that you'd get least 5 * sizeof(std::str ing) values of garbage.
        The problem is that when you do sizeof(a) you get back the total size
        of the array in bytes. a simple addition of
        std::cout << sizeof(a)<< std::endl;
        before the loop should confirm this.

        The for loop is best rewritten to
        for(int i=0; i< (sizeof(a)/sizeof(*a) -1); ++i)

        Comment

        • Shezan Baig

          #5
          Re: array size

          velthuijsen@hot mail.com wrote:[color=blue]
          > The for loop is best rewritten to
          > for(int i=0; i< (sizeof(a)/sizeof(*a) -1); ++i)[/color]


          You need to drop the "-1" (otherwise you will not include the last
          element).

          -shez-

          Comment

          • Gianni Mariani

            #6
            Re: array size

            Baloff wrote:[color=blue]
            > Hello
            >
            > why am I getting 6 outputs instead of just 5 from this code?[/color]
            ....[color=blue]
            > for(int i=0; i< (sizeof(a)-1); ++i)[/color]
            ....

            sizeof(a)/sizeof(*a) is what you need instead of sizeof(a)-1.

            This is commonly written into a macro like ...

            #define Nelem( A ) (sizeof(A)/sizeof(*A))

            .... which gives you the number of elements in the array, however it may
            be interpretted incorrectly so it's much better to use a template that
            will never be interpreted incorrectly.

            const char * x = "The size of this string is unimportant";

            Nelem( x ) will more than likely not be what you think it is and there
            will be no error emitted by the compiler.


            A better practice to get into it to is this:

            template <typename T, unsigned N>
            char ( & NelemArrayFunc( T ( & )[ N ] ) )[ N ];

            #define Nelem( A ) (sizeof( NelemArrayFunc( A ) ))

            In this case Nelem( x ) will be a compile-time error.

            Comment

            • __PPS__

              #7
              Re: array size

              I don't even understand this mix with macroses and strange templates.
              isn't better to simplify everything:

              template<class T, size_t N>inline unsigned int size_of(const T (&)[N]){
              return N;
              }

              and then for the OP's code:
              ...
              for(int i=0; i< size_of(a); ++i)
              cout << "item [" << i << "]= " << a[i] << endl;
              ...

              or a pointer version:
              template<class T, size_t N>inline const T* last_of(const T (&x)[N]){
              return &x[N+1];
              }

              std::string *i = &a[0];
              while(i!=last_o f(a)){
              cout << *i++ << endl;
              }

              Comment

              • Victor Bazarov

                #8
                Re: array size

                __PPS__ wrote:[color=blue]
                > I don't even understand this mix with macroses and strange templates.
                > isn't better to simplify everything:
                >
                > template<class T, size_t N>inline unsigned int size_of(const T (&)[N]){
                > return N;
                > }
                >
                > and then for the OP's code:
                > ...
                > for(int i=0; i< size_of(a); ++i)
                > cout << "item [" << i << "]= " << a[i] << endl;
                > ...
                >
                > or a pointer version:
                > template<class T, size_t N>inline const T* last_of(const T (&x)[N]){
                > return &x[N+1];[/color]

                Should probably be

                return &x[N];

                or

                return x + N;
                [color=blue]
                > }
                >
                > std::string *i = &a[0];
                > while(i!=last_o f(a)){
                > cout << *i++ << endl;
                > }
                >[/color]

                V

                Comment

                • __PPS__

                  #9
                  Re: array size

                  probably I chose a wrong name :)
                  should have been something like template<...> ... end(...)

                  Comment

                  • Victor Bazarov

                    #10
                    Re: array size

                    __PPS__ wrote:[color=blue]
                    > probably I chose a wrong name :)
                    > should have been something like template<...> ... end(...)
                    >[/color]

                    It doesn't matter what the name is. The last element's index is
                    N-1. When you want "one-past-the-end" you should use N. N+1 is
                    simply _wrong_. Just think about it a bit.

                    V

                    Comment

                    • __PPS__

                      #11
                      Re: array size

                      yes, you are right!
                      my error

                      Comment

                      • Gianni Mariani

                        #12
                        Re: array size

                        __PPS__ wrote:[color=blue]
                        > I don't even understand this mix with macroses and strange templates.
                        > isn't better to simplify everything:
                        >
                        > template<class T, size_t N>inline unsigned int size_of(const T (&)[N]){
                        > return N;
                        > }[/color]

                        int x[5];

                        int y[ size_of( x ) ];

                        ?

                        Comment

                        Working...