Non Portable C

G

Gautam

Can any one tell me what are the types in C which are non portable/which make C
non-portable

is it

a)structures, or
b)unions, or
3)bit-fields, or

are all of them , i am confused

thanx
 
M

Mark McIntyre

Can any one tell me what are the types in C which are non portable/which make C
non-portable
a)structures, or
b)unions, or
3)bit-fields, or

None of these is nonportable, unless you start treating them badly. You
also need to be careful when writing them to file as binary data, and then
reading back on a different platform.

Structures can have padding. The amount and size of that padding is
system-dependent, so if you rely on the padding being X bytes wide then you
will have problems. If you save the struct as binary data, the padding
will have been saved with the data, so if you read it on a different
system, it will cause problems.

Objects can have different sizes on different platforms. If you rely on an
int being (say) 4 bytes, then you will have problems. If, in a union, you
rely on an int being the same size as a float, then problems. If you save
the data to a binary file, same problem as for structs.
 
M

Malcolm

Gautam said:
Can any one tell me what are the types in C which are non
portable/which make C non-portable

is it

a)structures, or
b)unions, or
3)bit-fields, or

are all of them , i am confused
C isn't binary compatible. For instance float x = 0.1f will produce
different bit patterns on machines with different floating-point
architectures, and since 0.1 cannot be represented exactly in most formsts
may be slightly under on some machines and slightly over on others.
Similarly int, longs, and even chars may have different numbers of bits, and
might not even have twos complement representation for negative numbers.
When you get to structures, unions and bitfields, not only may the
individual members differ, but the padding and layout in memory may differ.
This is because some machines like values to be aligned on word boundaries.

For practical purposes, what you need to know is that fwrite() ing a
structure to disk will produce an binary image that can be fread() only by a
program produced by the same compiler (the same goes of course for sending
bytes down a socket). You also need to know that C programs won't
necessarily produce the same bit-for-bit result, even though they will still
be correct, if you use floating point. If you use pointers incorrectly as
scalar values, or you exceed the minimum size of an integer type, you will
also get different results on different platforms.
 
J

Joe Wright

Gautam said:
Can any one tell me what are the types in C which are non portable/which make C
non-portable

is it

a)structures, or
b)unions, or
3)bit-fields, or

are all of them , i am confused

In its normal sense, portability is the ease with which you might
'port' a C program from one platform to another. The main purpose of
the C Standard is to define rules for compiler writers and
programmers so that this 'porting' of C programs more easily and
correctly.

There is nothing in the C Standard concerning the compatibility of
internal data structures or external file formats among platforms.
Nothing! You are on your own in this area.

Luckily we have ASCII (American Standard Code for Information
Interchange) text files common among most platforms. There is also
an ISO standard mirroring ASCII. EBCDIC (Extended Binary Coded
Decimal Interchange Code) is IBM's way of being one-up on ANSI in
the day. But I digress.

The various C implementations agree on what text representation of
values mean: '12345' a positive integer, '-23456' a negative one.
'1.25' is a floating point value. Virtually all C systems can read
these representations from a text file and come up with the same
internal values, endianness, width, etc. notwithstanding.

The text file is the answer. First, you can read it. If the
recipient of your data file doesn't already know its format, you can
explain it to her in the file itself and even include the C code to
interpret it.

Text rules! XML anyone?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top