Pointer VS array pointer

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

    Pointer VS array pointer

    // example

    #include <iostream>

    using namespace std;

    size_t bsearch(int *a) {

    cout<<a<<endl;
    cout<<sizeof(a) / sizeof(a[0])<<endl;

    return 1;
    }


    int main() {

    int a[] = {1,3,5,7,9,11};

    cout<<a<<endl;
    cout<<sizeof(a) / sizeof(a[0])<<endl;;
    cout<< bsearch(a);

    }


    why they are difference ?

    a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?







    thanks

  • Ron Natalie

    #2
    Re: Pointer VS array pointer

    howa wrote:
    >
    size_t bsearch(int *a) {
    a is a pointer to int.
    int a[] = {1,3,5,7,9,11};
    >
    a is a 6 element array of int.
    >
    a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?
    No they do not have the same value. Arrays and
    pointers are different types.

    Comment

    • Default User

      #3
      Re: Pointer VS array pointer

      howa wrote:
      // example
      >
      #include <iostream>
      >
      using namespace std;
      >
      size_t bsearch(int *a) {
      >
      cout<<a<<endl;
      cout<<sizeof(a) / sizeof(a[0])<<endl;
      >
      return 1;
      }
      >
      >
      int main() {
      >
      int a[] = {1,3,5,7,9,11};
      >
      cout<<a<<endl;
      cout<<sizeof(a) / sizeof(a[0])<<endl;;
      cout<< bsearch(a);
      >
      }
      >
      >
      why they are difference ?
      The first is a pointer and the second is an array. They are different
      things.

      In many contexts, the name of an array is converted to a pointer to the
      first element. The two major exceptions are when used with the sizeof
      operator and the address-of operator (&).



      Brian


      Comment

      • Gavin Deane

        #4
        Re: Pointer VS array pointer


        howa wrote:
        // example
        >
        #include <iostream>
        >
        using namespace std;
        >
        size_t bsearch(int *a) {
        Here a is of type pointer-to-int. The fact that the int pointed to
        happens to be the first in a series of ints that are stored in an array
        in the calling function is irrelevant to the type of a here.
        cout<<a<<endl;
        This statement calls the operator<< function overloaded for pointers
        because a is a pointer.
        cout<<sizeof(a) / sizeof(a[0])<<endl;
        a is of type pointer-to-int
        a[0] is of type int
        This statement will output the size of a pointer-to-int divided by the
        size of an int, whatever that is in your implementation.
        >
        return 1;
        }
        >
        >
        int main() {
        >
        int a[] = {1,3,5,7,9,11};
        Here a is an array of 6 ints
        cout<<a<<endl;
        This statement calls the operator<< function overloaded for pointers
        because a decays from an array to a pointer in the context of the call
        to the operator<< function. So you see the same result as inside the
        bsearch function.
        cout<<sizeof(a) / sizeof(a[0])<<endl;;
        a has type array-of-6-ints and in the context of a sizeof expression
        the array name does _not_ decay to a pointer as it did in the call to
        operator<< above. So sizeof a is the size of the whole array, 6 x the
        size of int. a[0] is of type int so this statement will output (6 x the
        size of an int) divied by the size of an int, i.e. the output will be
        6.
        cout<< bsearch(a);
        >
        }
        >
        >
        why they are difference ?
        >
        a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?
        Because in one case a is an array of six ints and in the other case a
        is a pointer and those two entities are not the same size. (In both
        cases a[0] is an int so is the same size in each case)

        Gavin Deane

        Comment

        • Salt_Peter

          #5
          Re: Pointer VS array pointer


          howa wrote:
          // example
          >
          #include <iostream>
          >
          using namespace std;
          >
          size_t bsearch(int *a) {
          >
          cout<<a<<endl;
          cout<<sizeof(a) / sizeof(a[0])<<endl;
          >
          return 1;
          }
          >
          >
          int main() {
          >
          int a[] = {1,3,5,7,9,11};
          >
          cout<<a<<endl;
          cout<<sizeof(a) / sizeof(a[0])<<endl;;
          cout<< bsearch(a);
          >
          }
          >
          >
          why they are difference ?
          >
          a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?
          >
          a pointer to int is not an array. An array needs to keep a const count
          of the number of elements. A pointer to int needs no such thing. In
          the old days, nobody thought about the importance of objects, so you
          can't pass an array by value, by pointor nor by reference (unless you
          wrap an allocated array in an object).

          Does that suck? Nope, since a std::vector solves all those problems
          (and more).

          #include <iostream>
          #include <vector>

          size_t getsize(std::ve ctor< int >& r_v)
          {
          return r_v.size();
          }

          int main()
          {
          std::vector< int vn(6);

          std::cout << &vn << std::endl;
          std::cout << getsize( vn ) << std::endl;
          std::cout << vn.size() << std::endl;
          return 0;
          }

          /*
          0x7fffee50abf0
          6
          6
          */

          Comment

          • Bart

            #6
            Re: Pointer VS array pointer

            Salt_Peter wrote:
            a pointer to int is not an array. An array needs to keep a const count
            of the number of elements. A pointer to int needs no such thing. In
            the old days, nobody thought about the importance of objects, so you
            can't pass an array by value, by pointor nor by reference (unless you
            wrap an allocated array in an object).
            It's quite possible to pass arrays by reference, but the size has to be
            fixed:

            void foo(int (&myarray)[10]);

            declares a function taking a reference to an array of 10 ints.

            Regards,
            Bart.

            Comment

            • Default User

              #7
              Re: Pointer VS array pointer

              Bart wrote:
              Salt_Peter wrote:
              a pointer to int is not an array. An array needs to keep a const
              count of the number of elements. A pointer to int needs no such
              thing. In the old days, nobody thought about the importance of
              objects, so you can't pass an array by value, by pointor nor by
              reference (unless you wrap an allocated array in an object).
              >
              It's quite possible to pass arrays by reference, but the size has to
              be fixed:
              >
              void foo(int (&myarray)[10]);
              >
              declares a function taking a reference to an array of 10 ints.

              Similarly pointers to arrays can be passed:

              void foo(int (*myarray)[10]);

              This would be called with something like:


              int bar[10];

              foo(&bar);




              Brian

              Comment

              • Salt_Peter

                #8
                Re: Pointer VS array pointer


                Default User wrote:
                Bart wrote:
                >
                Salt_Peter wrote:
                a pointer to int is not an array. An array needs to keep a const
                count of the number of elements. A pointer to int needs no such
                thing. In the old days, nobody thought about the importance of
                objects, so you can't pass an array by value, by pointor nor by
                reference (unless you wrap an allocated array in an object).
                It's quite possible to pass arrays by reference, but the size has to
                be fixed:

                void foo(int (&myarray)[10]);

                declares a function taking a reference to an array of 10 ints.
                >
                >
                Similarly pointers to arrays can be passed:
                >
                void foo(int (*myarray)[10]);
                >
                This would be called with something like:
                >
                >
                int bar[10];
                >
                foo(&bar);
                >
                Brian
                Thanks to both of you. I stand corrected.
                This could indeed be useful, though i had to go ahead and screw with
                the issue:

                I can unsderstand that template deduction is taking place in the
                functions below.
                The question is: is the following legal for a function template?

                #include <iostream>

                template< typename T, const size_t Size >
                void array_ptr(T (*myarray)[Size])
                {
                size_t sz = sizeof(*myarray ) / sizeof(T);
                std::cout << "array size = " << sz;
                std::cout << std::endl;
                for (size_t i = 0; i < sz; ++i)
                {
                std::cout << (*myarray)[i] << "\n";
                }
                }

                template< typename T, const size_t Size >
                void array_ref(T (&myarray)[Size])
                {
                size_t sz = sizeof(myarray) / sizeof(T);
                std::cout << "array size = " << sz;
                std::cout << std::endl;
                for (size_t i = 0; i < sz; ++i)
                {
                std::cout << myarray[i] << "\n";
                }
                }

                int main()
                {
                int array[] = {0,1,2,3,4};
                array_ptr(&arra y);
                array_ref(array );

                double narray[] = {0.0,1.1,2.2,3. 3};
                array_ref(narra y);

                std::string sarray[] = {"string 0", "string 1", "string 2"};
                array_ref(sarra y);

                return 0;
                }

                /*
                array size = 5
                0
                1
                2
                3
                4
                array size = 5
                0
                1
                2
                3
                4
                array size = 4
                0
                1.1
                2.2
                3.3
                array size = 3
                string 0
                string 1
                string 2
                */

                Comment

                • red floyd

                  #9
                  Re: Pointer VS array pointer

                  Salt_Peter wrote:
                  I can unsderstand that template deduction is taking place in the
                  functions below.
                  The question is: is the following legal for a function template?
                  >
                  #include <iostream>
                  >
                  template< typename T, const size_t Size >
                  void array_ptr(T (*myarray)[Size])
                  {
                  size_t sz = sizeof(*myarray ) / sizeof(T);
                  size_t sz = Size; // but why bother? Just use Size.
                  std::cout << "array size = " << sz;
                  std::cout << std::endl;
                  for (size_t i = 0; i < sz; ++i)
                  {
                  std::cout << (*myarray)[i] << "\n";
                  }
                  }
                  >
                  template< typename T, const size_t Size >
                  void array_ref(T (&myarray)[Size])
                  {
                  size_t sz = sizeof(myarray) / sizeof(T);
                  see above
                  std::cout << "array size = " << sz;
                  std::cout << std::endl;
                  for (size_t i = 0; i < sz; ++i)
                  {
                  std::cout << myarray[i] << "\n";
                  }
                  }
                  [redacted]

                  Comment

                  • Salt_Peter

                    #10
                    Re: Pointer VS array pointer


                    red floyd wrote:
                    Salt_Peter wrote:
                    >
                    I can unsderstand that template deduction is taking place in the
                    functions below.
                    The question is: is the following legal for a function template?

                    #include <iostream>

                    template< typename T, const size_t Size >
                    void array_ptr(T (*myarray)[Size])
                    {
                    size_t sz = sizeof(*myarray ) / sizeof(T);
                    >
                    size_t sz = Size; // but why bother? Just use Size.
                    yes, why not indeed. Thanks
                    >
                    std::cout << "array size = " << sz;
                    std::cout << std::endl;
                    for (size_t i = 0; i < sz; ++i)
                    {
                    std::cout << (*myarray)[i] << "\n";
                    }
                    }

                    template< typename T, const size_t Size >
                    void array_ref(T (&myarray)[Size])
                    {
                    size_t sz = sizeof(myarray) / sizeof(T);
                    >
                    see above
                    std::cout << "array size = " << sz;
                    std::cout << std::endl;
                    for (size_t i = 0; i < sz; ++i)
                    {
                    std::cout << myarray[i] << "\n";
                    }
                    }
                    [redacted]

                    Comment

                    • Salt_Peter

                      #11
                      Re: Pointer VS array pointer


                      red floyd wrote:
                      Salt_Peter wrote:
                      >
                      I can unsderstand that template deduction is taking place in the
                      functions below.
                      The question is: is the following legal for a function template?

                      #include <iostream>

                      template< typename T, const size_t Size >
                      void array_ptr(T (*myarray)[Size])
                      {
                      size_t sz = sizeof(*myarray ) / sizeof(T);
                      >
                      size_t sz = Size; // but why bother? Just use Size.
                      Yes, of course, you mean use the variable Size directly.
                      i'm just testing the theory in the above, convincing myself of what
                      should be obvious.
                      Its in the loop below that the constant fits the bill perfectly.
                      Thanks for input, will do
                      >
                      std::cout << "array size = " << sz;
                      std::cout << std::endl;
                      for (size_t i = 0; i < sz; ++i)
                      {
                      std::cout << (*myarray)[i] << "\n";
                      }
                      }

                      template< typename T, const size_t Size >
                      void array_ref(T (&myarray)[Size])
                      {
                      size_t sz = sizeof(myarray) / sizeof(T);
                      >
                      see above
                      std::cout << "array size = " << sz;
                      std::cout << std::endl;
                      for (size_t i = 0; i < sz; ++i)
                      {
                      std::cout << myarray[i] << "\n";
                      }
                      }
                      [redacted]

                      Comment

                      • Nick Keighley

                        #12
                        Re: Pointer VS array pointer

                        Salt_Peter wrote:

                        <snip>
                        a pointer to int is not an array. An array needs to keep a const count
                        of the number of elements.
                        since when?
                        A pointer to int needs no such thing. In
                        the old days, nobody thought about the importance of objects,
                        I think C had different design goals from C++. That doesn't necessarily

                        mean it was wrong.

                        <snip>

                        --
                        Nick Keighley

                        Comment

                        • Kai-Uwe Bux

                          #13
                          Re: Pointer VS array pointer

                          Nick Keighley wrote:
                          Salt_Peter wrote:
                          >
                          <snip>
                          >
                          >a pointer to int is not an array. An array needs to keep a const count
                          >of the number of elements.
                          >
                          since when?
                          Since the requirement was introduced that its destructor calls the
                          destructor for all objects stored. Therefore, you can get the size of an
                          array when it dies by hijacking the destructor. The compiler, therefore,
                          will kept track of the size somewhere:

                          #include <iostream>
                          #include <new>

                          struct X {

                          static
                          unsigned & count ( void ) {
                          static unsigned dummy = 0;
                          return ( dummy );
                          }

                          ~X ( void ) {
                          ++ count();
                          }

                          };

                          int main ( void ) {
                          {
                          {
                          X array [300];
                          X::count() = 0;
                          }
                          std::cout << X::count() << '\n';
                          }
                          {
                          unsigned size;
                          while ( std::cin >size ) {
                          X* x_ptr = new X [size];
                          X::count() = 0;
                          delete [] x_ptr;
                          std::cout << X::count() << '\n';
                          }
                          }
                          }

                          echo 16 | a.out
                          300
                          16

                          However:
                          a) For types with trivial destructor, the compiler can optimize the count
                          away since it is not used.
                          b) If the size is known at compile time, the compiler can use that knowledge
                          instead of keeping a count.


                          [snip]


                          Best

                          Kai-Uwe Bux

                          Comment

                          Working...