Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > address of array at index

Reply
Thread Tools

address of array at index

 
 
Chris Forone
Guest
Posts: n/a
 
      05-18-2013
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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      05-18-2013
Chris Forone <(E-Mail Removed)> 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'; }

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-20-2013
On Saturday, May 18, 2013 8:49:40 AM UTC+1, Paavo Helde wrote:
> Chris Forone <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About use index and pointer address array Mronoal C Programming 1 09-26-2011 06:46 AM
Address of array && address of pointer to array Stanley Rice C Programming 33 09-20-2011 12:47 AM
Making an array wrap, where last index + 1 = first index Shawn W_ Ruby 5 09-16-2009 02:45 PM
Templated array of templates specialized by array index npankey@gmail.com C++ 6 10-12-2008 03:20 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM



Advertisments