473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C Program Portability

Anyone can list the difference between Solaris SunOS/Cygwin/Redhat?

I know SunOS has a byte ordering problem.

What else? Are there any website to list these difference?
What are the constructs to be avoided to write portable C program?
It maybe OT, coz' it is about system specific details, but because it is
multiple platform, and it is about C, I think the closest newsgroup is
c.l.c.
Maybe this group should discuss issue of wider range, only C standard, or
standard C, is too narrow of any discussion, almost all useful discussion in
C is OT. If it is simply about standard, why not just refer people to the
webpage listing C standard and close this group?
Nov 14 '05 #1
6 1281
Andrew Au (Newsgroup) wrote:
Anyone can list the difference between Solaris SunOS/Cygwin/Redhat?

I know SunOS has a byte ordering problem.

What else? Are there any website to list these difference?
What are the constructs to be avoided to write portable C program?
If you write portable code, no problem.
It maybe OT, coz' it is about system specific details, but because it is
It is.
multiple platform, and it is about C, I think the closest newsgroup is
c.l.c.
If you write portable code, no problem.
Maybe this group should discuss issue of wider range, only C standard, or
standard C, is too narrow of any discussion, almost all useful discussion in
C is OT. If it is simply about standard, why not just refer people to the
webpage listing C standard and close this group?

It is not about the standard, it is about the ISO standard C language --
a pretty rich topic in and of itself.

There are system specific newsgroups for system specific issues.

--ag

[BTW -- see Sun's website for Solaris/Linux issues]

--
Artie Gold -- Austin, Texas

20050120->44
Nov 14 '05 #2

On Sun, 19 Sep 2004, Andrew Au (Newsgroup) wrote:

Anyone can list the difference between Solaris SunOS/Cygwin/Redhat?
Operating system, for one thing.
I know SunOS has a byte ordering problem.
ITYM /Intel/ has a byte ordering problem. :-P
What else? Are there any website to list these difference?
What are the constructs to be avoided to write portable C program?
Avoid non-portable constructs. That is, avoid those constructs
with implementation-defined, unspecified, or undefined behavior.
Some examples include

int i;
fwrite(&i, sizeof i, 1, fp); /* bad! */

unsigned char buf[sizeof (int)];
buf[0] = i>>0;
buf[1] = i>>8;
buf[2] = i>>16;
buf[3] = i>>24;
fwrite(buf, 1, sizeof buf, fp); /* good! */

int i = 65536; /* bad! */
long i = 65536; /* good! */

FILE *fp = fopen("foo", "rb");
if (getc(fp) == '\n') /* bad! */
if (getc(fp) == 0x0A) /* good! */

FILE *fp = fopen("foo", "r");
if (getc(fp) == '\n') /* good! */

return add(get_term(), get_term()); /* bad! */

term t = get_term();
return add(t, get_term()); /* good! */

It maybe OT, coz' it is about system specific details, but because it is
multiple platform, and it is about C, I think the closest newsgroup is
c.l.c.
Yes, probably. But if you want real answers, you're going to have to
ask real questions. The generic "I'm a newbie. What do I do?" kind of
question doesn't usually get good answers here (and my response is no
exception). You might try comp.programming for your broader portability
concerns (as in, "what weird things will I encounter in the field?"),
and/or alt.comp.lang.learn.c-c++ for your pedagogically-oriented answers.
For starters, read the FAQs of both clc and acllcc++. They will help
you immensely.
Maybe this group should discuss issue of wider range, only C standard, or
standard C, is too narrow of any discussion, almost all useful discussion in
C is OT. If it is simply about standard, why not just refer people to the
webpage listing C standard and close this group?


Because the Standard is so highly arcane (in some sections) that there's
a whole group (comp.std.c) devoted simply to interpreting and revising
the Standard document! This group is not about the Standard document.
It's about using the real-world language defined by the Standard. And
that's plenty for one group; we don't need to try to cope with all your
Unix and MFC and OpenGL and Java questions into the bargain!

More succinctly: Because the Standard isn't available on any
publicly-accessible webpage. The nearest thing is N869 (Google it);
the Standard itself costs $18 from ANSI and other national bodies.

HTH,
-Arthur
Nov 14 '05 #3
On Tue, 21 Sep 2004 20:35:52 -0400 (EDT)
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:

On Sun, 19 Sep 2004, Andrew Au (Newsgroup) wrote:

Anyone can list the difference between Solaris SunOS/Cygwin/Redhat?
Operating system, for one thing.
I know SunOS has a byte ordering problem.


ITYM /Intel/ has a byte ordering problem. :-P
What else? Are there any website to list these difference?
What are the constructs to be avoided to write portable C program?


Avoid non-portable constructs. That is, avoid those constructs
with implementation-defined, unspecified, or undefined behavior.
Some examples include

int i;
fwrite(&i, sizeof i, 1, fp); /* bad! */

unsigned char buf[sizeof (int)];
buf[0] = i>>0;
buf[1] = i>>8;


Bad. What if CHAR_BIT!=8 ?
buf[2] = i>>16;
Very bad, what if sizeof int <= 2 ?
buf[3] = i>>24;
fwrite(buf, 1, sizeof buf, fp); /* good! */


<snip>

OK, so on most hosted systems that the OP is likely to use CHAR_BIT==8
(I know some real systems have other values, but those are rather less
common) but for portable binary formats you do have to be very careful
and allow for what mechanism will be used for transferring the data and
if CHAR_BIT>8 how this will affect the layout of the bits in a shared
resource such as a binary file. Text formats are generally easier to
deal with, although you still have to transfer them in a way that
ensures that they are converted to the native text format.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4
"Andrew Au \(Newsgroup\)" <eg****@hotmail.com> wrote:
# Anyone can list the difference between Solaris SunOS/Cygwin/Redhat?
#
# I know SunOS has a byte ordering problem.

It's not a problem, it's a feature.

Byte order issues occur when you view a multibyte value, like a binary int, as both
the multibyte value and as a byte string. Most I/O mediums convert to a byte string
and then back to an internal value, so this also shows up if you write a binary value
on one machine and read it back on another.

# What else? Are there any website to list these difference?
# What are the constructs to be avoided to write portable C program?

See if they all claim to obey a particular standard like ANSI C or Posix or SVID
or CIE LAB. If so, you can code to that standard.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
Nov 14 '05 #5

On Wed, 22 Sep 2004, Flash Gordon wrote:

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:

Avoid non-portable constructs. That is, avoid those constructs
with implementation-defined, unspecified, or undefined behavior.
Some examples include

int i;
fwrite(&i, sizeof i, 1, fp); /* bad! */

unsigned char buf[sizeof (int)];
buf[0] = i>>0;
buf[1] = i>>8;


Bad. What if CHAR_BIT!=8 ?


Then we waste a few bits. I have absolutely no experience with
porting data files between systems with different byte widths, but I
imagine it might be easier and more useful to convert 9-bit bytes to
8-bit bytes by truncation, than to copy the exact bit pattern. In
other words, in cases where it matters, this is a feature, not a bug.
buf[2] = i>>16;


Very bad, what if sizeof int <= 2 ?


Hmm, yes, that is UB, isn't it? Better make that something like

if (INT_MAX > 32767) {
buf[2] = i >> 16;
if ( [...???...] )
buf[3] = i >> 24;
else buf[3] = 0;
}
else buf[2] = buf[3] = 0;

....except that I can't think what to put in the [...???...] condition
that would be strictly portable. Comparison to 8388607 isn't right.

Of course, if I had taken my own advice and used 'long int' instead
of 'int' in the first place (and 'unsigned long int' for any values
that would need writing out to a file), then we wouldn't be having
this conversation. :)

-Arthur
Nov 14 '05 #6
On Thu, 23 Sep 2004 00:39:48 -0400 (EDT)
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
On Wed, 22 Sep 2004, Flash Gordon wrote:

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:

Avoid non-portable constructs. That is, avoid those constructs
with implementation-defined, unspecified, or undefined behavior.
Some examples include

int i;
fwrite(&i, sizeof i, 1, fp); /* bad! */

unsigned char buf[sizeof (int)];
buf[0] = i>>0;
buf[1] = i>>8;


Bad. What if CHAR_BIT!=8 ?


Then we waste a few bits. I have absolutely no experience with
porting data files between systems with different byte widths, but I
imagine it might be easier and more useful to convert 9-bit bytes to
8-bit bytes by truncation, than to copy the exact bit pattern. In
other words, in cases where it matters, this is a feature, not a bug.


I was thinking more of a 16 bit byte. Imagine this, you write your file
out on the 16 bit byte machine then transfer it to an 8 bit byte
machine. Now, since this is a binary file, any transfer is likely to
convert every 16 bit byte in to *two* 8 bit bytes, since to do otherwise
looses information. Then you have problems reading it!

Hence the comment I should have made (but may have forgotten to make)
that any code writing data as a binary file needs to be adapted for the
range of systems being used, and that the simplest way of dealing with
some possible systems is with different versions of the code.
buf[2] = i>>16;


Very bad, what if sizeof int <= 2 ?


Hmm, yes, that is UB, isn't it? Better make that something like

if (INT_MAX > 32767) {
buf[2] = i >> 16;
if ( [...???...] )
buf[3] = i >> 24;
else buf[3] = 0;
}
else buf[2] = buf[3] = 0;

...except that I can't think what to put in the [...???...] condition
that would be strictly portable. Comparison to 8388607 isn't right.

Of course, if I had taken my own advice and used 'long int' instead
of 'int' in the first place (and 'unsigned long int' for any values
that would need writing out to a file), then we wouldn't be having
this conversation. :)


It's more that if you are dealing with reading, processing, writing a
binary format (say, doing some image processing on a greyscale image
with 16 bits per pixel, you might want to use different types on
different systems. If dealing with something less bulky, for instance
exporting a balanced ledger (financial data) from one system and
importing it to another (something I am involved in) you will probably
want to use a text format, since that can easily be produced and
easily be interpreted and translations between text formats can
generally be handled by the tool (FTP, for instance, which definitely
does line termination translation when told to transfer in text mode)
used to transfer the file.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Tim Tyler | last post by:
I just ported one of the apps I wrote using PHP 5 under Windows to PHP 4 under unix. I knew I wanted to deploy in this environment - and wrote my code in anticipation of running in the other...
6
by: Eric Entressangle | last post by:
Hi all, is there any trouble setting an C++ static class method as callback function for a C program or library ? Thanks
7
by: fabio de francesco | last post by:
Hi, I'm not a professional programmer, but I've been writing C/C++ and Ada programs for a few years on GNU/Linux without ever concerning on standards and portability to other OSs. I've always...
15
by: Coca | last post by:
HI, all How can I convert the C6.0 program of OS/2 platform TO windows2000/xp platform. The program occurs a lot of errors in Windows 2000/xp when compiled. And I hope a quick way to solve...
93
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the...
20
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
73
by: Rajeet Dalawal | last post by:
Good day group. I was asked in an interview to explain the behavior of this program. void main() { char *s = "abc"; int *i = (int *) s; printf("%x", *i); }
18
by: jacob navia | last post by:
One of the holy cows here is this "portability" stuff. In practice, portability means: 1) Use the least common denominator of all the supported systems. 2) Between usability / good user...
46
by: Kenny O'Clock | last post by:
This came up in a job interview, what is the output of the program below? I tried to compile and run it myself, but my compiler (lcc-win32) aborts with this errors.... Warning test2.c: 3 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.