Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!brl-tgr!gwyn From: gwyn@brl-tgr.ARPA (Doug Gwyn) Newsgroups: net.lang.c Subject: C portability gotcha, example Message-ID: <2482@brl-tgr.ARPA> Date: Sun, 27-Oct-85 03:26:04 EST Article-I.D.: brl-tgr.2482 Posted: Sun Oct 27 03:26:04 1985 Date-Received: Tue, 29-Oct-85 01:15:38 EST Distribution: net Organization: Ballistic Research Lab Lines: 45 One of the fellows here ran across a C coding style problem that caused an application to break when ported from little- endian machines to a big-endian. I am posting this to help those who may encounter similar problems in the future. /* modeled after a grammar that builds expression trees: */ struct node { int op; struct node *left, *right; double val; } *lp, *rp, *p, *makenode(); double value; /* constant value from lex */ ... p = makenode( 'x', lp, rp ); /* create multiply node */ ... p = makenode( 'k', value ); /* create real constant node */ ... struct node *makenode( op, arga, argb ) int op; struct node *arga, *argb; { extern char *malloc(); struct node *new = (struct node *)malloc( sizeof(struct node) ); if ( new == 0 ) punt( "no space" ); switch ( new->op = op ) /* node type */ { case 'x': /* (binary) multiplication */ new->left = arga, new->right = argb; break; ... case 'k': /* real constant */ new->val = *(double *)&arga; /* XXX */ break; } return new; } The code failed at point "XXX". The exercise for the student is to figure out precisely WHY it fails on some machines but works on others. Do not post your answers, unless you think you see a unique twist; this is a well-known C coding trap that is in violation of language standards. A second exercise for the student is to figure out how to fix this code; note that simply adding a typecast (struct node *) when the function is invoked is NOT sufficient. No need to post these answers either. Maybe Laura will include this in her book.