A
amit.codename13
does defining a function with the same name as one of the standard
library functions invoke undefined behavior?
library functions invoke undefined behavior?
does defining a function with the same name as one of the standard
library functions invoke undefined behavior?
Eric Sosman said:Yes, unless the new function is `static' and none of the
headers that declare the Standard's function is included.
Yes, unless the new function is `static' and none of the
headers that declare the Standard's function is included.
Richard Heathfield said:osmium said:
In that circumstance, whatever behaviour the implementation decides
on is acceptable, so it's hard to see how you drew that conclusion.
osmium said:I have no idea what "that circumstance" is, nevertheless I will respond to
the last part of your sentence.
I drew the conclusion that the compiler was not independent of the libraries
from the statement that making them invisible is not enough. You must also
make the user defined function static. Therefore the compiler must know the
name of a set of "special" functions.
It seems that the actual problem
might be in the linker,
but the effect on the programmer is identical. It
is customary, as you know, to use "compiler" as shorthand for "the
implementation" or whatever.
int putchar(int c);
static void printf(char * str)
{
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
}
int main()
{
printf("Hi");
return 0;
}
Is the code above strictly conforming code?
osmium said:What a weird rule! That says the compiler is not independent of the
libraries as is sometimes claimed on this newsgroup.
You misunderstand the import of "undefined behavior". It does not
impose requirements on the compiler, but on the user. The compiler is
not required to notice that the function has the same name.
The linker is part of the implementation; it's only the implementation
as a whole that is governed by the C standard. The problem that the
linker may have is precisely the problem that is allowed by the fact
the standard says that the behavior is undefined.
That custom hides the fact that the compiler is generally not the
complete implementation. It's not possible to completely implement the
C language on most computer systems without combining features provide
by the compiler, the linker, the operating system, and the hardware
that they are attached to.
osmium said:What a weird rule!
That says the compiler is not independent of the
libraries as is sometimes claimed on this newsgroup.
int putchar(int c);
static void printf(char * str)
{
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
}
int main()
{
printf("Hi");
return 0;
}
Is the code above strictly conforming code?
osmium said:What a weird rule! That says the compiler is not independent of the
libraries as is sometimes claimed on this newsgroup.
osmium said:I guess I didn't realize that it was impossible to do it the way I
would like to see it. I can see the difficulty if the
implementation had to use a linker, as comes with a main frame. I
thought with PCs there was a new era, I guess that's where I went
wrong.
Keith Thompson said:Every C implementation I've ever seen requires a linker.
osmium said:What a weird rule! That says the compiler is not independent
of the libraries as is sometimes claimed on this newsgroup.
What a weird rule! That says the compiler is not independent of the
libraries as is sometimes claimed on this newsgroup.
osmium said:[... about replacing Standard library functions ...]
This may not be a very real problem for 99+% of the programs written, but
still, it IS a rule. I wrote a personal version of a sin(x) function
ignoring the "static" rule and it worked fine in my environment.
[...]
int putchar(int c);static void printf(char * str)
{
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
}int main()
{
printf("Hi");
return 0;
}Is the code above strictly conforming code?
Almost. I see two or three subtle problems.
First, "int main()" should be "int main(void)". (I've argued at some
length that an implementation needn't support "int main()" without the
void keyword. I don't want to re-hash the argument; I can try to find
a message-id if anyone is curious.) This is extremely unlikely to be
a problem in practice, but the question was whether the code is
strictly conforming, so a pedantic answer is warranted.
Second, the output is not terminated with a new-line. C99 7.19.2p2,
discussing text streams, says:
Whether the last line requires a terminating new-line character is
implementation-defined.
If an implementation does require a terminating new-line, and the
program doesn't provide one, then the behavior is undefined (simply
because the standard doesn't define the behavior).
Third, it's always possible for an input or output operation to fail.
putchar('H') can either write the character 'H' to stdout or fail and
return EOF. When this is taken into account, no program that performs
input or output can be strictly conforming, which probably was not the
intent.
The first two issues are easily corrected, and the third can be
ignored, or at least handwaved away, unless you want to be *really*
pedantic.
In particular, declaring "int putchar(int c)" rather than using
"#include <stdio.h>", and declaring your own static function called
"printf", are not problems as far as strict conformance is concerned.
Of course they're both horribly bad style, but that wasn't the
question.
Incidentally, gcc gives me a warning:
c.c:3: warning: conflicting types for built-in function 'printf'
That's ok; compilers can warn about anything they like, as long as
they produce at least one diagnostic for any translation unit
containing a syntax error, constraint violation, or #error directive.
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"(I've argued at some
length that an implementation needn't support "int main()" without the
void keyword. I don't want to re-hash the argument; I can try to find
a message-id if anyone is curious.)
[...]
int putchar(int c);static void printf(char * str)
{
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
}int main()
{
printf("Hi");
return 0;
}Is the code above strictly conforming code?
Almost. I see two or three subtle problems.
First, "int main()" should be "int main(void)". (I've argued at some
length that an implementation needn't support "int main()" without the
void keyword. I don't want to re-hash the argument; I can try to find
a message-id if anyone is curious.) This is extremely unlikely to be
a problem in practice, but the question was whether the code is
strictly conforming, so a pedantic answer is warranted.
Second, the output is not terminated with a new-line. C99 7.19.2p2,
discussing text streams, says:
Whether the last line requires a terminating new-line character is
implementation-defined.
If an implementation does require a terminating new-line, and the
program doesn't provide one, then the behavior is undefined (simply
because the standard doesn't define the behavior).
Third, it's always possible for an input or output operation to fail.
putchar('H') can either write the character 'H' to stdout or fail and
return EOF. When this is taken into account, no program that performs
input or output can be strictly conforming, which probably was not the
intent.
The first two issues are easily corrected, and the third can be
ignored, or at least handwaved away, unless you want to be *really*
pedantic.
In particular, declaring "int putchar(int c)" rather than using
"#include <stdio.h>", and declaring your own static function called
"printf", are not problems as far as strict conformance is concerned.
Of course they're both horribly bad style, but that wasn't the
question.
Incidentally, gcc gives me a warning:
c.c:3: warning: conflicting types for built-in function 'printf'
That's ok; compilers can warn about anything they like, as long as
they produce at least one diagnostic for any translation unit
containing a syntax error, constraint violation, or #error directive.
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"(I've argued at some
length that an implementation needn't support "int main()" without the
void keyword. I don't want to re-hash the argument; I can try to find
a message-id if anyone is curious.)
i am indeed curious.
well may be not worth a discussion but how did you actually know that
my program ended without a terminating new-line character in my post.
Thanks for answering.
i really wanted pedantic answers...
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.