By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

c standard

P: n/a
Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?

Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?

Thanks in advance,
Florian
Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...
Because it's only declared there.
when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?
You haven't.
Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?
You've obviously compiled this as C++, where leaving out the name of an
unused argument is legal.
Thanks in advance,
Florian


HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2

P: n/a


theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.
That kind of message probably comes from the linker,
not from the compiler. When the linker sees that your
definition of sin() already satisfies the reference in
main(), it does not try to import another definition of
sin() from the library. (By the way, this is just "the
way many implementations work;" it is not a behavior
guaranteed by the C Standard.)
Why is it possible to overwrite the definition of sin,
is this part of the standard?
Providing your own version of a library function
causes undefined behavior. The compiler is not required
to issue a diagnostic, but the program is not required
to behave as you intend.
Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?


No, it is not correct C and the compiler should have
complained. However, I understand C++ allows this so a
C++ compiler would not complain. Are you sure you are
using a C compiler and not a C++ compiler?

--
Er*********@sun.com

Nov 14 '05 #3

P: n/a
In article <24**************************@posting.google.com >,
theoderich <e0******@student.tuwien.ac.at> wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.
Because it isn't actually defined in <math.h>, only declared; the header
has[1] something like:
--------
double sin(double);
--------
This means "There's a function called sin, that takes a double and
returns a double, and it's defined somewhere else". When the compiler
sees your definition it assumes that this is the "somewhere else".
Why is it possible to overwrite the definition of sin,
is this part of the standard?
No, sort of.
By defining a function with the same name as a standard library function,
you're invoking undefined behavior, which means that the compiler is
no longer bound by the requirements the language definition places on
it (because your code doesn't conform to the requirements the language
definition places on _it_); it's allowed to do whatever the implementor
wants it to do in this case (usually either "whatever's convenient"
or "whatever this other standard that does cover this case defines",
but things like "make demons fly out of the programmers nose" aren't
forbidden, only extremely difficult to implement), and doesn't need to
warn you about it.

What I suspect GCC is actually doing is not linking with the math library
at all and not whining about not being able to find it because you gave it
a definition of the only function you use. What I suspect it would have
done if you told it to link the math library is let your sin() override
the one in the library and call the one you defined anyways. The details
of exactly what it's doing and why are beyond the scope of comp.lang.c,
but if you really want to know a newsgroup that discusses GCC, or possibly
one that discusses unix programming (I'm not sure whether this is a GCC
thing or a unix thing), would be able to answer questions about that.

Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?


In C++, yes. In C, no. I suspect you told GCC that it was C++ that it
was compiling and not C. (It usually makes this decision based on the
extension of the file, unless you use appropriate arguments to force it
to do otherwise - the name of the compiler executable you invoke doesn't
affect the language it compiles.)
dave

[1] Actually, to be pedantically correct, this should say "#including
the header is the equivalent of inserting something that has"; the
header need not be a real entity that actually contains anything -
it's perfectly valid for the compiler to interpret "#include <math.h>"
to mean "Copy the type declarations, function declarations, and macro
definitions from table 7-12 into the live-symbols table" and not have
the header exist as anything other than a pre-digested symbol table.
But if you catch us in between the pedantic nitpicking we like to do,
most of us will admit that almost all (all?) real implementations
actually have a file somewhere that they read instead of doing this.

--
Dave Vandervies dj******@csclub.uwaterloo.ca
The fact that you don't understand it doesn't mean it's wrong, and the
fact that you understand it doesn't mean it's right.
--Joona I Palaste in comp.lang.c
Nov 14 '05 #4

P: n/a
On Wed, 02 Mar 2005 08:34:34 -0800, theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.


have you tried adding -lmath, thus linking the math library? The linker
should complain then.

Till
--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Nov 14 '05 #5

P: n/a
theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double) {
return 1;
}

int main() {
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi. cat main.cc #include <math.h>

double sin(double) {
return 1;
}

int main(int argc, char* argv[]) {
sin(1);
return 1;
}
g++ -Wall -ansi -pedantic -o main main.cc main.cc: In function `double sin(double)':
main.cc:3: error: declaration of `double sin(double)' \
throws different exceptions
/usr/include/bits/mathcalls.h:66: error: \
than previous declaration `double sin(double) throw ()'
main.cc: In function `int main(int, char**)':
main.cc:8: warning: statement has no effect g++ --version g++ (GCC) 3.4.1
Why is it possible to overwrite the definition of sin,
is this part of the standard?
Function sin(double) is *declared* not *defined* in math.h
It is *defined* in libm.a
There is *no* indication that you even attempted
to link in the math library and, even if you had,
the link editor would not even have bothered to search it
because you have already provided a definition for sin(double).
Secondly, the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code? cat main.cc #include <cmath>
#include <iostream>

double sin(double x) throw() {
return x;
}

int main(int argc, char* argv[]) {
std::cout << "sin(1) = " << sin(1) << std::endl;
return 1;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

sin(1) = 1
Nov 14 '05 #6

P: n/a
"E. Robert Tisdale" wrote:
.... snip ...
> g++ -Wall -ansi -pedantic -o main main.cc

main.cc: In function `double sin(double)':
main.cc:3: error: declaration of `double sin(double)' \


Pay no attention to the Trollsdale fulminations. This is typical
trolling, as he uses a C++ compiler to annoy and confuse C
newbies. The c++ group is down the hall, and they discuss and use
a different language.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7

P: n/a
theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double) {
return 1;
}

int main() {
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?


The people in this newsgroup are such god damned hyppocrites its
unbelievable. If they were at all consistent there should be like 10
people responding to you with some invective about how gcc is a
*particular* compiler and therefore off topic in this newsgroup. If
you wanted to see this amusing response you should have tried to ask
the question with MSVC, or Turbo C.

In any event, I would say this is probably just a deviation from the
standard that gcc supports, the -ansi notwithstanding, and its probably
a very good idea that it does. Being able to override the underlying
library is probably the only way some heapcheckers could ever possibly
work without hacking on your installed libraries directly, for example.

I think the reason the standard says you just can't do this is because
its possible for a compiler to support some function calls by directly
inlining their contents according to their assumed implementation. In
fact I am pretty sure gcc does this, with the right command line
switches for some functions on some platforms (in fact the trig
functions on x86 looks like good candidates). You should try
experimenting by defining some of these functions in *other* files,
compile with these inlining flags turned on, and see what happens when
you link.

---
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 14 '05 #8

P: n/a
we******@gmail.com wrote:
theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double) {
return 1;
}


The people in this newsgroup are such god damned hyppocrites its
unbelievable. If they were at all consistent there should be like 10
people responding to you with some invective about how gcc is a
*particular* compiler and therefore off topic in this newsgroup. If
you wanted to see this amusing response you should have tried to ask
the question with MSVC, or Turbo C.


You complain when people say "off topic" and you complain when
people say "on topic". When do you stop complaining?

BTW, stop lumping everyone into the same category, it is
offensive.

Finally, the question of whether you can have your own
function called 'sin' is certainly on-topic. Just because
the OP has gcc doesn't make it off-topic.

Nov 14 '05 #9

P: n/a
>theoderich wrote:
Why I don't get any warnings [for code that defines its own sin()
function] when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?

In article <11**********************@z14g2000cwz.googlegroups .com>
<we******@gmail.com> wrote:The people in this newsgroup are such god damned hyppocrites its
unbelievable. If they were at all consistent there should be like 10
people responding to you with some invective about how gcc is a
*particular* compiler and therefore off topic in this newsgroup.
While GCC particulars *are* off topic, the question above is not.
If I may rephrase it slightly, the question asks:

Is a given compiler conforming, or non-conforming, when
it fails to emit a diagnostic for this code?

Dave Vandervies has already given the correct answer: it is
conforming. The effect of the code is undefined, and no diagnostic
is required.
In any event, I would say this is probably just a deviation from the
standard that gcc supports, the -ansi notwithstanding ...


It might be nice if gcc would emit a warning by default for all
the Standard C functions, but it is not a "deviation from the
standard". (Of course, those of us who act as implementors now
and then would want a way to *stop* the warning, because we like
to write our implementations of Standard C Library functions in C
when we can, and we somtimes use gcc to compile them.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

P: n/a
we******@gmail.com wrote:
theoderich wrote:
Following is possible with gcc and g++:

#include <math.h>

double sin(double) {
return 1;
}

int main() {
sin(1);
return 1;
}

Why I don't get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?


The people in this newsgroup are such god damned hyppocrites its
unbelievable. If they were at all consistent there should be like
10 people responding to you with some invective about how gcc is a
*particular* compiler and therefore off topic in this newsgroup.
If you wanted to see this amusing response you should have tried
to ask the question with MSVC, or Turbo C.

In any event, I would say this is probably just a deviation from
the standard that gcc supports, the -ansi notwithstanding, and its
probably a very good idea that it does. Being able to override
the underlying library is probably the only way some heapcheckers
could ever possibly work without hacking on your installed
libraries directly, for example.


You do seem to be unalterably inclined to avoid portable coding
(from another thread and group). If you had followed along, rather
than having a hissy-fit, you would have seen explanations of why
the behaviour adhered to the standard. You are just exhibiting
your own sloppiness here. This is not good, especially with such a
touchy language as C.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #11

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
<we******@gmail.com> wrote:
theoderich wrote:
Why is it possible to overwrite the definition of sin,
is this part of the standard?


The people in this newsgroup are such god damned hyppocrites its
unbelievable. If they were at all consistent there should be like 10
people responding to you with some invective about how gcc is a
*particular* compiler and therefore off topic in this newsgroup.


invective, noun:
1 : an abusive expression or speech
2 : insulting or abusive language

Nope, the only time I see that is when people like you complain about the
fact that we discuss C here and redirect discussion about the various
kinds of not-quite-C that people ask about to other newsgroups that
discuss that particular kind of not-quite-C.
If
you wanted to see this amusing response you should have tried to ask
the question with MSVC, or Turbo C.


What part of this:

}The details of exactly what it's doing and why are beyond the scope of
}comp.lang.c, but if you really want to know a newsgroup that discusses
}GCC, or possibly one that discusses unix programming (I'm not sure
}whether this is a GCC thing or a unix thing), would be able to answer
}questions about that.

is inconsistent with similar responses given to posters asking about
something particular to MSVC or Turbo C?
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
although come to think of it if you didn't enjoy sounding stupid you wouldn't
be talking about things you don't understand. never mind...
--David C. Ullrich roasts a troll in comp.lang.c++
Nov 14 '05 #12

P: n/a
In article <11**********************@o13g2000cwo.googlegroups .com>,
Old Wolf <ol*****@inspire.net.nz> wrote:
Finally, the question of whether you can have your own
function called 'sin' is certainly on-topic. Just because
the OP has gcc doesn't make it off-topic.


It's also worth noting that the only correct answers the OP got were
the ones that addressed it as a C question and not as a GCC question.

There's a reason we tell people there are better places to ask about
their compiler than a newsgroup that discusses the language it compiles...
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
although come to think of it if you didn't enjoy sounding stupid you wouldn't
be talking about things you don't understand. never mind...
--David C. Ullrich roasts a troll in comp.lang.c++
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.