address of array at index

Discussion in 'C++' started by Chris Forone, May 18, 2013.

  1. Chris Forone

    Chris Forone Guest

    hello group,

    i use the function std::inner_product(&arya[0], &arya[4], &aryb[0],
    0.0f) with the c-style and/or c++11-style array. does the compiler set
    the addresses at compile time or is there a runtime overhead to get the
    addresses of array indices?

    thanks & cheers, chris
     
    Chris Forone, May 18, 2013
    #1
    1. Advertising

  2. Chris Forone

    Stefan Ram Guest

    Chris Forone <> writes:
    >does the compiler set the addresses at compile time


    #include <iostream>
    #include <ostream>

    int const a[ 4 ]={ 0, 0, 0, 0 };

    int main()
    { constexpr const int * b = a + 2;
    constexpr auto diff = b - a;
    ::std::cout << diff << '\n'; }
     
    Stefan Ram, May 18, 2013
    #2
    1. Advertising

  3. Chris Forone

    James Kanze Guest

    On Saturday, May 18, 2013 8:49:40 AM UTC+1, Paavo Helde wrote:
    > Chris Forone <> wrote in news:kn78r4$s38$1
    > @newsreader2.utanet.at:


    > > i use the function std::inner_product(&arya[0], &arya[4], &aryb[0],
    > > 0.0f) with the c-style and/or c++11-style array. does the compiler set
    > > the addresses at compile time or is there a runtime overhead to get the
    > > addresses of array indices?


    > You mean, "addresses of array elements"?


    > In general, std::array is designed to be a minimal overhead replacement
    > for C arrays, so one ought to expect the runtime overhead (over a C-style
    > array) of an indexing operation is zero or negligible. However, this
    > depends on the compiler, compiler options and other settings, most
    > importantly on the optimization level and so-called checked iterator
    > support.


    > Anyway, any runtime overhead is probably not measurable here. I would
    > worry more about avoiding undefined behavior in your code, &arya[4] is an
    > illegal operation if the array only contains 4 elements, one should
    > instead use arya.end() or at least arya.data()+4. If there is a
    > possibility that the array is empty, then also &arya[0] becomes an
    > illegal operation and should be replaced by arya.begin() or arya.data().


    Alternatively, with C++11 (which is necessary for std::array)
    and a C style array, one could use std::begin and std::end.
    Pre-C++11, of course, you'd use the C style array and the
    corresponding functions from your tool box.

    --
    James
     
    James Kanze, May 20, 2013
    #3
    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. candide
    Replies:
    65
    Views:
    1,408
  2. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,145
    Keith Thompson
    Sep 20, 2011
  3. Mronoal

    About use index and pointer address array

    Mronoal, Sep 26, 2011, in forum: C Programming
    Replies:
    1
    Views:
    339
    Mronoal
    Sep 26, 2011
  4. Shawn W_
    Replies:
    5
    Views:
    288
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    306
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page