473,425 Members | 1,668 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,425 software developers and data experts.

c standard

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
12 1726
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


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
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
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
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
"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
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
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
>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 (40°39.22'N, 111°50.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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.)...
6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
29
by: David Eng | last post by:
In replying to P.J. Plauger (...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
24
by: noridotjabi | last post by:
Why isn't there a Graphical User Interface standard? Think about it for a second. You may say, well evey systems API for GUIs is differnt, but do take into acound: every operating system requires...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
1
by: manish deshpande | last post by:
Hi, When i'm installing MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm by the following command: rpm -i MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm the following error is being shown: ...
26
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? ...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.