Re: ISO: help porting C code to 64 bit linux platform
- From: Jan Panteltje <pNaonStpealmtje@xxxxxxxxx>
- Date: Sat, 13 Jan 2007 11:25:04 GMT
On a sunny day (Fri, 12 Jan 2007 19:25:08 +0000) it happened
=?iso-8859-1?Q?M=E5ns_Rullg=E5rd?= <mru@xxxxxxxxxxxxx> wrote in
<yw1xlkk8hwtn.fsf@xxxxxxxxxxxxxxxxxxxxx>:
Emmanuel Fleury <emmanuel.fleury@xxxxxxxx> writes:
Måns Rullgård wrote:
That is a good example of how *not* to write code. *Never*, ever read
or write a struct directly to/from a file. The compiler is free to
insert padding wherever it likes in a struct.
I have to still see that happen in gcc, do you have a code example?
The WAV header has a number of fixed-size fields, that is true.
However, that does not imply that the variables used to hold these
values inside a program need to have the same size. They need to be
at least as wide, but it doesn't hurt if they are wider if that makes
for more efficient code.
Will create problems too, as then when you write the header back, it may overflow.
(You cannot put a 64 bit value into a 32 bit register).
For exampe in a program that concatenates n wave files, the resulting could easily be > 4GB.
That would require a check and error abort (but you would need that anyways).
The correct way to handle things like a WAV header is to read one
value at a time, convert it to the machine endianness (the WAV header
is always little endiean). Reading the header directly into a struct
can lead to unpleasant surprises if the compiler inserted padding you
didn't expect, and you still need to handle endian conversions.
True.
/* cmp.c a program that demonstates bloat */
#include <stdlib.h>
#include <stdio.h>
#define TRUE 0
#define FALSE 1
int compare(int a, int b)
{
if(a == b) return TRUE;
return FALSE;
}
int main(int argc, char **argv)
{
int a;
if(argc != 3)
{
fprintf(stderr, "Usage: cmp value1 value2\nvalues must be integers\n");
exit(1);
}
a = compare(atoi(argv[1]), atoi(argv[2]) );
if(a == TRUE) fprintf(stderr, "TRUE\n");
else fprintf(stderr, "NOT TRUE\n");
exit(0);
}
# gcc -o cmp cmp.c
# ./cmp 1 2
NOT TRUE
Strange example? No:
NAME
isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit - character
classification routines
SYNOPSIS
#include <ctype.h>
int isalnum(int c);
int isalpha(int c);
int isascii(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);
DESCRIPTION
These functions check whether c, which must have the value of an unsigned char or EOF, falls into a certain character class accord-
-------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ing to the current locale.
RETURN VALUE
The values returned are non-zero if the character c falls into the tested class, and a zero value if not.
______^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
CONFORMING TO
ANSI-C, 4.3BSD. isascii() is a BSD extension and is also an SVID extension. isblank() conforms to ISO C99 7.4.1.3. isblank() is
conforming to IEEE Std 1003.1, 2003, and ISO C99 7.4.1.3.
-----------------------------------------------------------------------------------
I remember Z80 asm, where you test the zero flag after substracting 2 16 bit registers,
takes 3 bytes.
sbc hl,de
C is a fun language.
;-)
.
- Follow-Ups:
- Re: ISO: help porting C code to 64 bit linux platform
- From: jasen
- Re: ISO: help porting C code to 64 bit linux platform
- From: Måns Rullgård
- Re: ISO: help porting C code to 64 bit linux platform
- References:
- ISO: help porting C code to 64 bit linux platform
- From: lvirden
- Re: ISO: help porting C code to 64 bit linux platform
- From: Jan Panteltje
- Re: ISO: help porting C code to 64 bit linux platform
- From: Emmanuel Fleury
- Re: ISO: help porting C code to 64 bit linux platform
- From: Joe Pfeiffer
- Re: ISO: help porting C code to 64 bit linux platform
- From: Måns Rullgård
- Re: ISO: help porting C code to 64 bit linux platform
- From: Jan Panteltje
- Re: ISO: help porting C code to 64 bit linux platform
- From: Måns Rullgård
- Re: ISO: help porting C code to 64 bit linux platform
- From: Emmanuel Fleury
- Re: ISO: help porting C code to 64 bit linux platform
- From: Måns Rullgård
- ISO: help porting C code to 64 bit linux platform
- Prev by Date: Re: C/C++ Developent Environment
- Next by Date: Re: ISO: help porting C code to 64 bit linux platform
- Previous by thread: Re: ISO: help porting C code to 64 bit linux platform
- Next by thread: Re: ISO: help porting C code to 64 bit linux platform
- Index(es):
Relevant Pages
|