473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Nov 14 '05 #1
2 1053

"Fabian Wauthier" <fl*@REMOVEME.copyleft.de> wrote in message news:xv**************@beefy.inf.ed.ac.uk...

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.

--
John.
Nov 14 '05 #2
Fabian Wauthier wrote:
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.

--
++acr@,ka"
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
1
by: russ | last post by:
Been looking at some code..... #include <vector> #include <iostream> #include <algorithm> template <typename T> struct A { struct B {
4
by: RC | last post by:
I just got my first Access project that I am getting paid for. I have done other Access work for non-profits that I did not get paid for. I definitely have the work but we did not settle the...
6
by: Terry Bell | last post by:
We've had a very large A97 app running fine for the last seven years. I've just converted to SQL Server backend, which is being tested, but meanwhile the JET based version, running under terminal...
13
by: amanayin | last post by:
The program is simple and i know i should use switch instead of if but i done it this way for a reason. But i am not sure if it should work when i was compiling it i was getting warning messages...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
5
by: Andy B | last post by:
I was just wondering, when you create dataContext methods, should you put business logic there to try and minimize pushing data through 2-3 layers of code? or should the business logic still go in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.