473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

undefine for functions

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.
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 30 '07 #1
6 12190
Pietro Cerutti wrote:
is there a mean to undefine a function, in a similar way as you can
undefine macros?
No.

--
Chris "in denial" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Aug 30 '07 #2
Pietro Cerutti wrote:
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 );
}

--
Ian Collins.
Aug 30 '07 #3
Ian Collins wrote:
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)?

Great, thank you!

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 30 '07 #4
Pietro Cerutti wrote:
Ian Collins wrote:
>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.

--
Ian Collins.
Aug 30 '07 #5
Ian Collins wrote:
Pietro Cerutti wrote:
>Ian Collins wrote:
>>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!
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 30 '07 #6
Ian Collins wrote:
>
Pietro Cerutti wrote:
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:Th*************@gmail.com>
Aug 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
1
by: Rafal 'Raf256' Maj | last post by:
Hi, is there a commend to undefine all macros #define inside a file? in example: #define FOR(x) for (..............) #define MyMacro1(x,y) ........... // ... code ...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
5
by: Rocky86 | last post by:
hi people basically I am having a problem with the followinng code: $names=sizeof($temp); $report="total=$names&"; foreach($temp as $list) $report.="name".$names--."=".$list."&"; echo...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.