Re: libpq 7.4 and binary cursor - Mailing list pgsql-interfaces

From Jeroen T. Vermeulen
Subject Re: libpq 7.4 and binary cursor
Date
Msg-id 20040610150411.GD27490@xs4all.nl
Whole thread Raw
In response to libpq 7.4 and binary cursor  (Stephane Raimbault <stephane.raimbault@free.fr>)
Responses Re: libpq 7.4 and binary cursor  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-interfaces
On Thu, Jun 10, 2004 at 04:30:27PM +0200, Stephane Raimbault wrote:

> union {
>     double d; 
>     int64_t i64;
> } swap;
> uint32_t tab_uint32[2];
[...]

> /* Fusion */
> swap.i64 = tab_uint32[0];
> swap.i64 <<= 32;
> swap.i64 |= tab_uint32[1];
> 
> /* Cast */
> return swap.d;

This trick may work on some compilers and/or platforms, but it's not
correct C.  The language does not guarantee that the members of a union
will be allocated in the exact same address, or even that they will
overlap.  What if it registerizes swap.d and/or swap.i64?  It may even
recognize one of the two as uninitialized, and the other as a dead value.
That will get you very compact code, but not the effect you want.

To see how it may go wrong, imagine your compiler implemented "union"
as "#define union struct".  I think this is all described in the C FAQ,
BTW.


Jeroen



pgsql-interfaces by date:

Previous
From: "Jeroen T. Vermeulen"
Date:
Subject: Re: Problem with PQexecPrepared
Next
From: Tom Lane
Date:
Subject: Re: libpq 7.4 and binary cursor