Topic: Intersection of arrays
Author: "Homer Meyer" <homer@cqg.com>
Date: Sat, 13 Jan 2001 17:56:54 GMT Raw View
<kayakk@my-deja.com> wrote in message news:93mjqs$vpp$1@nnrp1.deja.com...
<SNIP>
> seq<int,10> s1={1,2,3,4,5,6,7,8,9,10};
> seq<int,6>& s2=reinterpret_cast<seq<int,6>&>(s1.values[4]);
> std::copy(s2.begin(),
> s2.end(),
> std::ostream_iterator<int>(std::cout," "));
> std::cout << s2[0];
<SNIP>
Look in the standard for reinterpret_cast... 5.2.10/9 and 10. You will see
that the result of your reinterpret_cast is unspecified except when you cast
it back to the same reference type. Now, look up the meaning of unspecified
behaviour in the standard. This is in 1.3.13. Note that it says that
unspecified behaviour depends on the implementation and the implementation
is not required to document the behaviour. So, the answer to your questions
is: maybe it is safe and maybe not. You certainly cannot depend on the
code doing the same thing on different implementations or even on different
versions of the same implementation. However, I would think that it is
likely to work on most implementations, but that's not the same as it being
100% safe on 100% of all implementations.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: kayakk@my-deja.com
Date: Sat, 13 Jan 2001 23:33:15 GMT Raw View
In article <t5udvr3i04hs78@news.supernews.com>,
"Homer Meyer" <homer@cqg.com> wrote:
> <kayakk@my-deja.com> wrote in message
news:93mjqs$vpp$1@nnrp1.deja.com...
> <SNIP>
> > seq<int,10> s1={1,2,3,4,5,6,7,8,9,10};
> > seq<int,6>& s2=reinterpret_cast<seq<int,6>&>(s1.values[4]);
> > std::copy(s2.begin(),
> > s2.end(),
> > std::ostream_iterator<int>(std::cout," "));
> > std::cout << s2[0];
> <SNIP>
>
> Look in the standard for reinterpret_cast... 5.2.10/9 and 10. You
will see
> that the result of your reinterpret_cast is unspecified except when
you cast
> it back to the same reference type.
I don't own the final standard, only cd2, however the place seems not
to have been changed.
> Now, look up the meaning of unspecified
> behaviour in the standard. This is in 1.3.13. Note that it says that
> unspecified behaviour depends on the implementation and the
implementation
> is not required to document the behaviour. So, the answer to your
questions
> is: maybe it is safe and maybe not. You certainly cannot depend on
the
> code doing the same thing on different implementations or even on
different
> versions of the same implementation. However, I would think that it
is
> likely to work on most implementations, but that's not the same as it
being
> 100% safe on 100% of all implementations.
>
Thanks for the explanation.
However, I see it very difficult to find a reason why that code should
not work as expected on a particular implementation. I mean, the array
has offset 0 in the struct (no ctors, no dtrors, no vfun), and every
cast "places" a reference to a memory zone where there are elements of
another array holding the same kind of objects, thus there are no
alignment problems. It is rather a little game of pointers...
Best Regards,
Giuseppe Polverini
Sent via Deja.com
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: kayakk@my-deja.com
Date: Fri, 12 Jan 2001 11:01:08 GMT Raw View
I asked a similar thing in comp.lang.c++, but we didn't really find a
final word, so I try here:
if I have this struct:
template<class T, int N>
struct seq
{
T values[N];
T& operator[] (int idx){return values[idx];}
const T& operator[](int idx)const{
return values[idx];
}
T* begin(){return values;}
T* end(){return values+N;}
//const begin(), const end()...
};
will the following code be safe?
seq<int,10> s1={1,2,3,4,5,6,7,8,9,10};
seq<int,6>& s2=reinterpret_cast<seq<int,6>&>(s1.values[4]);
std::copy(s2.begin(),
s2.end(),
std::ostream_iterator<int>(std::cout," "));
std::cout << s2[0];
output: 5 6 7 8 9 10 5
I think it should, but I don't find anything in the standard.
I think I can "intersect" the two arrays as long as s2 doesn't overrun
the space allocated for s1 and I bind s2 to the address of an element
of s1
(
ie I don't try to do
seq<int,6>& s2=*reinterpret_cast<seq<int,6>*>(((char*)s1.values)+3);
)
because the alignment requirements of seq<T,N>::values should be those
of T, or at least "setting the address of s2" that way should not give
unpredictable results because seq is just a container of properly-
aligned objects of type T...
or am I missing something?
TIA
Giuseppe Polverini
Sent via Deja.com
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]