Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #176248
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Call to a function |
| Date | 2023-09-23 07:38 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86v8c1j6op.fsf@linuxsc.com> (permalink) |
| References | <call-20230922130647@ram.dialup.fu-berlin.de> <20230922081706.858@kylheku.com> <uekcfs$a1at$1@dont-email.me> <20230922091405.491@kylheku.com> |
Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-09-22, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
>
>> On 9/22/23 11:21, Kaz Kylheku wrote:
>>
>>> The conversion is not supported by ISO C, and so itself has
>>> undefined behavior:
>>>
>>> void (*fptr)(double) = (void (*)(double)) 1;
>>
>> "An integer may be converted to any pointer type. Except as previously
>> specified, the result is implementation-defined, might not be correctly
>> aligned, might not point to an entity of the referenced type, and might
>> be a trap representation." (6.3.2.3p5).
>>
>> The conversion itself is supported, it's just not guaranteed to result
>> in a pointer to "an entity of the referenced type". I would expect that
>> calling a function through a pointer that does not point at a function
>> would result in undefined behavior - but I think Stefan has a point -
>> I'm having trouble locating the part of the standard that explicitly
>> says so. I suppose that such a pointer could be considered to be a trap
>> representation. But even so, it's what you do with the resulting pointer
>> value that has undefined behavior, not the conversion itself.
>
> Thanks for the correction.
>
> You would think that the description of function call expressions covers
> this. E.g. in C99 we had the wording:
>
> If the function is defined with a type that is not compatible with the
> type (of the expression) pointed to by the expression that denotes the
> called function, the behavior is undefined.
>
> Whatever is at the address arising from the conversion of 1, if that is
> an address, it is not a function defined with a type pointed to by the
> pointer expression.
It _could_ be a function. The mapping from integers to pointers
is implementation defined. There is no reason an implementation
couldn't define the conversion of integers to function pointers
as the nth function in the overall program, with functions being
listed alphabetically. So if a program defines a function
void aardvark( double d ){ (void)&d; }
then that program, with this definition of main(),
int main( void ){
((void (*)( double )) 1) ( 3.14 );
return 0;
}
could be a well-defined (as well as well-formed) program for that
implementation.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Call to a function Kaz Kylheku <864-117-4973@kylheku.com> - 2023-09-22 15:21 +0000
Re: Call to a function James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-09-22 11:39 -0400
Re: Call to a function Kaz Kylheku <864-117-4973@kylheku.com> - 2023-09-22 16:47 +0000
Re: Call to a function James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-09-23 01:26 -0400
Re: Call to a function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-09-23 07:38 -0700
Re: Call to a function Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-09-22 11:54 -0700
Re: Call to a function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-03 06:34 -0700
Re: Call to a function Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-03 15:13 -0700
Re: Call to a function Kaz Kylheku <864-117-4973@kylheku.com> - 2023-10-04 01:52 +0000
Re: Call to a function Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-03 19:13 -0700
Re: Call to a function "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-03 20:41 -0700
csiph-web