c standard

T

theoderich

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
 
A

Artie Gold

theoderich said:
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
 
E

Eric Sosman

theoderich said:
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?
 
D

Dave Vandervies

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.
 
E

E. Robert Tisdale

theoderich said:
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
 
T

Till Crueger

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
 
C

CBFalconer

E. Robert Tisdale said:
.... snip ...

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.
 
W

websnarf

theoderich said:
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.
 
O

Old Wolf

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.
 
C

Chris Torek

theoderich said:
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?

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.)
 
C

CBFalconer

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.
 
D

Dave Vandervies

theoderich 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.

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
 
D

Dave Vandervies

Old Wolf said:
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
 

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
474,159
Messages
2,570,888
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top