Why? Doesn't this simply mean that it is poor form to not declare your functions?
If the behavior is unspecified (and you have a lot of that on C++ too) the general rule of thumb is... don't do it.
Why? Doesn't this simply mean that it is poor form to not declare your functions?
If the behavior is unspecified (and you have a lot of that on C++ too) the general rule of thumb is... don't do it.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
I have a question. Is g++ the same fast as gcc? If not there will always be a reason to use C over C++. I found this http://shootout.alioth.debian.org/gp4/cpp.php for example.
But in any case, why wouldn't they two compilers be the same? And why have two compilers and not an upgraded one with a flag -Conly.
Or at least, for 99% of the cases, that would be true. There are some subtle differences between gcc and g++ that SOMETIMES cause a small difference.
But in essence the Doodle77 statement is correct: The two pieces of code compared here are not identical - they use the same basic algorithm, but it's implemented in different ways.
Compare:
http://shootout.alioth.debian.org/gp...&lang=gpp&id=3
http://shootout.alioth.debian.org/gp...otide&lang=gcc
and you can see that the code is not very similar at all [it is of course similar in the aspect that it's implementing the same functionality - but in the detail, it is quite different]. I don't see any obvious "big hit" on the performance, but C++ tends to offer subtle ways to "hide" performance hits - a possible candidate is that the C version uses realloc to allocate a 10K larger chunk of memory inside the initial fgets() loop - which is done differently in the C++ version.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Of course you shouldn't do it, but should a compiler actively allow it? I think not.
The compiler should complain and avoid undefined behavior. It's very easy to hide this little warning.
So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.
It would not be a reason to use C over C++, since language-wise, they are pretty much equal in performance, some places where C perform better, some places where C++ perform better.
Note that a poor C++ compiler may produce slower code than the equivalent C compiler, but that is hardly the language's fault and the solution is to get a BETTER compiler.
I don't even know where to start dismantling this opinion of yours.
Visual Studio complains starting on warning level 3 with default project setup.Code:// C int main() { int foo = 12; bar(foo); return; } void bar(int foo) { ; }
Didn't test it on gcc, but seen my share of "Implicit declaration of function..." warnings. So not sure what do you mean. The compiler complains.
Meanwhile...
Doesn't sound a blip on warning level 4.Code:// C++ int main() { int foo[5] = {1,2,3,4,5}; int bar[5]; int i = 0; while (i < 5) bar[i] = foo[i++]; }
So back to your argument:
>> So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.
So it is evaluation order dependency and yet I got no warning. In fact the C++ standard says I don't have to have a warning. It's undefined behavior... as it is the result of implicit declaration of functions in C. More, implicit declaration results in a warning, whereas evaluation order dependency doesn't. And on some circumstances the compiler couldn't even if it wanted too. Which is "worst"? Don't answer.
One last small little unimportant thing of absolutely no consequence at all: C99 removed implicit declaration of functions. So... yeah.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Classic error:
Yes, Visual Studio is actually kind enough to warn me. Are other compilers just as nice? No.Code:// C int main() { int* foo; foo = (int*)malloc(sizeof(int)); return; }
And just to clarify: I didn't include the proper header.
And what does the C++ sample have to do with anything?
No, it's not good that it doesn't warn, but it doesn't warn regardless if it's C or C++ code, does it?
My point is that C++ actually improved on something - it removed something very nasty and dangerous from C. That doesn't mean C++ is always better in everything.
*opens mouth to say something but gives up...*
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
The cast does mask the implicit declaration. However, there is really no use for an implicit declaration, except for cutting corners, which really isn't needed, I'd guess.
It's an easily detected error that pretty much all compilers recognize, and if it doesn't, you could just use a static checking tool instead, or compile it with another compiler. It's a problem that's fixed with a compiler switch, providing a way to not have that problem and write in fully conforming code otherwise. I don't see what's so impractical about this.
<my two cents>I like C.*</my two cents>
* My two cents are actually an arcade token and a plastic button. So actual monetary may vary depending on the exchange rates.