Mystery: static variables & performance

N

nrk

Peter said:
nrk said:
Ok, here's my take on this:

a) sizeof(int) > 1 for hosted implementations.
http://www.google.com/[email protected]

There mere fact that Dan Pop says so does not make it so! ;)

There are actual members of the C Committee who disagree on this.
So, integer overflow not an issue, yes?

No. Even if sizeof(int) == 2, you can still have INT_MAX < UCHAR_MAX.
[It's the limits which are important, not the byte size.]

Sorry, I goofed it up, but if you read the quoted thread, the idea is that
INT_MAX >= UCHAR_MAX for hosted implementations by implication. Can you
point out why there can be a disagreement on that?
No. Reading chars via an unsigned char lvalue can produce a different
value to the original.

This is only in the presence of padding bits, right? Or is there something
else that I am missing here?

-nrk.
 
P

pete

Peter said:
No. Even if sizeof(int) == 2, you can still have INT_MAX < UCHAR_MAX. [It's
the limits which are important, not the byte size.]
b) Peter's concern still remains. So, does changing the last line to:

return *(unsigned char *)s1 - *(unsigned char *)s2;

make it alright?

No. Reading chars via an unsigned char lvalue
can produce a different value to the original.

That's what's called for.
Since character constants and I/O are based on
unsigned char -> int -> char
_conversions_ when storing plain char strings,
the correct answer (assuming no integer overflow)
is to use a _conversion_ of the plain char value to
unsigned char...

Conversion is not called for.
The functions which use converted values, have the word "converted"
in their function descriptions.
return (unsigned char) *s1 - (unsigned char) *s2;

Note that on most implementations
(8-bit, 2c, no padding) there is no need
to go to this extreme, although the result is the same.

The most robust answer would seem to be...

return (unsigned char) *s1 > (unsigned char) *s2
- (unsigned char) *s1 < (unsigned char) *s2;

or...

return (unsigned char) *s1 < (unsigned char) *s2 ? -1
: (unsigned char) *s1 > (unsigned char) *s2;

I'm not seeing it that way.
((unsigned char) *s1), is *s1 *Converted* to unsigned char.
(*(unsigned char*)s1), is *s1, interpreted as unsigned char.

N869
7.21.4 Comparison functions
[#1] The sign of a nonzero value returned by the comparison
functions memcmp, strcmp, and strncmp is determined by the
sign of the difference between the values of the first pair
of characters (both interpreted as unsigned char) that
differ in the objects being compared.

memchr is a function which uses both converted and
differently interpreted values.

N869
7.21.5.1 The memchr function
Description
[#2] The memchr function locates the first occurrence of c
(converted to an unsigned char) in the initial n characters
(each interpreted as unsigned char) of the object pointed to
by s.

void *memchr(const void *s, int c, size_t n)
{
const unsigned char *p = s;

while (n-- != 0) {
if (*p == (unsigned char)c) {
return (void *)p;
}
++p;
}
return NULL;
}
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top