Linux MIPS Architecture development
 help / color / mirror / Atom feed
* Function pointers and #defines
@ 2002-05-30 19:32 Justin Carlson
  2002-05-30 19:50 ` Daniel Jacobowitz
  0 siblings, 1 reply; 6+ messages in thread
From: Justin Carlson @ 2002-05-30 19:32 UTC (permalink / raw)
  To: linux-mips

A fair number of places in the headers, we have stuff like this:

void (*_some_fn)(int arg1, int arg2);
#define some_fn(arg1, arg2) _some_fn(arg1, arg2)

Why do we do this, as opposed to:

void (*some_fn)(int arg1, int arg2);

Both syntaxes result in being able to say

some_fn(1, 2);

but the latter is both clearer and shorter.  Is there some deep,
mystical C reason that we use the former, or did someone do it that way
a long time ago and no one has changed it?

-Justin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function pointers and #defines
  2002-05-30 19:32 Function pointers and #defines Justin Carlson
@ 2002-05-30 19:50 ` Daniel Jacobowitz
  2002-05-30 19:58   ` Justin Carlson
  2002-05-30 21:18   ` Kevin D. Kissell
  0 siblings, 2 replies; 6+ messages in thread
From: Daniel Jacobowitz @ 2002-05-30 19:50 UTC (permalink / raw)
  To: Justin Carlson; +Cc: linux-mips

On Thu, May 30, 2002 at 12:32:47PM -0700, Justin Carlson wrote:
> A fair number of places in the headers, we have stuff like this:
> 
> void (*_some_fn)(int arg1, int arg2);
> #define some_fn(arg1, arg2) _some_fn(arg1, arg2)
> 
> Why do we do this, as opposed to:
> 
> void (*some_fn)(int arg1, int arg2);
> 
> Both syntaxes result in being able to say
> 
> some_fn(1, 2);
> 
> but the latter is both clearer and shorter.  Is there some deep,
> mystical C reason that we use the former, or did someone do it that way
> a long time ago and no one has changed it?

At a guess, this prevents taking the address of the function
unintentionally...

-- 
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function pointers and #defines
  2002-05-30 19:50 ` Daniel Jacobowitz
@ 2002-05-30 19:58   ` Justin Carlson
  2002-05-30 21:18   ` Kevin D. Kissell
  1 sibling, 0 replies; 6+ messages in thread
From: Justin Carlson @ 2002-05-30 19:58 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: linux-mips

On Thu, 2002-05-30 at 12:50, Daniel Jacobowitz wrote:
> > but the latter is both clearer and shorter.  Is there some deep,
> > mystical C reason that we use the former, or did someone do it that way
> > a long time ago and no one has changed it?
> 
> At a guess, this prevents taking the address of the function
> unintentionally...

But if you're writing code in such a way that the compiler type checking
doesn't flag this, you deserve what you get.  (IMHO, of course.  :)

-Justin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function pointers and #defines
  2002-05-30 19:50 ` Daniel Jacobowitz
  2002-05-30 19:58   ` Justin Carlson
@ 2002-05-30 21:18   ` Kevin D. Kissell
  2002-05-30 21:18     ` Kevin D. Kissell
  2002-05-31  9:15     ` Gleb O. Raiko
  1 sibling, 2 replies; 6+ messages in thread
From: Kevin D. Kissell @ 2002-05-30 21:18 UTC (permalink / raw)
  To: Daniel Jacobowitz, Justin Carlson; +Cc: linux-mips

From: "Daniel Jacobowitz" <dan@debian.org>
> On Thu, May 30, 2002 at 12:32:47PM -0700, Justin Carlson wrote:
> > A fair number of places in the headers, we have stuff like this:
> > 
> > void (*_some_fn)(int arg1, int arg2);
> > #define some_fn(arg1, arg2) _some_fn(arg1, arg2)
> > 
> > Why do we do this, as opposed to:
> > 
> > void (*some_fn)(int arg1, int arg2);
> > 
> > Both syntaxes result in being able to say
> > 
> > some_fn(1, 2);
> > 
> > but the latter is both clearer and shorter.  Is there some deep,
> > mystical C reason that we use the former, or did someone do it that way
> > a long time ago and no one has changed it?
> 
> At a guess, this prevents taking the address of the function
> unintentionally...

More likely, some ancient early version of the code was
written with a single global function, some_fn(), and it
was easier to override it with a pointer indirection in
the header than to hunt down and change all invocations.
Sometimes that's good software engineering.  Sometimes
it's just laziness...

            Kevin K.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function pointers and #defines
  2002-05-30 21:18   ` Kevin D. Kissell
@ 2002-05-30 21:18     ` Kevin D. Kissell
  2002-05-31  9:15     ` Gleb O. Raiko
  1 sibling, 0 replies; 6+ messages in thread
From: Kevin D. Kissell @ 2002-05-30 21:18 UTC (permalink / raw)
  To: Daniel Jacobowitz, Justin Carlson; +Cc: linux-mips

From: "Daniel Jacobowitz" <dan@debian.org>
> On Thu, May 30, 2002 at 12:32:47PM -0700, Justin Carlson wrote:
> > A fair number of places in the headers, we have stuff like this:
> > 
> > void (*_some_fn)(int arg1, int arg2);
> > #define some_fn(arg1, arg2) _some_fn(arg1, arg2)
> > 
> > Why do we do this, as opposed to:
> > 
> > void (*some_fn)(int arg1, int arg2);
> > 
> > Both syntaxes result in being able to say
> > 
> > some_fn(1, 2);
> > 
> > but the latter is both clearer and shorter.  Is there some deep,
> > mystical C reason that we use the former, or did someone do it that way
> > a long time ago and no one has changed it?
> 
> At a guess, this prevents taking the address of the function
> unintentionally...

More likely, some ancient early version of the code was
written with a single global function, some_fn(), and it
was easier to override it with a pointer indirection in
the header than to hunt down and change all invocations.
Sometimes that's good software engineering.  Sometimes
it's just laziness...

            Kevin K.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function pointers and #defines
  2002-05-30 21:18   ` Kevin D. Kissell
  2002-05-30 21:18     ` Kevin D. Kissell
@ 2002-05-31  9:15     ` Gleb O. Raiko
  1 sibling, 0 replies; 6+ messages in thread
From: Gleb O. Raiko @ 2002-05-31  9:15 UTC (permalink / raw)
  To: Kevin D. Kissell; +Cc: Daniel Jacobowitz, Justin Carlson, linux-mips

"Kevin D. Kissell" wrote:
> 
> From: "Daniel Jacobowitz" <dan@debian.org>
> > On Thu, May 30, 2002 at 12:32:47PM -0700, Justin Carlson wrote:
> > > A fair number of places in the headers, we have stuff like this:
> > >
> > > void (*_some_fn)(int arg1, int arg2);
> > > #define some_fn(arg1, arg2) _some_fn(arg1, arg2)
> > >
> > > Why do we do this, as opposed to:
> > >
> > > void (*some_fn)(int arg1, int arg2);
> > >
> > > Both syntaxes result in being able to say
> > >
> > > some_fn(1, 2);
> > >
> > > but the latter is both clearer and shorter.  Is there some deep,
> > > mystical C reason that we use the former, or did someone do it that way
> > > a long time ago and no one has changed it?
> >
> > At a guess, this prevents taking the address of the function
> > unintentionally...
> 
> More likely, some ancient early version of the code was
> written with a single global function, some_fn(), and it
> was easier to override it with a pointer indirection in
> the header than to hunt down and change all invocations.
> Sometimes that's good software engineering.  Sometimes
> it's just laziness...
> 
>             Kevin K.

Just remove the declaration, compile, and look at the code generated.
So, #define is just a safety belt.

Regards,
Gleb.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-05-31  9:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-05-30 19:32 Function pointers and #defines Justin Carlson
2002-05-30 19:50 ` Daniel Jacobowitz
2002-05-30 19:58   ` Justin Carlson
2002-05-30 21:18   ` Kevin D. Kissell
2002-05-30 21:18     ` Kevin D. Kissell
2002-05-31  9:15     ` Gleb O. Raiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox