Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Function casting - UB? |
| Date | 2012-06-27 12:49 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <jsfdik$vtr$1@dont-email.me> (permalink) |
| References | <0c176b1e-39f3-4e65-8a7b-eca16b8955c5@d24g2000yqh.googlegroups.com> |
On 6/27/2012 11:50 AM, nroberts wrote:
> In C++ this would be totally undefined. How about in C? It works on
> my machine with my compiler...
>
> If it's UB, is it one of those rules that pretty much has to work
> anyway? What I mean is things like the fact that it's UB to assign to
> one part of a union and read from another but it generally works and
> is a pretty important construct. Is this like that?
>
> #include <stdio.h>
>
> typedef void (*fun)(int, int);
>
> void f(int i) {
> int q = 33;
> printf("%d %d\n", i, q);
> }
>
> int main(void) {
> fun funptr = (fun)f;
> funptr(3,2);
>
> return 0;
> }
>
> output => 3 33
Undefined behavior, because the type of the pointer expression
`funptr' used in the call does not match the type of the called
function `f'. (The fact that the `(fun)' cast was necessary
should alert you to the mismatch.)
It's likely to "work" on a good many systems. For example,
on a system where the first four or so "sufficiently small"
arguments are passed in a few designated registers, `main' will
fill two registers and `f' will use only one of them, and there's
a decent chance that the one `f' uses will be the one `main' put
the 3 into. But it would be a bad idea to generalize from the
apparent success of this simple case to the notion "It works!"
Throw in a floating-point argument, or a union argument, or even
more than MagicNumber plain int arguments, and things may well go
blooey. Make the function struct- or union-valued, and things are
more likely than not to go blooey.
And then, there's wide variety in function linkage mechanisms.
Sometimes the caller takes care of both setting up and disposing
of the argument list, but sometimes the caller sets it up and the
callee disposes -- and if the callee does cleanup for a one-argument
list while the caller expected it to clean up two, it's blooey again.
Does it "pretty much" have to work? That sort of depends on
your definition of "pretty much," but my take would be "No."
Besides: Can you come up with a good reason to want to do such
a perverted thing in the first place? In what way does this merit
being called "a pretty important construct?"
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function casting - UB? nroberts <roberts.noah@gmail.com> - 2012-06-27 08:50 -0700
Re: Function casting - UB? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-27 12:49 -0400
Re: Function casting - UB? Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-06-27 17:54 +0000
Re: Function casting - UB? "io_x" <a@b.c.invalid> - 2012-06-28 18:58 +0200
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 10:37 -0700
Re: Function casting - UB? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-28 09:23 +0200
Re: Function casting - UB? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-28 09:27 +0200
Re: Function casting - UB? Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-28 12:03 +0200
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 11:10 -0700
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 11:03 -0700
Re: Function casting - UB? Les Cargill <lcargill99@comcast.com> - 2012-06-27 12:58 -0500
Re: Function casting - UB? Les Cargill <lcargill99@comcast.com> - 2012-06-27 12:59 -0500
Re: Function casting - UB? James Kuyper <jameskuyper@verizon.net> - 2012-06-27 14:45 -0400
Re: Function casting - UB? "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-27 14:55 -0700
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 17:44 -0700
Re: Function casting - UB? Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 16:46 -0700
Re: Function casting - UB? Barry Schwarz <schwarzb@dqel.com> - 2012-07-08 09:19 -0700
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-08 11:13 -0700
Re: Function casting - UB? Barry Schwarz <schwarzb@dqel.com> - 2012-07-08 15:24 -0700
Re: Function casting - UB? Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-08 18:12 -0700
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-10 10:40 -0700
Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-08 11:06 -0700
csiph-web