Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   'use reinterpret_cast to group neighbored values together' defined inc++ standard? (http://www.velocityreviews.com/forums/t956217-use-reinterpret_cast-to-group-neighbored-values-together-defined-inc-standard.html)

 familiewie@gmail.com 01-06-2013 08:07 PM

'use reinterpret_cast to group neighbored values together' defined inc++ standard?

A quick example:
struct vec {
long d[2];
};

struct segment {
vec A, B;
};

struct polygon {
vec* points;

polygon(unsigned int size) { points=new vec[size]; }
~polygon() { delete[] points; }
};

int main() {
polygon test(4);
test.points[0][0] = 1; test.points[0][1] = 2;
test.points[1][0] = 3; test.points[1][1] = 4;
test.points[2][0] = 5; test.points[2][1] = 6;
test.points[3][0] = 7; test.points[3][1] = 8;
segment *border = reinterpret_cast<segment*>(test.points);
for(int i=0; i<3; ++i){
std::cout << '(' << border->A.d[0] << ", " << border->A.d[1] << "), (" << border->B.d[0] << ", " << border->B.d[1] << ')' << std::endl;
border = reinterpret_cast<segment*>(reinterpret_cast<char*> (border) + sizeof(vec));
}
}

This example works in Visual Studio. Does it also work with any other c/c++ compiler? Meaning is this usage of pointers, well defined in the c/c++ standards or is this plattform/compiler specific?

 Öö Tiib 01-06-2013 08:21 PM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

On Sunday, 6 January 2013 22:07:46 UTC+2, famil...@gmail.com wrote:
> A quick example:

.... snip example that does not compile in Visual Studio.

>
> This example works in Visual Studio.

That is apparently desinformation, it does not.

Np, hope it helps.

 Ian Collins 01-06-2013 08:23 PM

Re: 'use reinterpret_cast to group neighbored values together' definedin c++ standard?

familiewie@gmail.com wrote:
> A quick example:
> struct vec {
> long d[2];
> };
>
> struct segment {
> vec A, B;
> };
>
> struct polygon {
> vec* points;
>
> polygon(unsigned int size) { points=new vec[size]; }
> ~polygon() { delete[] points; }
> };
>
> int main() {
> polygon test(4);
> test.points[0][0] = 1; test.points[0][1] = 2;

These lines shouldn't compile.

> test.points[1][0] = 3; test.points[1][1] = 4;
> test.points[2][0] = 5; test.points[2][1] = 6;
> test.points[3][0] = 7; test.points[3][1] = 8;
> segment *border = reinterpret_cast<segment*>(test.points);

reinterpret_cast is a bunt instrument that just tells the compiler to
interpret a bunch of bits as a particular type. There isn't any type
checking and the types don't have to be related. You could have written

int *border = reinterpret_cast<int*>(test.points);

and it would have compiled.

> This example works in Visual Studio. Does it also work with any other c/c++ compiler? Meaning is this usage of pointers, well defined in the c/c++ standards or is this plattform/compiler specific?

Please remember to wrap your lines are remove the other cap that awful

--
Ian Collins

 familiewie@gmail.com 01-06-2013 10:28 PM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

I do know that my example will compile with any compiler.

I wanted to know, if this mini programm does work exactly the same (with the same outputs), when I compile it with different compilers.

 familiewie@gmail.com 01-06-2013 10:39 PM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

Ups, I just realized, that this does not compile correctly. That is, because I quickly extracted the code from my project.

Here is the corrected version:

#include <iostream>

struct vec {
long d[2];
};

struct segment {
vec A, B;
};

struct polygon {
vec* points;

polygon(unsigned int size) { points=new vec[size]; }
~polygon() { delete[] points; }
};

int main() {
polygon test(4);
test.points[0].d[0] = 1; test.points[0].d[1] = 2;
test.points[1].d[0] = 3; test.points[1].d[1] = 4;
test.points[2].d[0] = 5; test.points[2].d[1] = 6;
test.points[3].d[0] = 7; test.points[3].d[1] = 8;
segment *border = reinterpret_cast<segment*>(test.points);
for(int i=0; i<3; ++i){
std::cout << '(' << border->A.d[0] << ", " << border->A.d[1] << "), (" << border->B.d[0] << ", " << border->B.d[1] << ')' << std::endl;
border = reinterpret_cast<segment*>(reinterpret_cast<char*> (border) + sizeof(vec));
}
return 0;
}

 Ian Collins 01-06-2013 10:39 PM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

familiewie@gmail.com wrote:

>
> I do know that my example will compile with any compiler.
>
> I wanted to know, if this mini programm does work exactly the same
> (with the same outputs), when I compile it with different compilers.

As posted, it won't compile with any compiler.

As I said in my reply (that you forgot to quote), all reinterpret_cast
does is tell the compiler to interpret a bunch of bits as a particular
type. If the data pointed to happens to be of a compatible type and
alignment, it will probably work. If not, it won't.

--
Ian Collins

 familiewie@gmail.com 01-06-2013 10:50 PM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

Sorry, what does that mean, English is not my native language?

 Ian Collins 01-06-2013 10:55 PM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

familiewie@gmail.com wrote:
>
> Sorry, what does that mean, English is not my native language?

this client has), so those of us who use normal news readers have to
scroll along your lines or manually re-wrap them.

Just add a return every 70-80 characters and you'll be doing us a favour.

--
Ian Collins

 Gerhard Fiedler 01-07-2013 01:42 AM

Re: 'use reinterpret_cast to group neighbored values together' defined in c++ standard?

Ian Collins wrote:

> familiewie@gmail.com wrote:
>>
>> Sorry, what does that mean, English is not my native language?

>
> That awful google Usenet interface doesn't add any line breaks (like
> this client has), so those of us who use normal news readers ...

^^^^^^

"normal" (in this context) === "lacking in functionality" (in a "normal"
context)? :)

I'm using a "normal" news reader that can display long lines just fine
for me to read without scrolling. We've come a long way from the
1980s... When I read stuff like this I feel like the guy who came from
the future :)

Who knows, you may even throw together a C++ program that adds this
lacking functionality to your news reader (to get back almost on topic
:)

Gerhard

 Bart van Ingen Schenau 01-07-2013 09:16 AM

Re: 'use reinterpret_cast to group neighbored values together'defined in c++ standard?

On Sun, 06 Jan 2013 14:39:02 -0800, familiewie wrote:

> struct segment {
> vec A, B;
> };
>

<snip>
> segment *border = reinterpret_cast<segment*>(test.points);
> for(int i=0; i<3; ++i){
> std::cout << '(' << border->A.d[0] << ", " << border->A.d[1] << "),
> (" << border->B.d[0] << ", " << border->B.d[1] << ')' << std::endl;
> border = reinterpret_cast<segment*>(reinterpret_cast<char*> (border)
> + sizeof(vec));
> }
> return 0;
> }

The results of this program are not defined by the C++ language. By using
reinterpret_cast and dereferencing the result, you have placed yourself
completely at the mercy of the implementation.

Although they are unlikely to take advantage of it, an implementation is
allowed to put padding between the A and B members of segment, which
would cause your code to fail.

Bart v Ingen Schenau

All times are GMT. The time now is 01:15 PM.