comp.lang.ada
 help / color / mirror / Atom feed
From: David Thompson <dave.thompson2@verizon.net>
Subject: Re: Importing C function with variable argument list
Date: Sun, 13 May 2012 00:18:28 -0400
Date: 2012-05-13T00:18:28-04:00	[thread overview]
Message-ID: <3eduq7lqvgmp2ui5kt1k5pgk2oopvo9o31@4ax.com> (raw)
In-Reply-To: 696103.1385.1336076109161.JavaMail.geo-discussion-forums@vbez18

On Thu, 3 May 2012 13:15:09 -0700 (PDT), sbelmont700@gmail.com wrote:

> On Friday, April 27, 2012 4:24:59 AM UTC-4, Natasha Kerensikova wrote:
> > Of course, on platforms where calling convention transmits C.short
> > arguments exactly like C.int (e.g. for improved alignment, or because
> > the platform has C.short equivalent to C.int (but then you could use
> > C.char instead of C.short, since C.char is guaranteed to be strictly
> > smaller than C.int on conforming hosted implementations)).
> 
> Isn't the general rule of thumb (since nothing in C is ever well-defined) 
that an int is the size of the default machine word (32-bit on 32-bit,
64 on 64, etc)?  If that's the case, whether it's stack or register,
the calling convention is very likely going to use an int sized
argument for anything smaller than an int anyway (e.g. if it uses the
32-bit stack, it would have to push a one-byte char as an int-sized
value anyway).

Yes. There are _some_ things well-defined in C (not nearly all), but
to your main point, 6.2.5p5 (formerly 6.1.2.5) says: A [signed] int
object has the natural size suggested by the architecture of the
execution environment (large enough to contain any value in the range
INT_MIN to INT_MAX as defined in the header <limits.h>). And p6 says
that unsigned int has the same size as signed int, and 5.2.4 requires
(without explicitly saying so) that size be at least 16 bits.

Technically this is normative text but there is no formal definition
of 'natural' so it can't actually be verified/enforced. And there are
machines where there can be legitimate controversy which of two (or
even more) widths is more natural. But where the machine does have a
word size, it is >= 16, and a C implementor doesn't use it for int,
they deserve to be tarred and feathered (not gzipped! <G>).

And yes, for all ABIs I've seen and most I can imagine, this means
that C.char and C.short will be passed the same as C.int, even though
formally they have different types in C (as well as Ada). However, the
default argument promotions in C (for varargs and also K&R1, as PP
correctly noted) also take 'float' to 'double'; it is fairly common
(though not required) for these to be passed differently in C, and
thus probably demonstrate PP's issue.

Also it isn't absolutely guaranteed that C char is smaller than int
(or more precisely that sizeof(int)>1). c.l.c loves to discuss offbeat
architectures like DSPs where it may actually make sense to have
char=short=int all 22-bit or somesuch, and thus 110%-portable code
can't assume that getchar()==EOF isn't actually a data character and
must 'confirm' it with feof() and/or ferror(). In practice no one has
reported any system where it both makes sense to have (C) char other
than 8 or at most 9 bits, and using stdio makes any sense at all.




  reply	other threads:[~2012-05-13  4:18 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-10  9:21 Importing C function with variable argument list Maxim Reznik
2012-04-10 11:22 ` Patrick
2012-04-10 13:09   ` Maxim Reznik
2012-04-10 13:50     ` Georg Bauhaus
2012-04-10 13:18 ` Markus Schöpflin
2012-04-10 16:47   ` Simon Wright
2012-04-12 10:08     ` Markus Schöpflin
2012-04-12 16:58       ` Adam Beneschan
2012-04-12 20:47         ` Randy Brukardt
2012-04-13  8:43           ` Markus Schöpflin
2012-04-13 23:01             ` Randy Brukardt
2012-04-27  8:24           ` Natasha Kerensikova
2012-04-27  9:18             ` Jacob Sparre Andersen
2012-04-27 16:48               ` Natasha Kerensikova
2012-05-03 20:15             ` sbelmont700
2012-05-13  4:18               ` David Thompson [this message]
2012-05-13  9:03                 ` Simon Wright
2012-05-13 17:01                   ` Jeffrey Carter
2012-05-13 18:20                     ` Simon Wright
2012-05-13 19:11                       ` Jeffrey Carter
2012-05-13 19:55                         ` Ludovic Brenta
2012-05-14  0:52                           ` Jeffrey Carter
2012-05-13 21:12                         ` Simon Wright
2012-05-13 23:57                           ` Georg Bauhaus
2012-05-14  0:54                           ` Jeffrey Carter
2012-05-14  8:10                             ` Nomen Nescio
2012-05-14 15:21               ` Natasha Kerensikova
2012-05-14 20:53                 ` sbelmont700
2012-04-13  4:08         ` Tero Koskinen
2012-04-13  8:04           ` Markus Schöpflin
2012-04-10 20:02 ` Tero Koskinen
2012-04-13  3:28   ` Tero Koskinen
2012-04-10 20:25 ` sbelmont700
2012-04-11 23:24 ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox