473,883 Members | 2,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

va_arg and short

I was compiling a program written by someone else about six years ago, and
widely distributed at the time. It also includes makefiles for many
different systems, so I know it has been compiled with many different
compilers.

I got compile errors when it used va_arg to fetch an argument of type short.
That seemed a little strange to me, so I changed it to int and it compiled
just fine.

So now I wonder, just what is the rule for va_arg and short? It would seem
strange to reject certain types, yet that is what the compiler did.

-- glen
Nov 13 '05
99 9135
Da*****@cern.ch (Dan Pop) wrote in message news:<bo******* ***@sunnews.cer n.ch>...
In <8b************ *************@p osting.google.c om> ku****@wizard.n et (James Kuyper) writes:

....
[Re: (short)va_arg(a p,int)]
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.


Chapter and verse, please.


The relevant section is 6.3.1.3, but I overstated the problem a
little; it doesn't say "undefined" . What it says is "either the result
is implementation-defined or an implementation-defined signal is
raised".

However, there's no portable way for a program to determine which
signal (if any) needs to be caught, and even if you do catch it,
there's almost nothing portably safe you can do from the signal
handler, and returning from the signal handler might itself cause
undefined behavior. Even if you merely get an implementation-defined
value, that's no better than calling rand().

Therefore, as a practical matter there's little difference between
this and undefined behavior, at least for programs intended to be
portable. I do concede, though, that it is in fact
implementation-defined, not undefined.
Nov 13 '05 #71
In <8b************ **************@ posting.google. com> ku****@wizard.n et (James Kuyper) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<bo******* ***@sunnews.cer n.ch>...
In <8b************ *************@p osting.google.c om> ku****@wizard.n et (James Kuyper) writes:

...
[Re: (short)va_arg(a p,int)]
>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.


Chapter and verse, please.


The relevant section is 6.3.1.3, but I overstated the problem a
little; it doesn't say "undefined" . What it says is "either the result
is implementation-defined or an implementation-defined signal is
raised".

However, there's no portable way for a program to determine which
signal (if any) needs to be caught, and even if you do catch it,
there's almost nothing portably safe you can do from the signal
handler, and returning from the signal handler might itself cause
undefined behavior. Even if you merely get an implementation-defined
value, that's no better than calling rand().

Therefore, as a practical matter there's little difference between
this and undefined behavior, at least for programs intended to be
portable. I do concede, though, that it is in fact
implementati on-defined, not undefined.


Furthermore, there is no known conforming implementation where a signal
is raised (this is a new C99 "feature" that would break existing and
*portable* C89 code).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #72

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bo******** *@sunnews.cern. ch...

(snip of related question)
Thanks. Has anyone actually did it this way in *real* C code?


And a related question: is calloc() *required* to fail if nmemb * size
exceeds SIZE_MAX?


What do you do on those unfortunate systems where pointers can address
memory much larger than size_t?

(Remembering all the tricks from the large model x86 days. Maybe that would
be huge model, which I tried not to use.)

This is not unrelated to the fseek() and ftell() problem on file systems
that allow files greater than 2GB or 4GB, but no convenient way to address
them.

-- glen
Nov 13 '05 #73

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bo******** **@sunnews.cern .ch...
In <8b************ **************@ posting.google. com> ku****@wizard.n et (James Kuyper) writes:

(snip)
The relevant section is 6.3.1.3, but I overstated the problem a
little; it doesn't say "undefined" . What it says is "either the result
is implementation-defined or an implementation-defined signal is
raised". However, there's no portable way for a program to determine which
signal (if any) needs to be caught, and even if you do catch it,
there's almost nothing portably safe you can do from the signal
handler, and returning from the signal handler might itself cause
undefined behavior. Even if you merely get an implementation-defined
value, that's no better than calling rand().


(snip)
Furthermore, there is no known conforming implementation where a signal
is raised (this is a new C99 "feature" that would break existing and
*portable* C89 code).


Not at all trying to start a language war, (there are enough already), but
in PL/I returning from signal handlers is supposed to be portably safe and
well defined. (There is the assumption that the OS and hardware support it.
Machines with imprecise interrupts tended not to support it very well.)

I was disappointed that Java supplies no way to return from signal handlers.

-- glen
Nov 13 '05 #74
"Douglas A. Gwyn" <DA****@null.ne t> wrote in message news:<3F******* ********@null.n et>...
Dan Pop wrote:
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.


What is actually wanted is to enforce the total size limit of
SIZE_MAX.


Recently, I've taken to writing code that assumes that the largest
safe size of an object allocation is the minimum of PTRDIFF_MAX and
SIZE_MAX.

--
Peter
Nov 13 '05 #75
Dan Pop wrote:
Thanks. Has anyone actually did it this way in *real* C code?


Most programmers don't try to create objects so large that
they have to worry about SIZE_MAX. If you do have to worry,
use a method like what Francis showed.

Nov 13 '05 #76
Glen Herrmannsfeldt wrote:
"Dan Pop" <Da*****@cern.c h> wrote ...
And a related question: is calloc() *required* to fail if nmemb * size
exceeds SIZE_MAX?

It probably doesn't matter; how is a s.c. program going to
treat the resulting chunk of memory as a single object with
size exceeding SIZE_MAX?
What do you do on those unfortunate systems where pointers can address
memory much larger than size_t?
The answer to questions on this general issue is the subject
of some debate, so if you want an authoritative resolution
you need to get a DR submitted asking for clarification.
Some think that the implementation is obliged to choose a
type for size_t such that this cannot happen.
This is not unrelated to the fseek() and ftell() problem on file systems
that allow files greater than 2GB or 4GB, but no convenient way to address
them.


That's an implementation that has made some unwise choices.
You can use fgetpos/fsetpos for many such purposes, but there
is no standard way to perform arithmetic on those offset
cookies.

Nov 13 '05 #77

"Douglas A. Gwyn" <DA****@null.ne t> wrote in message
news:gd******** ************@co mcast.com...
Glen Herrmannsfeldt wrote:
"Dan Pop" <Da*****@cern.c h> wrote ...
And a related question: is calloc() *required* to fail if nmemb * size
exceeds SIZE_MAX?

Someone wrote:
It probably doesn't matter; how is a s.c. program going to
treat the resulting chunk of memory as a single object with
size exceeding SIZE_MAX? What do you do on those unfortunate systems where pointers can address
memory much larger than size_t?
Well, that is a good question. Maybe 16 bit int is long past, and we
shouldn't worry about the problem. Though with 32 bit int and memories
larger than 4GB maybe we should. Consider a machine with 64 bit pointers,
but 32 bit int. You could still increment the pointer multiple times, less
than 2GB each time. Now, reasonably likely the machine would also have a
64 bit long or long long, but maybe doesn't supply the ability to increment
pointers (or subscript arrays) with them.

I notice some time ago that Java requires subscripts to be int (or castable
to int).
The answer to questions on this general issue is the subject
of some debate, so if you want an authoritative resolution
you need to get a DR submitted asking for clarification.
Some think that the implementation is obliged to choose a
type for size_t such that this cannot happen. This is not unrelated to the fseek() and ftell() problem on file systems
that allow files greater than 2GB or 4GB, but no convenient way to address them.

That's an implementation that has made some unwise choices.
You can use fgetpos/fsetpos for many such purposes, but there
is no standard way to perform arithmetic on those offset
cookies.


Those are interesting, but it seems that some still have a 32bit limit.
HP-UX seems to have non-standard fgetpos64() and fsetpos(64).

Well, the argument of fseek() and the return from ftell() were long (or even
int?) many years before 4GB of memory was affordable, real or virtual. It
looks like a mistake now, and then we have things like fseek64() and
ftell64().

Some systems have a 64 bit long, but that seems relatively rare these days.

-- glen
Nov 13 '05 #78
On 2003-11-10 14:56, Dan Pop wrote:
In <3f********@new s.unimelb.edu.a u> Fergus Henderson <fj*@cs.mu.oz.a u> writes:
For size_t x and y, you have x <= SIZE_MAX && y <= SIZE_MAX,
and x + y <= SIZE_MAX iff SIZE_MAX - x >= y,
and x * y <= SIZE_MAX iff x == 0 || SIZE_MAX / x >= y.

You can hence enforce the total size limit using functions such as
the following:

size_t plus(size_t x, size_t y) {
return (SIZE_MAX - x >= y) ? x + y ; error();
}

size_t times(size_t x, size_t y) {
return (x == 0 || SIZE_MAX / x >= y) ? x * y ; error();
}


Thanks. Has anyone actually did it this way in *real* C code?


Yes, though I used type-generic macros.
Excerpt:

/*
* T is assumed to be an integer type with minimum value T_MIN and
* maximum value T_MAX. Yields +1 or -1 if the multiplication of
* a and b in T would result in an overflow or underflow,
* respectively, otherwise 0.
*
* Preconditions: (1) a and b are integer values
* (2) T is an integer type
* (3) T_MIN and T_MAX are values of integer type
* (4) T_MIN and T_MAX are valid limits of type T
* (5) T can represent a and b
*
* Does not compile if either T_MIN, T_MAX, a or b is not a numeric
* value or if T is not a numeric type.
*/

#define UT__integer_prd _ovf(T, T_MIN, T_MAX, a, b) ( \
(T)(a) > 0 \
? (T)(b) > 0 \
? (T)(T_MAX) / (T)(b) < (T)(a) ? +1 : 0 \
: (T)(T_MIN) / (T)(a) > (T)(b) ? -1 : 0 \
: (T)(b) > 0 \
? (T)(T_MIN) / (T)(b) > (T)(a) ? -1 : 0 \
: (T)(b) < 0 \
? ( (T)(T_MAX) + (T)(T_MIN) > 0 \
? ( (T)(T_MAX) / -(T)(b) < -(T)(a)) \
: (-((-(T)(T_MAX)) / (T)(b)) > (T)(a)) \
) ? +1 : 0 \
: 0 \
)

#define UT_integer_prd_ ovf(T, T_MIN, T_MAX, a, b) ( \
UT_PRECONDITION ("a is an integer value", \
UT_is_integer_v alue(a)), \
UT_PRECONDITION ("b is an integer value", \
UT_is_integer_v alue(b)), \
UT_PRECONDITION ("T is an integer type", \
UT_is_integer_t ype(T)), \
UT_PRECONDITION ("T_MIN is an integer value", \
UT_is_integer_v alue(T_MIN)), \
UT_PRECONDITION ("T_MAX is an integer value", \
UT_is_integer_v alue(T_MAX)), \
UT_PRECONDITION ("T_MIN and T_MAX are valid limits of type T", \
UT__valid_integ er_type_limits( T, T_MIN, T_MAX)), \
UT_PRECONDITION ("T can represent a", \
UT__convertible _to_integer_typ e(a, T, T_MIN, T_MAX)), \
UT_PRECONDITION ("T can represent b", \
UT__convertible _to_integer_typ e(b, T, T_MIN, T_MAX)), \
UT__integer_prd _ovf(T, T_MIN, T_MAX, a, b) \
)

[...]

#define UT_size_prd_ovf (a, b) UT_integer_prd_ ovf(size_t, 0, SIZE_MAX, a, b)

There were similar macros for all common integer types and operations,
for saturation arithmetics and so on.

-- Niklas Matthies
Nov 13 '05 #79
In comp.std.c Douglas A. Gwyn <DA****@null.ne t> wrote:


You can use fgetpos/fsetpos for many such purposes, but there
is no standard way to perform arithmetic on those offset
cookies.


There is, but it's ugly:

/* add n to pos */
fsetpos(f, pos);
fseek(f, n, SEEK_CUR);
fgetpos(f, &pos);

-Larry Jones

Monopoly is more fun when you make your own Chance cards. -- Calvin
Nov 13 '05 #80

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2282
by: Niels Dekker (no reply address) | last post by:
Is it possible for a standard compliant C++ compiler to have ( sizeof(short) < sizeof(int) ) and ( sizeof(short) == sizeof((short)0 + (short)0) ) ? Regards, Niels Dekker http://www.xs4all.nl/~nd/dekkerware
34
16713
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3;
8
1701
by: NilsNilsson | last post by:
I wrote this: short s1 = 0; short s2 = 1; short s3 = s1 + s2; And gor this compile error message: Cannot implicitly convert type 'int' to 'short' What is wrong here?
8
16420
by: Ken Dopierala Jr. | last post by:
Hi, I'm reading the header file of a PCX image and I need to convert 2 bytes to a short. How would I go about doing this? I know that they are bytes 8 & 9 in my byte array. I'm not sure how to take those two and convert them into a short though. In C I would just use a union and assign them accordingly. Thanks! Ken.
15
8452
by: Steffen Loringer | last post by:
Hi, I'm using the following function to join 2 char (byte) into one short on a 32 bit X86 platform: unsigned short joinUnsigShort(unsigned char a,unsigned char b) { unsigned short val = 0; val = a; val <<= 8;
4
5226
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a 'uint16_t short'. The compiler (GNU 3.3.5) allows this but ignores the original unsigned keyword and initialzes the variable as a signed short int. I created a very simple test program just to see what was happening: #include <stdio.h> ...
10
5661
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl; } else
10
5459
by: nyhetsgrupper | last post by:
The following code result in the following compilation error: Error 1 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) short a = 1; short b = 2; short result = a + b; Can anyone explain why??
3
6413
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void foo(OutputType*) bla.cxx:10: note: void foo(PixelType*)
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7977
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.