|
-
February 10th, 2013, 10:37 AM
#8
Re: Completely stumped by stack smash.
 Originally Posted by Comintern
It crashes every time it runs. Looping around race() doesn't cause it to crash any earlier for me either -- it's always when main exits.
To add to what superbonzo discovered, why are you writing code like this anyway?
Code:
void moveTortoise(int *position) {
int move = 0;
move = 1 + rand() % 10;
move <= 5 ? *position += 3 : (move <= 7 ? *position -= 6 : *position++);
*position = *position > 0 ? (*position > FINISH ? FINISH : *position) : 0;
}
void moveHare(int *position) {
int move = 1 + rand() % 10;
move == 1 ? *position -= 12 : (move <= 3 ? *position -= 2 : (move <= 6 ? *position++ : (move <= 8 ? *position += 9 : 0)));
*position = *position > 0 ? (*position > FINISH ? FINISH : *position) : 0;
}
What's wrong with writing this in a clear, unobfuscated manner, so that it is understood what is being done? You aren't saving any time writing things on one line like that, and to add to the confusion, throwing in the ternary operator into the whole mix. It doesn't cost anything to make things clear by writing 3 or 4 if-then statements.
I don't know what your goal was to write code this way, but a lot of people who do write like this believe they can outsmart the compiler by believing "one line means speed". Instead, as in your case, it did nothing except introduce bugs.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 10th, 2013 at 10:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|