delorie.com/archives/browse.cgi   search  
Mail Archives: geda-help/2012/01/23/16:02:51

X-Authentication-Warning: delorie.com: mail set sender to geda-help-bounces using -f
X-Recipient: geda-help AT delorie DOT com
Date: Mon, 23 Jan 2012 12:57:25 -0800
From: Andrew Poelstra <asp11 AT sfu DOT ca>
To: geda-help AT delorie DOT com
Subject: Re: [geda-help] Help, how can I help?
Message-ID: <20120123205725.GA15392@malakian.lan>
References: <4F1A1196 DOT 20100 AT gmail DOT com>
<201201210150 DOT q0L1o04r012622 AT envy DOT delorie DOT com>
<4F1C643E DOT 7080503 AT gmail DOT com>
<CAPFCoithVSn-AOggs4nZscwYN+5ejH7Cb38VEHDHFzrVLY7Wxg AT mail DOT gmail DOT com>
<20120122234306 DOT GB13485 AT malakian DOT lan>
<20120123121355 DOT 0d3ea4bc AT svelte>
MIME-Version: 1.0
In-Reply-To: <20120123121355.0d3ea4bc@svelte>
User-Agent: Mutt/1.5.20 (2009-12-10)
Reply-To: geda-help AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: geda-help AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019