🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

10 total messages Started by olson Tue, 12 Jul 1988 16:11
function returning pointer to itself
#5170
Author: olson
Date: Tue, 12 Jul 1988 16:11
14 lines
1016 bytes
Does the new C standard have a natural way to declare a
function that returns a pointer to itself
????

(if you are puzzled by the question, consider the problem 
 of writing a state machine where the states are indicated
 by functions.  To go from one state to another, a function
 exits, returning a pointer to the next function to be executed.
 In the C that I know, I know of no way to declare such functions.
 What does the new standard suggest?)

Todd Olson
olson@helios.tn.cornell.edu
Physics Dept/ Cornell Univ/ Ithaca NY 14853
Re: function returning pointer to itself
#5183
Author: olson
Date: Wed, 13 Jul 1988 06:41
80 lines
3798 bytes
In article <5485@batcomputer.tn.cornell.edu> olson@tcgould.tn.cornell.edu (olson) writes:
>Does the new C standard have a natural way to declare a
>function that returns a pointer to itself
>????

Thanks to all the people that replied to my question.
Everyone said effectively, "NO, this can not be done NATURALLY in C"
(which is the response I expected)
BUT you can mock it up by
  1) using type cast
  2) encapsulation in a data structure

That C can't do this naturally bugs me.
What do people think of the following proposal?

Define a new primative type  'self'   (not a very good name ...)
  self *p;	means  p points to something that 
                         points to something that ...
                   or  p points to itself
		       p->p->p->p->p->...
  self *f();	means  f is a function returning a pointer to a 
                              function returning a pointer to a ...
                    or f is a function returning a pointer to itself
		       f()->f()->f()->...
  self **f()	means  f is a function returning a pointer to a pointer to a
                              function returning a pointer to a pointer to a ...
                   or  ???
                       f()->p->f()->p->f()->p->...
  self *(*p)() 	means  p is a pointer to a function returning a pointer to a
                              pointer to a function returning a pointer to a ...
		   or  ???
		       p->f()->p->f()->p->f()->...
That is, 'self' sets up a definition that is "solved" by iteration
(but that is NOT how you implement it)
self *(*p)() =>  p=*(*p)() = *(*(*(*p)()))() = ...

Note that   self  must always involve pointers,  self p  is not allowed.

Further one could write
  p->f()->f()->f()->f()->...
as
 typedef self *state();
 state *p;
Without typedefs this would not be possible to express,
unless further syntax modifications were made, such as
  self *<*p>();		means p->f()->f()->f()->...
  self *<f()>;		means f()->p->p->p->...
where the <> indicate the part that is to be iterated on
ie    self *<f()>    =>    <f()>=*<f()>      rather than  f = *<f()>

SIDE ISSUE: (independent of self)
Note in the same way that it is impossible to declare a function
using the typedef
typedef int myfunc();
because 
myfunc f(){};   means   int  (f())(){};
which is illegal,
it is impossible to declare a function
state f(){};
This is something that bugs me too.
Would it screw up the intuitiveness of typedefs too much
to define  myfunc f(){}; to mean  int f(){}; 
since the only other meaning is illegal????????


COMMENTS
1) I don't see how 'self' would pose any implementation problems.
   (on the other hand I have never written a compiler, only thought about it)
2) There have been enough occasions where I could have used this primative
   that I would call it a useful feature.
3) It might be opening a pandora's box.  Consider, someone might say, well
   I want  f()->p->f()->p->p->f()->p->p->p->f()->...
   or one where the number of p's are the digits of PI.
   Well, what can I say.  These would be a lot harder to implement.
   I haven't been able to conceive of a use for them.
4) It is probably to late to add this primative to the new C standard.

Todd Olson
olson@helios.tn.cornell.edu
Physics Dept/ Cornell Univ/ Ithaca NY 14853
Re: function returning pointer to itself
#5184
Author: Jeff Siegal
Date: Wed, 13 Jul 1988 08:26
23 lines
1117 bytes
In article <5485@batcomputer.tn.cornell.edu> olson@tcgould.tn.cornell.edu (olson) writes:
>Does the new C standard have a natural way to declare a
>function that returns a pointer to itself
>????

I'm not sure about a natural way, but the following should suffice:

typedef void *(*NextStateFun)();

static void *state1();
static void *state2();
static void *state3();

void *state1() { return (void *)state2; }
void *state2() { return (void *)state3; }
void *state3() { return NULL; }
void * const initial_state = (void *)state1;
.
.
.
    void *state = initial_state;
  
    while (state) { state = (*(NextStateFun)state)(); }
Re: function returning pointer to itself
#5185
Author: Chris Torek
Date: Wed, 13 Jul 1988 13:04
18 lines
1179 bytes
In article <9667@eddie.MIT.EDU> jbs@fenchurch.MIT.EDU (Jeff Siegal) writes:
>... the following should suffice:
>
>typedef void *(*NextStateFun)();

No good: the dpANS does not say that data pointers are the same `size'
as function pointers, and indeed, on (e.g.) the Univac 1100 series, as
I understand it, data pointers are 2 words while function pointers are
18 words.  Hence stuffing a function pointer into a data pointer will
lose information.  The dpANS *does* guarantee that pointers to functions
are all the same `size' (I think), so:

	typedef void (*(*NextStateFun)())();

and go from there.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris
Re: function returning pointer to itself
#5176
Author: William E. Davidsen Jr
Date: Wed, 13 Jul 1988 14:21
55 lines
2112 bytes
In article <5485@batcomputer.tn.cornell.edu> olson@tcgould.tn.cornell.edu (olson) writes:
| Does the new C standard have a natural way to declare a
| function that returns a pointer to itself
| ????

  Three solutions come to mind. The first may only be used if all
procedures are global. This may not always be the case, as you may want
to return pointers to static functions.

int do_state_1(), do_state_2();
int (*foo[])() = {
	do_init(),
	do_state_1,
	do_state_2
};
int next_state = 0;
.
.
	next_state = (*foo[next_state])();

  This can either set the next_state as a global from the procedure, or
an int state may be returned.

method two:

void do_init();
void (*foo)() = do_init;
.
.
	(*foo)();

  And each procedure sets foo to the next state.

method three:

  This is close to what you want, but gives warnings on some compilers.
I would not use it personally for portability reasons.

int (*do_init)();
int (*next_state())();
.
	next_state = (int (*)()) (*next_state)();

  The assumption is that a pointer to a function returning any one thing
looks like a pointer to a function returning some other thing. I can't
think of any reason this wouldn't be true, but I don't trust it.

  I personally like method two best, then one. If you have some old
archives (ie. net.something) I posted my Turing machine in C using a
modified method one and two, where the pointer was pointing a a STATE
struct, gotten by an index.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me
Re: function returning pointer to itself
#5171
Author: Scott M. King
Date: Wed, 13 Jul 1988 23:05
14 lines
715 bytes
In article <5485@batcomputer.tn.cornell.edu> olson@tcgould.tn.cornell.edu (olson) writes:
>Does the new C standard have a natural way to declare a
>function that returns a pointer to itself

	void *(*
main() )()
{
	return main;
}

Ugly eh???
--

Scott M. King
Re: function returning pointer to itself
#5189
Author: Karl Heuer
Date: Thu, 14 Jul 1988 18:14
24 lines
1625 bytes
In article <11514@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>The first [of three solutions] may only be used if all procedures are global.

Not true; the same method works with static functions (provided you correctly
forward-declare them as static).

>... method three:
>This is close to what you want, but gives warnings on some compilers.
>I would not use it personally for portability reasons. ...
>The assumption is that a pointer to a function returning any one thing
>looks like a pointer to a function returning some other thing.  I can't
>think of any reason this wouldn't be true, but I don't trust it.

The dpANS guarantees that you can cast function pointers to other function
pointer types and back; as long as you use the "right" type to invoke it.  I
think there was at least one typo in your code fragment, though.  Try this:

	typedef void (*func_t)(void);	/* arbitrary function pointer type */
	func_t do_init(void), do_state_1(void);
	func_t (*next_state)(void) = do_init;

	next_state = (func_t (*)(void))(*next_state)();

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
Re: function returning pointer to itself
#5213
Author: Rick Coates
Date: Mon, 18 Jul 1988 21:57
46 lines
1548 bytes
In article <5485@batcomputer.tn.cornell.edu>, olson@batcomputer.tn.cornell.edu (olson) writes:
> Does the new C standard have a natural way to declare a
> function that returns a pointer to itself
> ????
> 
(deleted)
(several more articles, with obscure declartions)


Please forgive me if I'm missing something obvious here.....
(I used to be a hardware person - what can I say).

I just tried this out on the only dpANSI compiler I have available -
Microsoft C 5.1 for IBM pc's (this is ironic because I do a lot of work
on a Sun 4 - great machine but no ANSI C; I also work  on Masscomp machines,
which also have no ANSI compiler. When are the Big Boys (medium boys, anyway)
going to support ANSI C?).

This seems to work fine. Compiles with no errors or warnings.

Isn't this a reasonable use of void *   ???
It isn't even Ugly!
-------------program------------
void * test_func();

main()
{
    printf("test: %x\n",test_func);
}

void *
test_func()
{
    return test_func;
}
-------------results-------------

test: 24

---------------------------------

Rick Coates

tektronix!reed!percival!agora!rickc
OR
tektronix!sequent!islabs!ateq!rick
Pointer to void (was: function returning pointer to itself)
#5225
Author: Rick Coates
Date: Fri, 22 Jul 1988 15:52
22 lines
1147 bytes

I did not intend to appeal to authority with my comment about
Microsoft C (_especially_ Microsoft) in my previous posting - it
was just a comment.

I don't have a copy of the dpANSI standard - but I do
have the second edition of K&R. On page 199, they discuss void pointers
with no mention of restrictions about pointer to functions.
(Also a comment - I am _not_ saying that K&R are the authority).

Seems (offhand - I haven't spent years of my life thinking about this
issue) that a void pointer should be for any pointer, not just data.

Can someone elaborate on why ANSI does not allow void pointers for
functions?

Rick Coates

tektronix!reed!percival!agora!rickc
OR
tektronix!sequent!islabs!ateq!rick
Re: Pointer to void (was: function returning pointer to itself)
#5221
Author: T. William Wells
Date: Sun, 24 Jul 1988 15:51
18 lines
1340 bytes
In article <1066@agora.UUCP> rickc@agora.UUCP (Rick Coates) writes:
: Can someone elaborate on why ANSI does not allow void pointers for
: functions?

The basic reason is that data pointers and function pointers can
have very different sizes.  I have heard claims for function
pointers with 7x and 9x data pointer sizes.  (Anybody want to say
which machines?) And, of course, there is the IBM-PC, where
certain programming models may use far function pointers and near
data pointers (or v.v.), a difference of 2x.

Given this, it is desirable to have generic function and data
pointers not be commensurable in order to save memory for the
shorter data type.  Of course, this can be taken to the extreme
where there are no generic pointer types.

For one who needs a data structure which can hold both types of
pointers, use a union.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads