A problem initializing a static char array

Discussion in 'C Programming' started by Jai Prabhu, May 18, 2008.

  1. Jai Prabhu

    Jai Prabhu Guest

    Hi All,

    Consider the following piece of code:

    void func (void)
    {
    static unsigned char arr[3] = "\x00\xAA\xBB";

    fprintf (stderr, "0x%x\n", arr[0]);
    fprintf (stderr, "0x%x\n", arr[1]);
    fprintf (stderr, "0x%x\n", arr[2]);
    }

    On a x86 machine, with the gcc (v3.3.5) compiler, the above piece of
    code (on a OpenBSD 3.8 machine) gives the following result:
    [...]
    0x0
    0xaa
    0xbb
    [...]

    However, with the VxWorks 5.4 C compiler (which I believe is Wind
    River's) variant of gcc, the above piece of code gives the following
    result (run on a x86 machine running VxWorks 5.4):

    [...]
    0x0
    0x0
    0x0
    [...]

    If I not use the static qualifier initializing the array, then VxWorks
    host too gives the same result as OpenBSD host. Can someone explain
    the discrepancy and why the VxWorks compiler would be initializing
    all the elements of the array to 0x00?

    TIA
    Jai
    Jai Prabhu, May 18, 2008
    #1
    1. Advertising

  2. Jai Prabhu <> writes:
    > Consider the following piece of code:
    >
    > void func (void)
    > {
    > static unsigned char arr[3] = "\x00\xAA\xBB";
    >
    > fprintf (stderr, "0x%x\n", arr[0]);
    > fprintf (stderr, "0x%x\n", arr[1]);
    > fprintf (stderr, "0x%x\n", arr[2]);
    > }
    >
    > On a x86 machine, with the gcc (v3.3.5) compiler, the above piece of
    > code (on a OpenBSD 3.8 machine) gives the following result:
    > [...]
    > 0x0
    > 0xaa
    > 0xbb
    > [...]
    >
    > However, with the VxWorks 5.4 C compiler (which I believe is Wind
    > River's) variant of gcc, the above piece of code gives the following
    > result (run on a x86 machine running VxWorks 5.4):
    >
    > [...]
    > 0x0
    > 0x0
    > 0x0
    > [...]
    >
    > If I not use the static qualifier initializing the array, then VxWorks
    > host too gives the same result as OpenBSD host. Can someone explain
    > the discrepancy and why the VxWorks compiler would be initializing
    > all the elements of the array to 0x00?


    I can't think of any really plausible reason. It *looks* like a
    compiler bug, but it's impossible to be certain of that without more
    context.

    Did you remember the required "#include <stdio.h>"?

    As I recall, VxWorks is an embedded system, which means it's likely
    that you're using a freestanding implementation. Conforming
    freestanding implementations aren't required to provide most of the
    standard library, including stdio; if they do provide portions of it,
    it doesn't necesssarily behave as the standard describes. It's
    unlikely that a variation in the behavior of fprintf would cause this
    kind of problem, but it's hard to be sure.

    You haven't shown us a complete program, or even a call to func(). I
    can't think of anything outside the definition of func() that would
    cause the symptom you're seeing (other than a malicious macro
    definition for "static"), but it's hard to be sure.

    If it's a system-specific issue, try asking in comp.os.vxworks.

    [Hey, aioe.org is back!]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 18, 2008
    #2
    1. Advertising

  3. Jai Prabhu wrote:
    > Hi All,
    >
    > Consider the following piece of code:
    >
    > void func (void)
    > {
    > static unsigned char arr[3] = "\x00\xAA\xBB";


    Note that "\xAA" and '\xAA' need not yield 0xAA on
    non vanilla 2c implementations.

    > fprintf (stderr, "0x%x\n", arr[0]);
    > fprintf (stderr, "0x%x\n", arr[1]);
    > fprintf (stderr, "0x%x\n", arr[2]);


    BTW, "0x%x" can be simplified to "%#x"

    It wouldn't hurt to force the unsigned char's to unsigned int,
    as required by %x.

    > }
    >

    <snip>
    > ... with the VxWorks 5.4 C compiler (which I believe is Wind
    > River's) variant of gcc, the above piece of code gives the
    > following result (run on a x86 machine running VxWorks 5.4):
    >
    > [...]
    > 0x0
    > 0x0
    > 0x0
    > [...]
    >
    > If I not use the static qualifier initializing the array, then VxWorks
    > host too gives the same result as OpenBSD host. Can someone
    > explain the discrepancy and why the VxWorks compiler would
    > be initializing all the elements of the array to 0x00?


    You should confirm that is actually the case, e.g. do a memcmp
    with a zero byte initialised object. Otherwise, post a complete
    compilable source that exhibits the problem.

    --
    Peter
    Peter Nilsson, May 19, 2008
    #3
  4. On Tue, 20 May 2008 00:07:24 +0200, Pietro Cerutti <"gahr<S P A M>gahr
    wrote:
    > Peter Nilsson wrote:
    >> Jai Prabhu wrote:
    >>> fprintf (stderr, "0x%x\n", arr[0]);
    >>> fprintf (stderr, "0x%x\n", arr[1]);
    >>> fprintf (stderr, "0x%x\n", arr[2]);

    >>
    >> BTW, "0x%x" can be simplified to "%#x"
    >>
    >> It wouldn't hurt to force the unsigned char's to unsigned int, as
    >> required by %x.

    >
    > Nor it would bring anything, no?
    > Aren't unsigned chars integer-promoted to unsigned int?


    They are usually promoted to signed int. They are only promoted to
    unsigned int when UCHAR_MAX > INT_MAX (when a conversion to signed int
    might change the value), which almost certainly is not the case on your
    system.
    Harald van Dijk, May 20, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    996
    Ian Collins
    May 9, 2006
  2. nk
    Replies:
    7
    Views:
    407
    Andrew Poelstra
    Jul 31, 2006
  3. Replies:
    3
    Views:
    710
  4. davidb
    Replies:
    0
    Views:
    746
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,525
    Default User
    Sep 1, 2006
Loading...

Share This Page