Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
From: Ioannis Vranos (ivr_at_remove.this.grad.com)
Date: 03/29/05
- Next message: Martin Krischik: "C++ language support."
- Previous message: Chad R. Meiners: "Re: Teaching new tricks to an old dog (C++ -->Ada)"
- In reply to: REH: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Next in thread: Ioannis Vranos: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Reply: Ioannis Vranos: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Mar 2005 06:10:21 +0300
REH wrote:
>>#include <iostream>
>>#include <cstring>
>>
>>class SomeClass
>>{
>> public:
>>
>> double d;
>> int i;
>> float f;
>> long l;
>>};
>>
>>
>>int main()
>>{
>> using namespace std;
>>
>> unsigned char array[sizeof(SomeClass)];
>>
>> SomeClass obj= {1, 2, 3, 4};
>>
>> memcpy(array, &obj, sizeof(obj));
>>
>> SomeClass *p= reinterpret_cast<SomeClass *>(array);
>>
>> cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
>>}
>>
>>
>>C:\c>temp
>>1 2 3 4
>>
>>C:\c>
>>
>>
>>So since this is *guaranteed* to be portable, why this isn't?
>>
>>
>>#include <iostream>
>>#include <cstring>
>>
>>class SomeClass
>>{
>> public:
>>
>> double d;
>> int i;
>> float f;
>> long l;
>>};
>>
>>
>>int main()
>>{
>> using namespace std;
>>
>> unsigned char array[sizeof(SomeClass)];
>>
>> SomeClass obj= {1, 2, 3, 4};
>>
>> SomeClass *p= new(array)SomeClass(obj);
>>
>> cout<<p->d<<" "<<p->i<<" "<<p->f<<" "<<p->l<<"\n";
>>}
>>
>>
>>C:\c>temp
>>1 2 3 4
>>
>>C:\c>
>>
>>
>>--
>>Ioannis Vranos
>>
>
> No, NEITHER is portable! That both have alignment issues! Both exihibit
> undefined behavior. Show me where in the standard is says you may cast a
> pointer to a char array to a pointer of any other type (other than another
> signed/unsigned char*) and access it. You do this on a system that cannot
> handle misaligned accesses, and your program will most likely crash!
Perhaps you are right. If you have the standard, have a look at 3.9-2, 3.9-4, and 3.9-5
which I think agrees with you.
-- Ioannis Vranos http://www23.brinkster.com/noicys
- Next message: Martin Krischik: "C++ language support."
- Previous message: Chad R. Meiners: "Re: Teaching new tricks to an old dog (C++ -->Ada)"
- In reply to: REH: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Next in thread: Ioannis Vranos: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Reply: Ioannis Vranos: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|