structure member changing after a condition test

Discussion in 'C Programming' started by B. Wood, Aug 27, 2003.

  1. B. Wood

    B. Wood Guest

    I have written a simple program the has a structure with two members.

    There are two strange things going on.

    1. When on of the structure members is tested in a condition, the
    value of the second member seems to change.

    2. The order of the condition test seems to have an effect on if the
    one structure member changes.

    The program follows:
    -------------------------------------------------------------------------------
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct LongStruct
    {
    unsigned long lo;
    unsigned long hi;
    }
    LongStruct;

    int
    main(int argc, char * argv[])
    {
    LongStruct s;

    s.lo = 0x00000001;
    s.hi = 0x00000000;

    s.lo += 0xFFFFFFFFUL;

    if (s.lo < 0xFFFFFFFFUL)
    {
    s.hi++;
    }

    printf("\ns.lo tested before s.hi\n");
    printf("\n#1 before s.lo test s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);

    if (s.lo == 0x00000000)
    {
    printf("\n#2 s.lo tested == 0 s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);
    }
    else
    {
    printf("\n#2 s.lo tested != 0 s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);
    }

    printf("\n#1 before s.hi test s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);

    if (s.hi == 0x00000001)
    {
    printf("\n#3 s.hi tested == 1 s.lo = %08lX s.hi = %08lX\n\n",
    s.lo, s.hi);
    }
    else
    {
    printf("\n#3 s.hi tested != 1 s.lo = %08lX s.hi = %08lX\n\n",
    s.lo, s.hi);
    }

    s.lo = 0x00000001;
    s.hi = 0x00000000;

    s.lo += 0xFFFFFFFFUL;

    if (s.lo < 0xFFFFFFFFUL)
    {
    s.hi++;
    }

    printf("\ns.hi tested before s.lo\n");
    printf("\n#1 before s.hi test s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);

    if (s.hi == 0x00000001)
    {
    printf("\n#2 s.h1 tested == 1 s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);
    }
    else
    {
    printf("\n#2 s.hi tested != 1 s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);
    }

    printf("\n#1 before s.lo test s.lo = %08lX s.hi = %08lX\n",
    s.lo, s.hi);

    if (s.lo == 0x00000000)
    {
    printf("\n#3 s.lo tested == 0 s.lo = %08lX s.hi = %08lX\n\n",
    s.lo, s.hi);
    }
    else
    {
    printf("\n#3 s.lo tested != 0 s.lo = %08lX s.hi = %08lX\n\n",
    s.lo, s.hi);
    }

    return (0);

    }
    --------------------------------------------------------------------------------

    The output of the program is as follows:

    s.lo tested before s.hi

    #1 before s.lo test s.lo = 00000000 s.hi = 00000001

    #2 s.lo tested == 0 s.lo = 00000000 s.hi = 00000000

    #1 before s.hi test s.lo = 00000000 s.hi = 00000001

    #3 s.hi tested == 1 s.lo = 00000000 s.hi = 00000001


    s.hi tested before s.lo

    #1 before s.hi test s.lo = 00000000 s.hi = 00000001

    #2 s.h1 tested == 1 s.lo = 00000000 s.hi = 00000001

    #1 before s.lo test s.lo = 00000000 s.hi = 00000001

    #3 s.lo tested == 0 s.lo = 00000000 s.hi = 00000001

    --------------------------------------------------------------------------------

    This program was compiled on a Linux machine.
    The version of Linux is 2.4.7-10.
    The compiler used is gcc version 2.96.

    This same program builds and runs fine on a Windows machine running
    Windows 2K and also a Solaris machine running SunOS 5.8.

    The size of unsigned longs, unsigned ints and ints is the same on all
    three platforms.

    Does anyone have any ideas on what could be happening ?
    B. Wood, Aug 27, 2003
    #1
    1. Advertising

  2. B. Wood

    Greg P. Guest

    "B. Wood" <> wrote in message
    news:...
    <snip>
    | s.lo tested before s.hi
    |
    | #1 before s.lo test s.lo = 00000000 s.hi = 00000001
    |
    | #2 s.lo tested == 0 s.lo = 00000000 s.hi = 00000000
    <snip>

    Is the line above what you are talking about? I tried executing your code on
    various platforms and could not reproduce the problem. What is your Linux
    running on? You may have stack frame problems (some kind of internal wild
    pointer flying around). Try tagging the struct members with volatile and see
    if they still change and post back.

    Sorry that I can't help further =(
    Greg P., Aug 27, 2003
    #2
    1. Advertising

  3. B. Wood

    B. Wood Guest

    "Greg P." <> wrote in message news:<avS2b.4711$>...
    > "B. Wood" <> wrote in message
    > news:...
    > <snip>
    > | s.lo tested before s.hi
    > |
    > | #1 before s.lo test s.lo = 00000000 s.hi = 00000001
    > |
    > | #2 s.lo tested == 0 s.lo = 00000000 s.hi = 00000000
    > <snip>
    >
    > Is the line above what you are talking about? I tried executing your code on
    > various platforms and could not reproduce the problem. What is your Linux
    > running on? You may have stack frame problems (some kind of internal wild
    > pointer flying around). Try tagging the struct members with volatile and see
    > if they still change and post back.
    >
    > Sorry that I can't help further =(



    Yes, that is the line. After the condition test between printing #1
    and #2, s.hi seems to be changed to 0.

    Weirder is after the printf that outputs #2 above with s.hi =
    00000000, the very next line of code executed is the printf that
    outputs the following line:

    #1 before s.hi test s.lo = 00000000 s.hi = 00000001

    Now s.hi is suddenly back to 00000001.

    Here is some of the system information

    model name : Pentium II
    cpu MHz : 267.280

    I added volatile to the member type and that worked. Thanks.
    I think this might be a compiler bug. The compiler must be trying to
    do some
    kind of optimization that the volatile qualifier prevents.

    Thanks.
    B. Wood, Aug 27, 2003
    #3
    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. -
    Replies:
    12
    Views:
    692
    Remon van Vliet
    Jun 15, 2005
  2. Replies:
    1
    Views:
    349
    Kevin Spencer
    Aug 7, 2006
  3. August Karlstrom

    Re: while( ) and test condition

    August Karlstrom, Sep 4, 2008, in forum: C Programming
    Replies:
    8
    Views:
    265
    August Karlstrom
    Sep 5, 2008
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    694
    Skybuck Flying
    Oct 4, 2009
  5. Bill W.
    Replies:
    13
    Views:
    293
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page