Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Aliasing/Torek's strtod() experience

Thread Tools

Aliasing/Torek's strtod() experience

Adam Warner
Posts: n/a
Hi all,

Message ID <(E-Mail Removed)> is one of many informative
articles by Chris Torek about C. The particular message discusses aliasing
and concludes with this paragraph:

Under these strict type-aliasing rules, casting from (e.g.) "int *" to
"short *" is not only quite suspicious, it is also likely to cause
puzzling behavior, at least if you expect your "short *" to access or
modify your "int". Even the time-honored, albeit dubious, practise of
breaking a 64-bit IEEE "double" into two 32-bit integers (int or long
depending on the CPU involved) via a union need not work, and sometimes
does not. (We had a problem with strtod() not working right because of
code just like this. It worked in older gcc compilers, and eventually
failed when gcc began doing type-specific alias analysis and

The code I've written below breaks an 8 byte double into two 4 byte signed
integers via a union. How should this code be modified so it conforms to
C's aliasing rules?

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

union u {
double f64;
uint32_t u32[2];

int main() {
double val=strtod("1.23", NULL);
printf("%i %i\n", ((union u) val).u32[0], ((union u) val).u32[1]);
return 0;

Many thanks,
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Experience Beta 2 Kit Elvis ASP .Net 1 07-27-2005 11:39 AM
Samsung Experience (6.17.05) Event Coverage at XYZ Computing Silverstrand Front Page News 3 06-26-2005 07:10 AM
OPINIONS/EXPERIENCE PLEASE: Wireless Network Adapters and Base Stations of different brands Jimmy Dean Wireless Networking 2 02-22-2005 02:42 PM
Storable - Anyone with experience with this perl mod? Harsha Perl 1 09-11-2004 07:39 PM
Is there a person who I experience absurd event that experience? PS2 gamer Cisco 4 06-01-2004 07:52 PM