Skip site navigation (1) Skip section navigation (2)

Re: libpq 7.4 and binary cursor

From: "Jeroen T(dot) Vermeulen" <jtv(at)xs4all(dot)nl>
To: Stephane Raimbault <stephane(dot)raimbault(at)free(dot)fr>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq 7.4 and binary cursor
Date: 2004-06-10 15:04:12
Message-ID: 20040610150411.GD27490@xs4all.nl (view raw or flat)
Thread:
Lists: 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


In response to

Responses

pgsql-interfaces by date

Next:From: Tom LaneDate: 2004-06-10 15:39:25
Subject: Re: libpq 7.4 and binary cursor
Previous:From: Jeroen T. VermeulenDate: 2004-06-10 14:56:29
Subject: Re: Problem with PQexecPrepared

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group