J
James Kuyper
[email protected] (Mantorok Redgormor) wrote in message news: said:so to get the effect of a short, you would do something like this?
(short) va_arg(ap, int);
Exactly.
[email protected] (Mantorok Redgormor) wrote in message news: said:so to get the effect of a short, you would do something like this?
(short) va_arg(ap, int);
In said:so to get the effect of a short, you would do something like this?
(short) va_arg(ap, int);
Dan Pop said:In <[email protected]> Daniel Haude
Only binary distributions, validated for his platform.
Both require administrator privileges, which the normal computer user is
not supposed to have. In practice, there are plenty of normal users that
do have administrator provileges, and this explains why many machines get
screwed up and reinstalled from scratch far more often than others.
Dan Pop said:In <[email protected]> (e-mail address removed)
(Mantorok Redgormor) said:The cast is *useless*, due to the integral promotions. If the original
argument had type short, va_arg(ap, int) will yield a value that is
guaranteed to be in the range of the short type.
"Peter Nilsson said:Peter Nilsson said:Christian Bau said:I wonder why va_arg could not accept a type subject to promotion and
handle them accordingly, i.e. accept the promoted type implicitly and
convert it to the original/requested type. [So, cases like the
character types and unsigned short, which might promote to signed or
unsigned int, could be handled without having to resort to integer
limit checks.]
These are no problem anyway, because the C Standard explicitely allows
you to use for example unsigned int instead of int if the value passed
can be represented in both types. So whether unsigned short is passed as
int or unsigned int, you can always read it as an unsigned int.
I see that in C99. Is it also in the previous standard? [It doesn't appear
to be in my copy of the C89 draft.]
In any case, plain char's signedness still causes a problem.
Include float in the list too.
James Kuyper said:Every arithmetic type has a range that overlaps the range of any other
arithmetic type, at the very least in the range from 0 to 127,
inclusive. Therefore, I presume that what you meant is not quite what
you said.
I presume that you're actually trying to refer to 6.2.5p8
Dan said:Where does the standard define the proper usage of size_t?
Douglas A. Gwyn said:The standard is not a tutorial.
"Glen Herrmannsfeldt said:Well, yes, but if size_t is supposed to hold a value that can be the
sizeof() something, the question is when would you want to multiply those.
The argument to malloc(), for example, should be the number of elements
desired * sizeof(one element).
Say, for example, I wanted to store the individual bytes of an int variable,
each in an int.
int num;
char *tmp;
int *bytes;
tmp=malloc(sizeof(num));
memcpy(tmp,(unsigned char*)&num,sizeof(num));
bytes=malloc(sizeof(num)*sizeof(*bytes));
Two size_t values multiplied together.
In said:Two size_t values multiplied, and the result cast to size_t.
If the product fits into size_t, then everything is fine. If the product
does not fit into size_t, then you will not get the memory that you
wanted, no matter whether the multiplication produced defined or
implementation defined or undefined behavior.
In said:If you want to install it as root, such is in /usr/bin, /usr/local/bin, or
if it requires setuid root, yes.
Many programs can be installed in a users own directory and run fine from
there. The program that started this thread runs fine in a user directory.
A large fraction of the GNU programs will, also.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^But if you don't know that the original was of type short, and requiring int
in the va_arg call means you don't, ^^^^^^^^^^^^^^^^^
then you can cast it.
I am not sure
now if the compiler is required to enforce the cast. (If it is required to
enforce short, is it also required to enforce float?)
As an unrelated topic, did you ever find a web page where the menu didn't
include the value you wanted, download the html page, modify it, and load it
into the browser? CGI programs should not rely on the menu to enforce
limits. va_arg() won't enforce them, either.
Dan Pop said:In <99aqb.82956$275.242639@attbi_s53> "Glen Herrmannsfeldt" ^^^^^^^^^^^^^^^^^
Where did you get this idea from? You require int because you *know* that
short is promoted to int. However, if the function specification required
short, there is no point in casting. See below.
What for? If the caller provided a value outside of the range of short,
the cast will prevent you from detecting the error condition (the runtime
equivalent of casting malloc's return value). If the caller provided
a good value, the cast buys you nothing (the analogy with casting malloc
still holds). And if you later decide to change the type of the argument
from short to int, the cast becomes a potential bug (if you forget to
remove it) and no compiler is going to warn you about it. It's a typical
example of a "gratuitous" cast having a fairly high price.
I don't understand what you're talking about. The compiler doesn't
enforce anything, it merely performs the default argument promotions when
calling the function and nothing else.
va_arg() doesn't enforce anything. It merely retrieves parameter values.
And your point is?
Glen said:bytes=malloc(sizeof(num)*sizeof(*bytes));
Two size_t values multiplied together.
In said:The example looks broken, but anyway,
the issue was wanting to get wraparound modulo wordsize,
not what you would want here (or in any other legitmate
use of size_t).
If I read a value into a short variable with scanf() I can be 100% sure that
the value in the variable is within the range of a short. I might, for
example, use it as an array subscript knowing it couldn't be outside the
range of a short.
If I expect a short argument, assume that its value is within the range of a
short, but a sneaky caller supplies an int instead, I might, for example,
access outside of an array. The cast, if enforced, ensures that the value
is within the range of a short.
Is the compiler required to enforce the cast, such that the value can never
be outside the range of a short?
Is it required to enforce (float), even on machines with extended floating
point form where all arithmetic is done with a 64 bit mantissa?
If the compiler enforces the cast it will truncate (or something else) the
value to fit within the range of a short, even though it is supplied, and is
assigned to, an in variable.
The claim, not by me, was that the (short) cast on va_arg() would give the
effect of a short. You were claiming that the cast was useless.
I
believe, but I am not sure, that with the cast the compiler is required to
convert to a short before assigning, or otherwise using, the return value of
(short)va_arg().
If I read a value into a short variable with scanf() I can be 100%
sure that the value in the variable is within the range of a short.
I might, for example, use it as an array subscript knowing it couldn't
be outside the range of a short.
If I expect a short argument, assume that its value is within the
range of a short, but a sneaky caller supplies an int instead, I
might, for example, access outside of an array. The cast, if
enforced, ensures that the value is within the range of a short.
Though the & operator is probably better at limiting the range of
values than a (short) cast.
Is the compiler required to enforce the cast, such that the value can
never be outside the range of a short?
Is it required to enforce (float), even on machines with extended
floating point form where all arithmetic is done with a 64 bit
mantissa?
If the compiler enforces the cast it will truncate (or something else)
the value to fit within the range of a short, even though it is
supplied, and is assigned to, an in variable.
The claim, not by me, was that the (short) cast on va_arg() would give
the effect of a short. You were claiming that the cast was useless.
I believe, but I am not sure, that with the cast the compiler is
required to convert to a short before assigning, or otherwise using,
the return value of(short)va_arg().
Glen Herrmannsfeldt said:If I read a value into a short variable with scanf() I can be 100% sure that
the value in the variable is within the range of a short. I might, for
example, use it as an array subscript knowing it couldn't be outside the
range of a short.
If I expect a short argument, assume that its value is within the range of a
short, but a sneaky caller supplies an int instead, I might, for example,
access outside of an array. The cast, if enforced, ensures that the value
is within the range of a short.
Though the & operator is probably better at limiting the range of values
than a (short) cast.
Is the compiler required to enforce the cast, such that the value can never
be outside the range of a short?
If the compiler enforces the cast it will truncate (or something else) the
value to fit within the range of a short, even though it is supplied, and is
assigned to, an in variable.
The claim, not by me, was that the (short) cast on va_arg() would give the
effect of a short. ...
... You were claiming that the cast was useless. I
believe, but I am not sure, that with the cast the compiler is required to
convert to a short before assigning, or otherwise using, the return value of
(short)va_arg().
In said:I don't know what "enforced" means to you; it means nothing to the
standard. If the value returned by va_arg(ap,int) is outside the valid
range of 'short', the behavior is undefined.
Dan said:That's precisely what he wanted here: when multiplying two size_t values
together, to compute the size of a large object, overflow may happen and
you may want to be able to detect it.
As usual, you're replying to a post you haven't bothered to read
carefully.
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.