pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeroen T. Vermeulen <jtv@xs4all.nl>
To: Stephane Raimbault <stephane.raimbault@free.fr>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: libpq 7.4 and binary cursor
Date: Thu, 10 Jun 2004 17:04:12 +0200
Message-ID: <20040610150411.GD27490@xs4all.nl> (raw)
In-Reply-To: <1086877827.4646.22.camel@picasso.lan>
References: <1086877827.4646.22.camel@picasso.lan>

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




view thread (5+ messages)  latest in thread

Message-ID: <20040610150411.GD27490@xs4all.nl>
Permalink:  ../20040610150411.GD27490@xs4all.nl/
Also on:    postgresql.org/message-id/20040610150411.GD27490@xs4all.nl

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jtv@xs4all.nl, stephane.raimbault@free.fr
  Subject: Re: libpq 7.4 and binary cursor
  In-Reply-To: <20040610150411.GD27490@xs4all.nl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox