Non Portable C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gautam

    #1

    Non Portable C

    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
  • Malcolm

    #2
    Re: Non Portable C


    "Gautam" <gautam92in@yah oo.com> wrote in message[color=blue]
    > 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
    >[/color]
    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.


    Comment

    • Mark McIntyre

      #3
      Re: Non Portable C

      On 9 May 2004 00:07:03 -0700, in comp.lang.c , gautam92in@yaho o.com
      (Gautam) wrote:
      [color=blue]
      >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[/color]

      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.


      --
      Mark McIntyre
      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
      CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


      ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
      http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
      ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

      Comment

      • Joe Wright

        #4
        Re: Non Portable C

        Gautam wrote:[color=blue]
        > 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
        >[/color]

        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?
        --
        Joe Wright mailto:joewwrig ht@comcast.net
        "Everything should be made as simple as possible, but not simpler."
        --- Albert Einstein ---

        Comment

        Working...