Faster for() loops?

T

toby

Keith said:
I suspect, based on his "then they will get what they deserve!", that
he really was referring to counting down a pointer.


It's more than "not guaranteed by the standard". Repeatedly
decrementing a pointer will invoke undefined behavior as soon as the
location it points to is no longer within the original object. It may
happen to "work" on many systems; ...

Which is all I intended to say. The code snippet made no real sense as
a pointer. I think the poster may have meant an integral type.
Incidentally, there's no requirement for p to be a multiple of
sizeof(*p) (unless sizeof(*p)==1, of course). ...

Agreeed there is no 'requirement', but as I said, the loop will not
actually terminate at NULL (on implementations where it happens to work
at all -- i.e. most of the ones we know) unless that condition is met.
 
D

David Brown

Robert said:
It amazes me how some people can claim to speak for the whole group
with no documentation at all. I, for one, appreciate top-posting when
it is appropriate. At least I won't claim to speak for a whole group
who did not elect me to represent them.


-Robert Scott
Ypsilanti, Michigan

I can't claim to speak for everyone in the group, obviously. Every
newsgroup post has an implicit "IMHO" attached. But I think it's a fair
summary of the opinions of the group, or at least of those who post
regularly. And very few people object to top-posting when it is
appropriate - it is just the definition of "appropriate" that they
disagree on...

Anyway, the key point with posting etiquette is to be (reasonably)
polite and informative, and to remember it's a conversation between real
people. If, as is normally the case, top-posting makes it unnecessarily
harder for people to follow the sense of a post, then it is impolite and
uninformative, just like posting in SMS or all caps. If top-posting
makes a reply easier to read, then it is appropriate (although usually
snipping would be far better).
 
P

pete

Jim said:
<snip>

I'd agree the TRUE and FALSE instance are pedantic, but THIS form

if ( boolean_flag == RlyON )

can be safer and simpler, as any downstream logic inversion is
handled transparently.
Here, you have a common define setting RlyON is active Hi or Lo,
but the code is always clearer. ( and that may change with
PCB revision, for example )

Also note that not all compilers treat these the same !!

if ( boolean_flag == TRUE )
if ( boolean_flag )

I prefer to compare Boolean values against zero or FALSE.

if (boolean_flag != 0)

means just exactly the same thing as

if ( boolean_flag )

and should be easy enough to read.
 
P

pete

pete said:
I prefer to compare Boolean values against zero or FALSE.

if (boolean_flag != 0)

Actually, with values of a boolean nature,
I prefer to do it Steve's way:

#include <ctype.h>

int a_toi(const char *nptr)
{
int n;

n = 0;
while (isspace(*nptr)) {
++nptr;
}
if (*nptr != '-') {
if (*nptr == '+') {
++nptr;
}
while (isdigit(*nptr)) {
n = 10 * n - '0' + *nptr++;
}
} else {
++nptr;
while (isdigit(*nptr)) {
n = 10 * n + '0' - *nptr++;
}
}
return n;
}
 
K

Keith Thompson

Jim Granville said:
<snip>

I'd agree the TRUE and FALSE instance are pedantic, but THIS form

if ( boolean_flag == RlyON )

can be safer and simpler, as any downstream logic inversion is
handled transparently.
Here, you have a common define setting RlyON is active Hi or Lo,
but the code is always clearer. ( and that may change with
PCB revision, for example )

I'll ignore, for now, the built-in type _Bool and the standard header
<stdbool.h> introduced in C99.

A value used as a condition in C is treated as false if it's equal to
zero, true if it's non-zero. It's not 0 vs. 1, it's 0 vs. anything
other than 0.

If your variable "boolean_flag" is something other than zero-is-false,
non-zero-is-true, it shouldn't be called "boolean_flag".

Assuming boolean_flag is a proper condition, "boolean_flag == TRUE"
isn't just pedantic, it's positively dangerous. The relational
operators always yield 0 or 1, but other boolean expressions can have
any non-zero value; for example, the is*() functions in <ctype.h>
return 0 for false, but can return any arbitrary non-zero value for
true.
Also note that not all compilers treat these the same !!

if ( boolean_flag == TRUE )
if ( boolean_flag )

Any compiler that does treat them the same is badly broken (unless the
compiler is able to prove that the value of boolean_flag is either 0
or 1).

This is a different issue from using something that's not a
boolean flag as a condition, such as "if (!ptr)" rather than
"if (ptr == NULL)", or "if (count)" rather than "if (count != 0)".
I prefer the more explicit comparison, but both forms are perfectly
valid and equally safe.

See also section 9 of the C FAQ.
 
J

Joe Butler

'king 'ell - Some people.

If you are going to "*plonk*", why do you have to broadcast it. That's the
equivalent of putting your fingers in your ears and going, baaaa! bblaah!
I'm not listening!

Plonker.
 
R

Richard Henry

Keith Thompson said:
I'll ignore, for now, the built-in type _Bool and the standard header
<stdbool.h> introduced in C99.

A value used as a condition in C is treated as false if it's equal to
zero, true if it's non-zero. It's not 0 vs. 1, it's 0 vs. anything
other than 0.

If your variable "boolean_flag" is something other than zero-is-false,
non-zero-is-true, it shouldn't be called "boolean_flag".

Assuming boolean_flag is a proper condition, "boolean_flag == TRUE"
isn't just pedantic, it's positively dangerous. The relational
operators always yield 0 or 1, but other boolean expressions can have
any non-zero value; for example, the is*() functions in <ctype.h>
return 0 for false, but can return any arbitrary non-zero value for
true.


Any compiler that does treat them the same is badly broken (unless the
compiler is able to prove that the value of boolean_flag is either 0
or 1).

This is a different issue from using something that's not a
boolean flag as a condition, such as "if (!ptr)" rather than
"if (ptr == NULL)", or "if (count)" rather than "if (count != 0)".
I prefer the more explicit comparison, but both forms are perfectly
valid and equally safe.

See also section 9 of the C FAQ.
 
K

Keith Thompson

Steve at fivetrees said:
Oh, so calling someone "clinically thick" over something so trivial is
somehow respectful?

Show some respect for others. *Then* you'd be entitled to talk to me on the
subject.

I suggest you take a look at this thread to see what really happened.

Joe Butler was asked not to top-post. He replied with a top-posted
followup. Someone else again reminded him not to top-post. His
response, in article <[email protected]>, was,
and I quote, "prick." (and it was, of course, top-posted).

There are good reasons for our convention (here in comp.lang.c) of
discouraging top-posting; I won't repeat them here. There are also
good reasons for not insulting people who are offering good advice.
If you've come into the middle of this discussion, it might not be
clear what's going on; groups.google.com can help you.

I suggest we drop this. I don't believe there are any relevant points
that haven't already been made.
 
C

Christian Bau

Skarmander said:
Hold on there. Is there any guarantee that any amount of -- applications
eventually yield a null pointer? Could this loop forever or does it
summon nasal demons?

Well, "toby" snipped too much of the previous post. What I wrote makes
only sense when you can read the bits that he removed as well.

(Hint: The last line of a joke is usually not very funny if the
beginning is missing).
 
C

Christian Bau

If you could see what toby replaced with three dots, then my post would
have made much more sense.
I suspect, based on his "then they will get what they deserve!", that
he really was referring to counting down a pointer.

I suspect that "toby" didn't get the joke that was there before he
snipped the part that I had been responding to.
 
S

Steve at fivetrees

Keith Thompson said:
Joe Butler was asked not to top-post. He replied with a top-posted
followup. Someone else again reminded him not to top-post. His
response, in article <[email protected]>, was,
and I quote, "prick." (and it was, of course, top-posted).

Ah. Mea culpa. If the disrespect I reacted to was in response to insulting
behaviour, then I apologise for leaping to the defence of the offender.
There are good reasons for our convention (here in comp.lang.c) of
discouraging top-posting; I won't repeat them here. There are also
good reasons for not insulting people who are offering good advice.

Fully agreed.

FWIW: I understand and agree with the reasons for discouraging top-posting.
However I've grown far more tired of the flamefests that result from
top-posting than of top-posting itself. My patience ran out when I saw what
I thought was an over-the-top response. I was wrong; I apologise.

Steve
http://www.fivetrees.com
 
C

Christian Bau

"toby said:
Which is all I intended to say. The code snippet made no real sense as
a pointer. I think the poster may have meant an integral type.

I think my post was mangled by some stupid idiot with a room temperature
IQ. And I mean Celsius.
 
W

Walter Banks

If you want a debate about top posting change the subject line so everyone who doesn't care if it is cross posted top posted, middle posted, bottom posted , written in one line or in HTML can filter out the noise.

Default said:
Joe said:
OK, point taken.
[snip]

Flash Gordon said:
Joe Butler wrote:

Don't top post. Replies belong after the text you are replying to.

You need to get the other point, the one about not top-posting.

Brian
 
A

Anonymous 7843

Oh dreidel, dreidel, dreidel
I made it out of clay
And when it's dry and ready
Then dreidel I shall play!

It has a lovely body
With legs so short and thin
And when my dreidel's tired
It drops and then I win!

Oh dreidel, dreidel, dreidel
I made it out of clay
And when it's dry and ready
Then dreidel I shall play!

My dreidel's always playful
It loves to dance and spin
A happy game of dreidel
Come play now, let's begin!

Oh dreidel, dreidel, dreidel
I made it out of clay
And when it's dry and ready
Then dreidel I shall play!
 
J

Joe Butler

Let's put this into perspective.

Althought I don't know how to verify this, I suspect many 100s, perhaps
1000s, of people read these posts.

Only 2 or 3 people told me not to top post. That's 3 out of 1000? 3 out of
10,000? Who knows. That suggests to me that the vast majority of people
don't give a damn. Probably, like me, the vast majority are easily able to
deal with all the forms of posting and it is, therefore, not an issue for
them.

I understand that top-posting annoys some people. For me, when someone
who's only contribution is to butt in with something along the lines of
"space corp. directive one ex delta nine has been violated", that is
infinitely more annoying than any other person's posting style.

The only input 'Default User' (prick) gave at that point was, "You need to
get the other point, the one about not top-posting". If Default User had
any sense, he would have realised that I had chosen to ignore the advice
about top-posting, that it was not the first time that I had been told and,
therefore, it was a pretty pointless exercise to step in at that point.

Sincere regards to everyone.


Keith Thompson said:
I suggest you take a look at this thread to see what really happened.

Joe Butler was asked not to top-post. He replied with a top-posted
followup. Someone else again reminded him not to top-post. His
response, in article <[email protected]>, was,
and I quote, "prick." (and it was, of course, top-posted).

There are good reasons for our convention (here in comp.lang.c) of
discouraging top-posting; I won't repeat them here. There are also
good reasons for not insulting people who are offering good advice.
If you've come into the middle of this discussion, it might not be
clear what's going on; groups.google.com can help you.

I suggest we drop this. I don't believe there are any relevant points
that haven't already been made.
 
T

toby

Christian said:
Well, "toby" snipped too much of the previous post. What I wrote makes
only sense when you can read the bits that he removed as well.

(Hint: The last line of a joke is usually not very funny if the
beginning is missing).

Apologies, you're right: I didn't realise that the error was
intentional. Too subtle for me at that moment...

--T
 
T

Tom

Joe said:
Let's put this into perspective.

Althought I don't know how to verify this, I suspect many 100s, perhaps
1000s, of people read these posts.

Only 2 or 3 people told me not to top post. That's 3 out of 1000? 3 out of
10,000? Who knows. That suggests to me that the vast majority of people
don't give a damn. Probably, like me, the vast majority are easily able to
deal with all the forms of posting and it is, therefore, not an issue for
them.

Don't know about majority, personally when I see a post that hard to
make sense of I'll just skip it and jump to next one...

Tom
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top