On portable C programming

Jerry Feldman gaf at blu.org
Fri Jan 9 07:47:35 EST 2009


On 01/08/2009 08:20 AM, Jim Kuzdrall wrote:
> On Thursday 08 January 2009 00:06, Ben Scott wrote:
>   
>> On Wed, Jan 7, 2009 at 11:31 PM,  <VirginSnow at vfemail.net> wrote:
>>     
>>> So what's the recommended way to do this?
>>>       
>>   I dunno that there really is any really good way.
>>     
>
>     Once created, the structs go into a "universal" struct library which 
> can be used various ways, depending on your needs.  Your programs must 
> explicitely access all structs via a pointer array, constructed each 
> time the program loads.  The program which uses the struct first 
> translates the universal struct to its native format.  Then it replaces 
> the pointer the compiler put in the array with the location of the 
> translated struct.
>
>   
There are actually systems that do this such as ASN 1 == *X.680.  
Essentially, the way it works is that everything transmitted is in TLD 
(type, length data) format. An integer, for instance would have a type 
code (one for 16-bit, 1 for 32-bit, 1 for 64-bit, etc), followed by a 1 
byte length, followed by several individual bytes representing the 
integer. Lengths longer than 127 would be represented in a multi-byte 
length. All data would be sent in big endian format. Endian is another 
problem with standard data. Integers and Doubles are in different 
formats on big endian machines like the Sun Sparc, and little endian in 
Intel x86 and Digital Alpha. Some chips, like the Intel IA64 and the 
later Alpha chips could be set to be either little or big endian.  Using 
any portable format for local use is costly from a performance 
standpoint, but absolutely necessary for interchange between computers 
of unlike types.  Additionally, the implementers of the C and C++ 
standards decided to use objects like size_t which may be 32-bits on a 
32-bit architecture, 64-bits on a 64-bit architecture (LP64) where longs 
are 64-bit, and 32-bits where longs are 32-bits such as on Windows 64. 
Java certainly was written with portability in mind. Data base systems 
took all this into account many years ago. I once sat on the ANSI 
database committee and our biggest discussions had to do with how to 
describe numbering formats which are not only different on different 
architectures, but also on different programming languages (C, FORTRAN. 
COBOL, et. al).  So, portability is not just structs and alignments, but 
also endian and as mentioned numbering types if data is to be sent to a 
receiver with another programming language. *

-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 251 bytes
Desc: OpenPGP digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090109/873f61d3/attachment.bin 


More information about the gnhlug-discuss mailing list