Mail Archives: geda-help/2012/01/23/16:02:51
On Mon, Jan 23, 2012 at 12:13:55PM -0800, Colin D Bennett wrote:
> On Sun, 22 Jan 2012 15:43:06 -0800
> Andrew Poelstra <asp11 AT sfu DOT ca> wrote:
>
> > Function pointers may not be casted to function pointers of
> > different types. (This is very irritating, but it's what the
> > standard says.)
>
> I don't see how it's irritating. It's just the only sane thing to
> do. How is a function pointer usefull unless you know the argument
> types?
>
Because a lot of times you have unnecessary arguments -- i.e., suppose
you had an object that looked like
typedef struct stream_t {
/* some public data */
int (*open) (struct stream_t *, int mode);
/* some other functions */
} stream_t;
And you had some type of stream for which 'mode' does not make sense,
say, because both reading and writing are always allowed. It would be
nice to be able to do something like
static int _my_stream_open (struct stream_t *s)
{
/* opening code */
return 1;
}
stream_t *my_stream_new ()
{
my_stream_priv *priv = malloc (sizeof *priv);
stream_t *rv = (stream_t *) priv;
rv->open = _my_stream_open;
/* set other functions */
return rv;
}
As it stands now, _my_stream_open would need to accept a dummy 'mode'
varible, which would then require a "(void) mode". (Without this, gcc
complains about unused variables. WITH it, splint complains.)
Plus, it would be nice if I could assign functions with void* arguments
or return value, to a function pointer expecting different pointer types.
(Often one has to write trivial wrapper functions for things like this.)
> > Calling a function with an invalid declaration is
> > even worse, since it has the same effect without warning the
> > compiler. For example, the author does the following:
> >
> > char *malloc();
> > char *s = malloc(BUFSIZ);
> >
> > which tells the compiler that malloc() is a function declared as
> >
> > char *malloc (int);
>
> Doesn't the empty argument list in the declaration of malloc()
> mean the number and type of arguments is completely unspecified,
> equivalent to:
>
> char *malloc(...);
>
> and that means you could write the following code
>
> malloc("Hello", 3.54f);
> malloc();
>
Not quite. The compiler takes the types of the /first/ invocation of
malloc(), and uses that as a defacto signature.
--
Andrew Poelstra
Email: asp11 at sfu.ca OR apoelstra at wpsoftware.net
Web: http://www.wpsoftware.net/andrew
"I don't understand. Are you saying dualism is always good, or always bad?"
- Raw text -