Should this work?

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

    Should this work?


    Hi List,
    I'm not sure if this is the appropriate list for this, but perhaps
    someone has got some pointers or ideas...

    The assert fails at some point. However, everything seems to work fine
    once I add a line:

    printf("");

    to my code. I discovered this when trying to print our info-messages
    to help me find the bug. To be a bit more specific, this is the code:

    a->x, a->y, (a->v)->x and (a->v)->y are floats,
    Width = 80, Height = 24 are ints.

    --cut--

    a->x += (a->v)->x;
    a->y += (a->v)->y;

    /* Check if atom is off one of the egdes; if so, continually
    adjust the corresponding coordinate (mirror point along the
    corresponding edge) until it's within bounds, inverting the
    vector each time */

    /**** Help with this line ****/

    printf("");

    /*************** **************/

    /* Check if x coord is off screen; (int) x < 0 || (int) x >= Width */

    while(((int) a->x < 0) || ((int) a->x >= Width)) {

    /* Check if (int) x < 0 */

    if((int) a->x < 0) {
    a->x = fabsf(a->x); /* Mirror along edge */
    (a->v)->x *= -1; /* Invert vector */
    }

    /* Check if (int) x >= Width */

    if((int) a->x >= Width) {
    a->x = 2 * (Width - 1) - a->x; /* Mirror along edge */
    (a->v)->x *= -1; /* Invert vector */
    }
    }

    /* Assert that everything is OK now; This fails at some point */

    assert(((int) a->x >= 0) && ((int) a->x < Width));

    --cut--

    Why do the numbers slip past the test at the head of the while, but
    then trigger the assert that follows? Why does printf(""); change
    all this? Perhaps this points to a problem elsewhere? I don't use any
    threading.

    I hope this mail wasn't to verbose, any pointers and comments appreceated.

    Fabian
  • John L

    #2
    Re: Should this work?


    "Fabian Wauthier" <flw@REMOVEME.c opyleft.de> wrote in message news:xvbvptae3i tr.fsf@beefy.in f.ed.ac.uk...[color=blue]
    >
    > Hi List,
    > I'm not sure if this is the appropriate list for this, but perhaps
    > someone has got some pointers or ideas...
    >
    > The assert fails at some point. However, everything seems to work fine
    > once I add a line:
    >
    > printf("");
    >
    > to my code. I discovered this when trying to print our info-messages
    > to help me find the bug. To be a bit more specific, this is the code:
    >
    > a->x, a->y, (a->v)->x and (a->v)->y are floats,
    > Width = 80, Height = 24 are ints.
    >
    > --cut--
    >
    > a->x += (a->v)->x;
    > a->y += (a->v)->y;
    >
    > /* Check if atom is off one of the egdes; if so, continually
    > adjust the corresponding coordinate (mirror point along the
    > corresponding edge) until it's within bounds, inverting the
    > vector each time */
    >
    > /**** Help with this line ****/
    >
    > printf("");
    >
    > /*************** **************/
    >
    > /* Check if x coord is off screen; (int) x < 0 || (int) x >= Width */
    >
    > while(((int) a->x < 0) || ((int) a->x >= Width)) {
    >
    > /* Check if (int) x < 0 */
    >
    > if((int) a->x < 0) {
    > a->x = fabsf(a->x); /* Mirror along edge */
    > (a->v)->x *= -1; /* Invert vector */
    > }
    >
    > /* Check if (int) x >= Width */
    >
    > if((int) a->x >= Width) {
    > a->x = 2 * (Width - 1) - a->x; /* Mirror along edge */
    > (a->v)->x *= -1; /* Invert vector */
    > }
    > }
    >
    > /* Assert that everything is OK now; This fails at some point */
    >
    > assert(((int) a->x >= 0) && ((int) a->x < Width));
    >
    > --cut--
    >
    > Why do the numbers slip past the test at the head of the while, but
    > then trigger the assert that follows? Why does printf(""); change
    > all this? Perhaps this points to a problem elsewhere?[/color]

    This often means you are, somewhere in the code, stamping on
    some random piece of memory, possibly due to an uninitialised
    pointer, or going past the end of an array. The consequences, if
    any, of this depend on what is in this particular bit of memory,
    and this can be changed by adding printf's to the code.

    --
    John.


    Comment

    • Sam Dennis

      #3
      Re: Should this work?

      Fabian Wauthier wrote:[color=blue]
      > a->x += (a->v)->x;
      > a->y += (a->v)->y;
      > printf("");[/color]
      /* makes the code `work', for some reason */

      There's nothing to indicate that this should have any effect, so you
      probably invoke undefined behaviour somewhere, such as by writing to
      the memory pointed to by an unitialised or freed memory. Has a been
      initialised to point to valid memory?
      [color=blue]
      > while(((int) a->x < 0) || ((int) a->x >= Width)) {[/color]

      You might like to cut down on the parentheses and casts; they mainly
      serve to hide syntax errors and to reduce readability in your code.
      [color=blue]
      > a->x = 2 * (Width - 1) - a->x; /* Mirror along edge */[/color]

      Are you sure that this expresses your intent? It seems to contradict
      the associated comment.

      Width - 1 - a->x seems more likely.
      [color=blue]
      > assert(((int) a->x >= 0) && ((int) a->x < Width));[/color]

      The only way that I can see this failing is by invoking UB again; it
      seems very likely that the problem is, indeed, with the `a' pointer.

      --
      ++acr@,ka"

      Comment

      Working...