Should this work?

F

Fabian Wauthier

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
 
J

John L

Fabian Wauthier said:
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?

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.
 
S

Sam Dennis

Fabian said:
a->x += (a->v)->x;
a->y += (a->v)->y;
printf("");
/* 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?
while(((int) a->x < 0) || ((int) a->x >= Width)) {

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.
a->x = 2 * (Width - 1) - a->x; /* Mirror along edge */

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

Width - 1 - a->x seems more likely.
assert(((int) a->x >= 0) && ((int) a->x < Width));

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top