- C: Using function-like macro instead of real function
- Posted by joshc on April 13th, 2005
So I am working on some code that interfaces with some code that
another group of folks is working on. We have an interface defined and
on my part I have to provide certain functions and declare those
functions in certain header files that we agreed upon.
The external group is including my header file with the function
declaration as appropriate in their source. In the interface many of
the functions we agreed to provide are of the following form:
/* prototype agreed upon in interface document */
int GetInputX(void);
My question is that if I define a function-like macro in the header
file I am providing to this external group as follows:
/* myfuncs.h */
extern int x;
#define GetInputX() x
/* end file */
Do you see any problem with doing this? THis is in a real-time embedded
environment. I guess one thing I see as a potential problem is that
there is nothing to stop anyone from including "myfuncs.h" and messing
with the variable 'x'.
This brings up a somewhat related question-could someone potentially
use the macro to assign to the variable 'x' as follows:
GetInputX() = 23;
This question I merely ask to satisfy my curiousity.
Thanks.
- Posted by joshc on April 13th, 2005
joshc wrote:
I guess I failed to realize that the other group went ahead and created
'myfuncs.h' which has the declarations for the functions I am to
implmenet and handed that on to me along with a relocatable object file
containing their portion of the code. Their code was compiled assuming
a real function GetInputX() so I guess if I had received their code as
source then the Macro approach would still work.
- Posted by Willem on April 13th, 2005
joshc wrote:
) My question is that if I define a function-like macro in the header
) file I am providing to this external group as follows:
)
) /* myfuncs.h */
)
) extern int x;
)
) #define GetInputX() x
)
) /* end file */
)
) Do you see any problem with doing this?
There's a simple solution to this:
/* myfuncs.h */
#ifdef OPTIMIZE
extern int x;
#define GetInputX() (x)
#else
int GetInputX(void);
#endif
/* myfuncs.c */
#include "myfuncs.h"
#ifndef OPTIMIZE
int GetInputX() { return x }
#endif
If everythink runs without problems without 'OPTIMIZE' then you can't
have any of the problems you mentioned when you compile with 'OPTIMIZE'.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
- Posted by pete on April 13th, 2005
joshc wrote:
This:
#define GetInputX() (x + 0)
turns GetInputX() into an rvalue,
which can only be read, but not written to.
/* BEGIN myfuncs.h */
#define GetInputX() (x + 0)
extern int x;
int (GetInputX)(void);
/* END myfuncs.h */
/* BEGIN myfuncs.c */
#include "myfuncs.h"
int (GetInputX)(void)
{
return x;
}
/* END myfuncs.c */
--
pete
- Posted by Peter Ammon on April 15th, 2005
Willem wrote:
But there are other problems you could have. To wit:
int (*func_ptr)(void) = GetInputX;
-Peter
--
Pull out a splinter to reply.
- Posted by beliavsky@aol.com on April 15th, 2005
There is a newsgroup comp.lang.c for questions specific to C.