undefine for functions

P

Pietro Cerutti

Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?

For example, let's say that I need a few declarations from stdio.h but
want to define my own printf:

#include <stdio.h>
#undefine-or-similar printf
#include "printf.h"

int main(void)
{
printf(123);
return (0);
}

Thank you.
 
I

Ian Collins

Pietro said:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?

For example, let's say that I need a few declarations from stdio.h but
want to define my own printf:

#include <stdio.h>
#undefine-or-similar printf
#include "printf.h"

int main(void)
{
printf(123);
return (0);
}
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.

The best you can do is something gross like:

#define puts _puts
#include <stdio.h>
#undef puts

void puts( int n ) {}

int main(void) {
puts( 42 );
}
 
N

Nick Keighley

is there a mean to undefine a function, in a similar way as you can
undefine macros?
no.


For example, let's say that I need a few declarations from stdio.h but
want to define my own printf:

don't do that

#include <stdio.h>
#undefine-or-similar printf
#include "printf.h"

int main(void)
{
printf(123);
return (0);

}

replace printf() with your own version (eg. xprintf())
inside that either use your new code or call printf().
You have to do a global substitute on every printf(),
which isn't hard, and you've added an extra function
call overhead to every printf() call. But then printf()
isn't that quick anyway.
 
P

Pietro Cerutti

Ian said:
Pietro said:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?
[snip]
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.

The best you can do is something gross like:

#define puts _puts
#include <stdio.h>
#undef puts

void puts( int n ) {}

int main(void) {
puts( 42 );
}

That achieves what I was looking for. Basically, it replaces the
occurrences of "puts" in stdio.h with "_puts", which isn't a function
name. After that, I'm able to declare and define my own version of puts.

Anyway, why I don't get warnings about puts being defined elsewhere
(libc.so)?

Great, thank you!
 
I

Ian Collins

Pietro said:
Ian said:
Pietro said:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?
[snip]
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.

The best you can do is something gross like:

#define puts _puts
#include <stdio.h>
#undef puts

void puts( int n ) {}

int main(void) {
puts( 42 );
}

That achieves what I was looking for. Basically, it replaces the
occurrences of "puts" in stdio.h with "_puts", which isn't a function
name. After that, I'm able to declare and define my own version of puts.

Anyway, why I don't get warnings about puts being defined elsewhere
(libc.so)?
Well I said it was gross and I should have added dangerous and not
portable as well!

Your system uses some form of week symbol for standard library functions
and your function replaced it.
 
P

Pietro Cerutti

Ian said:
Pietro said:
Ian said:
Pietro Cerutti wrote:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros? [snip]
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.

The best you can do is something gross like:

#define puts _puts
#include <stdio.h>
#undef puts

void puts( int n ) {}

int main(void) {
puts( 42 );
}
That achieves what I was looking for. Basically, it replaces the
occurrences of "puts" in stdio.h with "_puts", which isn't a function
name. After that, I'm able to declare and define my own version of puts.

Anyway, why I don't get warnings about puts being defined elsewhere
(libc.so)?
Well I said it was gross and I should have added dangerous and not
portable as well!

Your system uses some form of week symbol for standard library functions
and your function replaced it.

Not understood, sorry.
Could you elaborate it or give further directions, please?

Thank you!
 
K

Kenneth Brody

Ian said:
Pietro said:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?

For example, let's say that I need a few declarations from stdio.h but
want to define my own printf:
[...]
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.

The best you can do is something gross like:

#define puts _puts
#include <stdio.h>
#undef puts

void puts( int n ) {}

int main(void) {
puts( 42 );
}

Or:

#include <stdio.h>
#undef puts
#define puts MyPuts
...

Besides, doesn't the standard forbid you from replacing standard
library functions? For example, I have seen compilers which can
inline some standard functions, like strcpy(), so even if you
had a function called strcpy(), it wouldn't be called.

BTW, is it legal to #undef something that hasn't been #define'd?
Does the above need a #ifdef around the #undef? (My compiler
doesn't complain about it.)

Hold on... I knew I downloaded n1124.pdf for a reason...

Don't mind me. Just go about your things while I have this
conversation with myself.

6.10.3.5p2

(#undef)

It is ignored if the specified identifier is not currently
defined as a macro name.

So, to answer my own question... Yes, it is perfectly legal to
#undef something which isn't #define'd already.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Kenneth said:
Ian said:
Pietro said:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?

For example, let's say that I need a few declarations from stdio.h but
want to define my own printf:
[...]
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.

The best you can do is something gross like:

#define puts _puts
#include <stdio.h>
#undef puts

void puts( int n ) {}

int main(void) {
puts( 42 );
}

Or:

#include <stdio.h>
#undef puts
#define puts MyPuts
...

I think this is the best you can do, for all the best
definitions of "best." It's still not perfect. One problem
is that it doesn't affect puts() calls in modules you don't
have an opportunity to recompile, like those in libraries
you use only in pre-compiled form.

Another problem is illustrated by a conversation I had
with a colleague at a PPOE. He'd spent some time as a compiler
developer, and told me that the compiler he worked on replaced
`printf ("Hello, world!\n")' with `puts ("Hello, world")'. It
seems one of the SPEC benchmarks involved a lot of printf() calls
with no "%" directives, so this transformation got them a better
SPEC score ...

Anyhow, a question arises: He told me they replaced the
call to printf() with a call to puts(), but he didn't tell me
exactly how that call was expressed and implemented. Perhaps
the substitution occurred long after the preprocessor operated,
so the macro dodge above would fail to intercept the call. Or
perhaps the substitution *was* subject to macro replacement, in
which case the dodge above would wind up handling some printf()
calls in addition to the explicit puts() calls it was intended
to snaffle ... Either way, the "best you can do" macro dodge
is on thin ice.

Besides, a strict reading of 7.1.3p1 says `#undef puts'
and `#define puts ...' invoke undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top