![](/i/fill.gif) |
![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Can somebody interpret this for me?
while (TRUE) { cont = (*cont)(); }
What on earth...?
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Invisible wrote:
> Can somebody interpret this for me?
>
> while (TRUE) { cont = (*cont)(); }
>
> What on earth...?
cont is a pointer to a function that is expected to return a pointer to
a function. The code here causes a series of functions to be called,
each one specifying another function to be called afterwards by
returning the address of the function to be called.
Regards,
John
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
>> Can somebody interpret this for me?
>>
>> while (TRUE) { cont = (*cont)(); }
>>
>> What on earth...?
>
> cont is a pointer to a function that is expected to return a pointer to
> a function. The code here causes a series of functions to be called,
> each one specifying another function to be called afterwards by
> returning the address of the function to be called.
Ah, so it *is* possible to read C? :-)
So cont points to a zero-argument function that returns a zero-argument
function? And this loop just calls it repeatedly?
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
John VanSickle a écrit :
> Invisible wrote:
>> Can somebody interpret this for me?
>>
>> while (TRUE) { cont = (*cont)(); }
>>
>> What on earth...?
>
> cont is a pointer to a function that is expected to return a pointer to
> a function. The code here causes a series of functions to be called,
> each one specifying another function to be called afterwards by
> returning the address of the function to be called.
>
I was thinking the same, but I can't seem to find the right type
definition to make it work without casting to and from void*... Which is
apparently illegal
(http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.8)
Here's what I ended up with:
typedef void* (*pfunc)();
void* test1()
{
return (void*)test1;
}
int main()
{
pfunc cont = test1;
while (true) { cont = (pfunc)(*cont)(); }
return 0;
}
I'd like to write:
typedef pfunc (*pfunc)();
pfunc test1()
{
return test1;
}
int main()
{
pfunc cont = test1;
while (true) { cont = (*cont)(); }
return 0;
}
But the typedef here does not seem to work. I can't say I blame the
compiler for choking on that one either :-)
I'm not too comfortable with function pointers though, so it's likely
that I'm missing something here... Any hints?
--
Vincent
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
>> cont is a pointer to a function that is expected to return a pointer
>> to a function. The code here causes a series of functions to be
>> called, each one specifying another function to be called afterwards
>> by returning the address of the function to be called.
>>
>
> I was thinking the same, but I can't seem to find the right type
> definition to make it work without casting to and from void*... Which is
> apparently illegal
This is example code, so it might not be literally runnable... (It might
be simplified somewhat.)
> I'm not too comfortable with function pointers though, so it's likely
> that I'm missing something here... Any hints?
Don't look at me! ;-)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Invisible a écrit :
>>> cont is a pointer to a function that is expected to return a pointer
>>> to a function. The code here causes a series of functions to be
>>> called, each one specifying another function to be called afterwards
>>> by returning the address of the function to be called.
>>>
>>
>> I was thinking the same, but I can't seem to find the right type
>> definition to make it work without casting to and from void*... Which
>> is apparently illegal
>
> This is example code, so it might not be literally runnable... (It might
> be simplified somewhat.)
>
Does it still provide the declaration of cont, by any chance?
--
Vincent
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
On Thu, 24 Jul 2008 14:24:51 +0200, Vincent Le Chevalier
<gal### [at] libertyallsurfsp amfr> wrote:
> I was thinking the same, but I can't seem to find the right type
> definition to make it work without casting to and from void*... Which is
> apparently illegal
> (http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.8)
It is illegal because a 'void *' is a data pointer, not a function pointer.
On a related note, we do not know if the original code was supposed to be
standards-compliant or not, nor if it was supposed to be C or C++.
> Here's what I ended up with:
[...snipped...]
The "right" way to do it, assuming C++:
http://www.gotw.ca/gotw/057.htm
--
FE
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Fredrik Eriksson wrote:
> On a related note, we do not know if the original code was supposed to
> be standards-compliant or not, nor if it was supposed to be C or C++.
Ment to be ANSI C (not C++).
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Invisible wrote:
> So cont points to a zero-argument function that returns a zero-argument
> function? And this loop just calls it repeatedly?
Assuming TRUE is a value that always returns non-zero, yes. The code itself
appears to implement a state machine.
Of course, this being C, 'TRUE' could be a macro that expands to a function
that returns a value and thus the loop need not be infinite. It could even
expand to a variable with a decrement or increment operator. Or really
anything - if the code were presented to someone as part of an computer
science exam, for example, they would probably be expected to pick that
possibility up. In the real world, no programmer would ever do something
that mean with a macro called TRUE. Right? :-)
-- Chris
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
>> So cont points to a zero-argument function that returns a zero-argument
>> function? And this loop just calls it repeatedly?
>
> Assuming TRUE is a value that always returns non-zero, yes. The code itself
> appears to implement a state machine.
More exactly: It's a workaround for C's lack of lack of GOTO.
[This fragment appears in the output of a compiler that's using C as
"portable assembly".]
> In the real world, no programmer would ever do something
> that mean with a macro called TRUE. Right? :-)
Hey, I found this cool site, it's called The Daily WTF... ;-)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |