Ian MacArthur wrote:
> So, without any testing whatsoever, here's a guess; try this -
> 
> //    unsigned int a = (unsigned int)&t; // four more bytes
>      union { void *pv; unsigned char a[sizeof(void*)]; } v;
>      v.pv = (void *)(&t);
>      // we will extract 4 more bytes from the address of t
>      b[8] = v.a[0];
>      b[9] = v.a[1];
>      b[10] = v.a[2];
>      b[11] = v.a[3];

    Nice! Yes, that's definitely The Right Way in this case;
    exactly what unions are for.

    That compiles fine on my 64bit linux machine with -Wall,
    and generates expected results. Made a test program to check it:

----
#include <stdio.h>
#include <time.h>
int main() {
     time_t t = 1234;
     unsigned char b[20];
     union { void *pv; unsigned char a[sizeof(void*)]; } v;
     v.pv = (void *)(&t);
     // we will extract 4 more bytes from the address of t
     b[8] = v.a[0];
     b[9] = v.a[1];
     b[10] = v.a[2];
     b[11] = v.a[3];
     printf("%p %02x %02x %02x %02x\n", (void*)&t, (unsigned int)b[8], 
(unsigned int)b[9], (unsigned int)b[10], (unsigned int)b[11]);
     return(0);
}
----

        Result on my x64 ubuntu:


r...@delta:/var/tmp# g++ -Wall -o foo foo.cxx

r...@delta:/var/tmp# ./foo
0x7fff86b85318 18 53 b8 86
       | | | |  |  |  |  |
       | | | |__|  |  |  |
       | | |_______|  |  |
       | |____________|  |
       |_________________|
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to