loop for all values of ushort ports using ushort index .... with caution.

Y

Yakov

What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.

Yakov
 
R

Richard Heathfield

Yakov said:
What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.

Obviously not, because of wrap.

"Nicest" is in the eye of the beholder. Personally, I'd use an unsigned
long int as the index, but you say you don't want that.

Here's one way:

int flag = 0;
for(port = 0; flag == 0; port++)
{
do_stuff(port);
if(port == PORT_MAX)
{
flag = 1;
}
}

Blech, right?

Here's another way:

port = 0;
top:
{
do_stuff(port);
if(port < PORT_MAX)
{
++port;
goto top; /* cough, splutter, gurgle, aaargh */
}
}
 
Y

Yakov

Yakov said:



Obviously not, because of wrap.

"Nicest" is in the eye of the beholder. Personally, I'd use an unsigned
long int as the index, but you say you don't want that.

Here's one way:

int flag = 0;
for(port = 0; flag == 0; port++)
{
do_stuff(port);
if(port == PORT_MAX)
{
flag = 1;
}

}

Blech, right?

Here's another way:

port = 0;
top:
{
do_stuff(port);
if(port < PORT_MAX)
{
++port;
goto top; /* cough, splutter, gurgle, aaargh */
}

}

I believe the standard does not contain, anywhere, the guarantee that
long is strictly wider that short.

That would be weird platform, you'd say.
Yes, that would be weird platform.So what.Still adhering to the
standard.

But I'd not want the code to be broken by such very subtle assumtions.
From here, the desire to not rely on assumptions that you can have
index of wider int than the type in question, the ushort.

Yakov
 
L

Lew Pitcher

What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.

{
unsigned short port = 0;

do
{
/*
** something with port
*/
} while (++port != 0);
}
 
R

Richard Heathfield

Yakov said:
I believe the standard does not contain, anywhere, the guarantee that
long is strictly wider that short.

Correct, but you specifically requested a range from 0 to 0xffff, and
the Standard /does/ guarantee that unsigned long is wide enough to
support that range with plenty of horses left over.
 
R

Ralf Damaschke

Walter said:
[...] Lew Pitcher said:
What would be the nicest way to write the loop for all
values of unsigned short (0..0xffff), usnig 'unsigned short
port;' as an index ?
unsigned short port = 0;
do
{
/*
** something with port
*/
} while (++port != 0);
That doesn't meet the spec if the implementation's unsigned
short can hold more than 0xffff .

Well then let us change the condition to (port++ < 0xFFFF).

Ralf
 
L

Lew Pitcher

That doesn't meet the spec if the implementation's unsigned short
can hold more than 0xffff .

I took the value range "(0..0xffff)" as a non-binding footnote to the
specification that the loop should flow "for all values of unsigned
short". To me, this /does/ meet the spec. Agreed, it does not meet
the footnote, unless (sizeof(unsigned short) * CHAR_BIT) == 16
 
K

Keith Thompson

Yakov said:
I believe the standard does not contain, anywhere, the guarantee that
long is strictly wider that short.
Correct.

That would be weird platform, you'd say.
Yes, that would be weird platform.So what.Still adhering to the
standard.

Not that weird. I've worked on a system where short, int, and long
are all 64 bits.
 
J

J. J. Farrell

That doesn't meet the spec if the implementation's unsigned short
can hold more than 0xffff .

The spec is ambiguous since it specifies both 'all values of unsigned
short' and 'all values in the range 0 to 0xffff'. Meeting either
requirement could be interpreted as meeting the spec, though rejecting
the spec would be safer.
 
P

pete

Richard said:
Yakov said:


Correct, but you specifically requested a range from 0 to 0xffff, and
the Standard /does/ guarantee that unsigned long is wide enough to
support that range with plenty of horses left over.

I don't think any extra horses are needed.
But I would prefer to use type unsigned to run through
all the values of unsigned short.
The thing that I don't like about lower ranking types than int,
is that they usually get promoted,
which complicates the semantics of the code
and can also slow down the program.

The thing I don't like about unsigned types lower ranking than int,
is that they can flip.
If INT_MAX equals USHRT_MAX, which it may,
then (USHRT_MAX + 1) is undefined.

unsigned short port = -1;
/*
** or
** unsigned port = (unsigned short)-1;
*/
do {
/**/
} while (port-- != 0);
 
K

Keith Thompson

J. J. Farrell said:
The spec is ambiguous since it specifies both 'all values of unsigned
short' and 'all values in the range 0 to 0xffff'. Meeting either
requirement could be interpreted as meeting the spec, though rejecting
the spec would be safer.

Meeting one requirement but not the other would fail to meet the spec.
This implies that the spec cannot be met *unless* USHRT_MAX==0xffff.
This restriction should have been made explicit.

(More likely, the original poster just wasn't aware that unsigned
short can be bigger than 16 bits.)
 
R

Richard Tobin

[/QUOTE]
(More likely, the original poster just wasn't aware that unsigned
short can be bigger than 16 bits.)

He probably really wanted to loop from 0 to 0xffff, and the "ports" in
question are TCP/IP port numbers. In fact, port 0 is reserved, which
simplifies the problem.

-- Richard
 
C

CBFalconer

J. J. Farrell said:
The spec is ambiguous since it specifies both 'all values of unsigned
short' and 'all values in the range 0 to 0xffff'. Meeting either
requirement could be interpreted as meeting the spec, though rejecting
the spec would be safer.

So correct the code.

for (port = 0; port < 0xffff; port++) {
/* do something with port */
}
/* do something with port */
 
N

Nick Keighley

What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.

how about

unsigned short port = 0;
for (;;)
{
frobnicate (port);
if (port == 0xffff)
break;
port++;
}
 
R

Ralf Damaschke

pete said:
Peter said:
[about:]
unsigned short port = 0;
do { [...]
} while (++port != 0);
It's undefined if INT_MAX equals USHRT_MAX.

while ((port += 1u) != 0);

That won't do what you want,
if UINT_MAX is greater than USHRT_MAX.

Would you care to explain? The loop (in this sub-thread) was
meant to iterate over all unsigned values. Assuming arbitralily
UINT_MAX > USHRT_MAX = 0xFFFF and the most-may-be-critical value
port = 0xFFFF I do not see any problem; the value will be
converted to int, then incremented by 1, giving 0x10000 (which
by supposition is still representable by int). Then 0 will be
stored to port and that is the value of the assignment.

So what is wrong?

Ralf
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top