Re: valarray <vallaray<T> > efficiency
On Jan 5, 3:01 pm, Ioannis Vranos <j...@no.spam> wrote:
jkherci...@gmx.net wrote:
Let's take a step at a time. Is the following guaranteed to
work (to output 50 zeros) always?
#include <iostream>
inline void some_func(int *p, const std::size_t SIZE)
{
using namespace std;
for(size_t i=0; i<SIZE; ++i)
cout<< p[i]<< " ";
}
int main()
{
int array[10][5]= {0};
some_func(array[0], sizeof(array)/sizeof(**array));
std::cout<< std::endl;
}
I think, the code has undefined behavior. See the following
thread for a similar case:
groups.google.com/group/comp.lang.c++/browse_frm/thread/9c501bae821bd406=
AFAIK it has not undefined behavior.
According to both the C and the C++ standard, it is undefined
behavior.
In all kinds of array, its members are in a sequence. Also all
POD objects can be considered as sequences of chars or
unsigned chars (bytes), and we can read them as such.
Reading an object as a sequence of bytes is a special case, and
I'm not sure how it applies here. In the above code, however,
array[0] is an array of 5 ints. The conversion to int* results
in a pointer to the first element in an array of 5 ints. The C
standard was very carefully worded to allow an implementation to
check this, and there have been implementations (and maybe still
are) which actually checked this. (Checking is not widespread,
because it requires fat pointers---the pointer must include not
only the address, but the legal bounds---, which have a very
definite negative impact on performance.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34