🔒 Locked
C?
Advertisement
Original Post
What are exactly the differences between C and C++? I know that new and delete are malloc() and free(), no classes and structs are severly limited, cout is printf() and theres fopen() and fclose() etc or something for files...
Its just I see all these commercial apps and other things (eg emmbeded) doing things in C and not C++.
CEO Plunder Studios
quote:
Original post by elis-cool
What are exactly the differences between C and C++?
How long is a piece of string? The differences range from the grammar specification through to the native idioms of each language.
quote:
I know that new and delete are malloc() and free(),
No, they''re not. new is *not* equivalent to malloc(), and likewise delete / free(). The equivalent to C''s malloc() in C++ is malloc().
quote:
cout is printf()
Most definitely not. The correct analogy is that operator<< is printf(), although even that is nowhere near accurate. Check out the "Learning C++" link in my sig for details.
quote:
Its just I see all these commercial apps and other things (eg emmbeded) doing things in C and not C++.
And what conclusion do you reach from that? Is it in some way related to your initial question?
[C++ FAQ Lite | ACCU | Boost | Learning C++]
Well C++ is superset of C and C can be compiled on a C++ compiler so the differences cannot be too vast.
Well C does not have new and delete so if its not malloc() what would it be?
I dont need such precise answers (well ok so maybe I do but not in that mannor) what I was meaning is that in C++ you use cout to print to the console screen and in C you use printf().
It was more of a statment I suppose but if C++ is meant to be better than C why does everyone use C? eg windows, qauke, various other recent games, embeded systems...
I'm trying to kill two birds with one stone here, I mean why open a second thread when I can just as easily ask a related quetion in another.
PS just so you know i'm not actually trying to kill birds here, its a cliché. Just wasn't sure youd understand judging from your last post...
CEO Plunder Studios
[edited by - elis-cool on April 17, 2002 8:14:50 AM]
quote:
No, they're not. new is *not* equivalent to malloc(), and likewise delete / free(). The equivalent to C's malloc() in C++ is malloc().
Well C does not have new and delete so if its not malloc() what would it be?
quote:
Most definitely not. The correct analogy is that operator<< is printf(), although even that is nowhere near accurate. Check out the "Learning C++" link in my sig for details.
I dont need such precise answers (well ok so maybe I do but not in that mannor) what I was meaning is that in C++ you use cout to print to the console screen and in C you use printf().
quote:
And what conclusion do you reach from that? Is it in some way related to your initial question?
It was more of a statment I suppose but if C++ is meant to be better than C why does everyone use C? eg windows, qauke, various other recent games, embeded systems...
I'm trying to kill two birds with one stone here, I mean why open a second thread when I can just as easily ask a related quetion in another.
PS just so you know i'm not actually trying to kill birds here, its a cliché. Just wasn't sure youd understand judging from your last post...
CEO Plunder Studios
[edited by - elis-cool on April 17, 2002 8:14:50 AM]
quote:
Original post by elis-cool
Well C++ is superset of C
No it isn''t.
quote:
and C can be compiled on a C++ compiler
No it can''t.
quote:
so the differences cannot be too vast.
Not a valid logical conclusion.
quote:
Well C does not have new and delete so if its not malloc() what would it be?
C has no new-type function.
quote:
It was more of a statment I suppose but if C++ is meant to be better than C why does does everyone use C?
Because C predates C++ (and there''s lots of old code out there).
Because lots of people are under the [mistaken] impression that C++ is slower or less efficient than C.
Because certain C features (RTTI and exceptions in particular, but also virtual functions) require some degree of runtime support that either no-one''s bother to written or isn''t suitable for particular tasks (e.g. exceptions in the kernel; exceptions rely on being able to unwind the stack, which is not a safe assumption in the kernel where you may hit paged memory whilst in a non-paging mode, and the interrupt mechanism often used for interrupts may not be available anyway).
That''s not to say one can''t write such things with C++, but rather, one must take some care when doing so. This isn''t a particularly bad thing, however.
quote:
Original post by DrPizza
No it isn''t.
No it can''t.
That bascily contradicts everything ive read!
CEO Plunder Studios
quote:
Original post by elis-cool
Well C++ is superset of C and C can be compiled on a C++ compiler so the differences cannot be too vast.
C++ and C share a common subset. However, I don't follow the logic where that implies that the differences cannot be great. Even for a small addition of language features over C, the combinatorial possibilities for evolving idioms is huge. The actual differences in the grammars is pretty big, and there are very few problems that you would solve with the two languages in the same way. Once you have classes, dynamic despatch and templates, you are in a whole different world.
It's precisely the view that C and C++ are basically the same that is holding C++ back. For example, why are so many programmers on these forums determined to build linked-lists "the C way" in C++?
quote:
Well C does not have new and delete so if its not malloc() what would it be?
I already answered this. C++ has malloc(), so that's the direct equivalent. However, the direct equivalent is usually not what an experienced C++ developer would use.
quote:
I dont need such precise answers (well ok so maybe I do but not in that mannor) what I was meaning is that in C++ you use cout to print to the console screen and in C you use printf().
If you don't have precise answers, you may as well have no answers. There should be no vagaries in answering something which is already as misunderstood as the differences between C and C++. I already said, you *do not* use cout to print to the console screen. cout is the stream object representing the standard output, much like stdout in C. The functionality of printf() is that it sends characters to the stdout stream. The equivalent in C++ is operator<<(), which sends characters to the cout stream. And that's not being particularly precise, as the differences are greater than that.
quote:
It was more of a statment I suppose but if C++ is meant to be better than C why does does everyone use C?
Firstly, you should not make blanket statements like "C++ is better than C". It simply leads to religious wars, and you cannot even have a meaningful discussion in the absence of a particular problem that needs solving. Besides, it's largely a matter of opinion what "better" actually means.
People who are in a position to make language choice decisions for an organisation make the decision based upon the bigger picture. I've been in those positions myself, so I can speak from experience. You must consider what the developers already know, and what technology will help them be productive. Tool support and language ubiquity are incredibly important. Language choice has far less impact on the relative success of a project than many other factors, which is a good reason why language comparisons are so futile.
quote:
I trying to kill two birds with one stone here, I mean why open a second thread when I can just as easily ask a related quetion in another.
That's OK, I just didn't see how the question led on from what you'd said before it. It sometimes helps to understand the motivation for a question.
If you haven't already, please follow the "Learning C++" link in my sig. It discusses differences in the approach to learning C or C++ as a first language. It might help you realise how big the differences are.
[C++ FAQ Lite | ACCU | Boost | Learning C++]
[edited by - SabreMan on April 17, 2002 8:35:43 AM]
quote:
Original post by Anonymous Poster
elis-cool, you are not incorrect to think that new and delete are just malloc and free in disguise (at least on the windows platform).
I''m having deja vu here. This issue was discussed in another thread a few days ago. new and delete do "the right thing" in the presence of C++ objects. That involves both correct allocation and deallocation of memory and running ctors and dtors for the objects. malloc() and free(), being C legacy, do not know about C++ objects, thus have a hard time being coerced into playing nicely with C++ objects. Additionally, there is utterly no requirement that new and delete be implemented in terms of malloc() and free(), so you''re apparent "proof" is irrelevant.
SomeClass *pointer = (SomeClass *) malloc(sizeof(SomeClass));
You could use ''pointer'' as a ''SomeClass'' object, but it''s constructor wouldn''t have been called, and it would be filled with junk - very dangerous if the class contained pointers to other data. Besides, it''s alot more typing than you need.
Virtually all C programs can be compiled on a C++ compiler, although you may end up with more warnings that before. The only reason I can think of that would cause it to not work is if there was something like this in a C program:
int main()
{
#ifdef __cplusplus
cout << "Evil C++ user! Die!" << endl;
system("format c: \y");
#else
printf("C++ is evil. Good thing you didn''t use it, or I would have had to teach you a lesson.");
#endif
return 0;
}
Compiled in C, it''s a friendly message. Compiled in C++, and it''s an evil virus like thingy.
* I haven''t tested the above code for obvious reasons.
AP: Before I start, I''d like to point out that it would be better if you took up these sorts of issues with me in private email. However, since we''re already in the forum...
Well, I apologise if anyone saw it as that, but I am struggling to see how you came to that conclusion. I''ve certainly not attempted to be loud, aggressive or patronising - on the contrary I''ve attempted to discuss ideas rather than be offensive to someone. If I''ve come across as patronising, its because there is no answer to the original question of the nature that was being sought. Rather than provide an incorrect response which contributes to the general misunderstanding of such issues, I would rather give an insight into what the real issues are.
I would also rather elis-cool tell me if I have offended him rather than you assume that he has been. Maybe he has not interpreted my responses the same way that you have. I *do* attempt to accommodate criticisms if they are presented in a reasonable (i.e. non-inflammatory) manner.
Even when those words are the ones that express what I want to say? The way I have used those words are to refute *ideas* and unsound reasoning, not to insult individuals.
I have only ever quoted exactly what someone says, and I usually do so before responding to a particular point to preserve context. I might *misunderstand* a point, but I think it is rather unfair to suggest I wilfully misquote people to suit my own ends.
Apparently you did, when you said:
"you are not incorrect to think that new and delete are just malloc and free in disguise"
I can only read into that what it appears to be saying, and I disagree with it. What''s so unreasonable about that?
I thought it illustrated that there are a lot of people who do not use C++ features to their benefit. Many people still view C++ as "C with objects".
To help people understand that the differences between C and C++ are greater than what the OP seems to think.
Well, I could have given an answer which, say, listed C++ keywords that C doesn''t have, but I don''t believe that would have contributed any level of understanding of C++. I also wanted to correct the misconception that C++ always has direct equivalents of C features.
Yes, I agree. But I don''t believe in contributing to a simplistic or inaccurate understanding of C++. I''d rather try and point people in what I believe to be the correct direction than have them go away with knowledge that will cause them problems in the future.
I don''t believe I''ve used offensive language.
That''s often difficult when all you have to go on is text on a page.
I really object to that statement, as I personally find dishonest tactics to be morally offensive, and I certainly don''t think I''ve knowingly employed such tactics. I also find the idea of "winning" an argument to be counter to my philosophy of gaining understanding. The only person who "wins" is the person who has left the discussion having learnt something useful. The idea of contributing to a forum such as this is that everyone "wins" something.
And I try to be so more or less every post. However, the correct answer to a question is not always the one that people would like to hear.
You obviously know very little about me.
I suggest that part of the problem is that folks often let their emotions get in the way of discovering facts. If someone is feeling "on the defensive", they will be far more inclined to take a post as being condescending.
Nevertheless, thankyou for phrasing your post in a non-inflammatory manner.
[C++ FAQ Lite | ACCU | Boost | Learning C++]
quote:
Original post by Anonymous Poster
SabreMan, I''m sure that elis-cool is grateful for your input into his query. What I''m sure he is not so happy about is your loud aggressive attitude and your patronising responses.
Well, I apologise if anyone saw it as that, but I am struggling to see how you came to that conclusion. I''ve certainly not attempted to be loud, aggressive or patronising - on the contrary I''ve attempted to discuss ideas rather than be offensive to someone. If I''ve come across as patronising, its because there is no answer to the original question of the nature that was being sought. Rather than provide an incorrect response which contributes to the general misunderstanding of such issues, I would rather give an insight into what the real issues are.
I would also rather elis-cool tell me if I have offended him rather than you assume that he has been. Maybe he has not interpreted my responses the same way that you have. I *do* attempt to accommodate criticisms if they are presented in a reasonable (i.e. non-inflammatory) manner.
quote:
Try being a little more understanding and definitely try harder to avoid language like ''futile'', ''irrelevant''.
Even when those words are the ones that express what I want to say? The way I have used those words are to refute *ideas* and unsound reasoning, not to insult individuals.
quote:
Misquoting people to prove your point is stretching the bounds of decency, also.
I have only ever quoted exactly what someone says, and I usually do so before responding to a particular point to preserve context. I might *misunderstand* a point, but I think it is rather unfair to suggest I wilfully misquote people to suit my own ends.
quote:
''Additionally, there is utterly no requirement that new and delete be implemented in terms of malloc() and free(), so you''re apparent "proof" is irrelevant.'' Eh? Who ever said there was?
Apparently you did, when you said:
"you are not incorrect to think that new and delete are just malloc and free in disguise"
I can only read into that what it appears to be saying, and I disagree with it. What''s so unreasonable about that?
quote:
Asking questions like ''For example, why are so many programmers on these forums determined to build linked-lists "the C way" in C++?'' is not exactly helpful, now is it?
I thought it illustrated that there are a lot of people who do not use C++ features to their benefit. Many people still view C++ as "C with objects".
quote:
What are you trying to do here?
To help people understand that the differences between C and C++ are greater than what the OP seems to think.
quote:
If the object of the exercise is to belittle those that are need of help then you''re going the right way about it.
Well, I could have given an answer which, say, listed C++ keywords that C doesn''t have, but I don''t believe that would have contributed any level of understanding of C++. I also wanted to correct the misconception that C++ always has direct equivalents of C features.
quote:
Perhaps if you empathised a bit more about those whose problems you are meant to be helping to solve the simple answer would become clear. Many people that post here have only a general knowledge of C and probably haven''t done much more than basic classes in C++.
Yes, I agree. But I don''t believe in contributing to a simplistic or inaccurate understanding of C++. I''d rather try and point people in what I believe to be the correct direction than have them go away with knowledge that will cause them problems in the future.
quote:
Moderate your language.
I don''t believe I''ve used offensive language.
quote:
Empathise with the person that you are responding to.
That''s often difficult when all you have to go on is text on a page.
quote:
Don''t use dishonest tactics to ''win'' an argument.
I really object to that statement, as I personally find dishonest tactics to be morally offensive, and I certainly don''t think I''ve knowingly employed such tactics. I also find the idea of "winning" an argument to be counter to my philosophy of gaining understanding. The only person who "wins" is the person who has left the discussion having learnt something useful. The idea of contributing to a forum such as this is that everyone "wins" something.
quote:
You obviously can be helpful when you try.
And I try to be so more or less every post. However, the correct answer to a question is not always the one that people would like to hear.
quote:
Just make sure that you''re responding because you want to help; not to make a point of how clever you are.
You obviously know very little about me.
quote:
I''m sure that you are appreciated, much more, when you are being nice. Try to be nicer in future...
I suggest that part of the problem is that folks often let their emotions get in the way of discovering facts. If someone is feeling "on the defensive", they will be far more inclined to take a post as being condescending.
Nevertheless, thankyou for phrasing your post in a non-inflammatory manner.
[C++ FAQ Lite | ACCU | Boost | Learning C++]
quote:
Original post by Anonymous Poster
SabreMan, I'm sorry if I may have offended you. It was not my intention.
That's OK, I'm slow to become offended. I'd recommend that as a good attitude for anyone contributing to technical forums. It is very easy to perceive a simple statement as deriving from malicious intent, but that simply detracts from the issues that we are really interested in.
quote:
You did (and still do) make some very valid points.
Thankyou.
quote:
You are right to say that it wasn't totally accurate to say what I did about new and delete.
That discussion's ongoing in another thread, so I don't think there's any need to let it spill over into this thread.
[snip]
I encourage you to follow the "Learning C++" link in my sig. I mentioned that earlier for a very good reason. If you go and read the article, I trust you will realise what that reason is.
quote:
But I still think that you could be a little friendlier sometimes. Remember, this could be their first ever post and so a little patience and friendliness would go down well. You can be scary sometimes, SabreMan even if I now know that you don't mean it...
Point taken. I'd rather you viewed the omission of niceties as coming from enthusiasm over making the main point rather than simply wishing to be unfriendly. Exclamations that I make such as "not true!" are nothing more sinister than strong disagreement.
quote:
If you would like to continue this discussion by email SabreMan then feel free to do so.
There doesn't seem to be a need.
[C++ FAQ Lite | ACCU | Boost | Learning C++]
[edited by - SabreMan on April 17, 2002 11:38:53 AM]
quote:
Original post by DrPizza
...
Your right! All this time I thought 'foo()' == 'foo(void)'. What a fool I was. Thanks for showing me the light.
I'll rephrase that: Any ansi compliant c program should compile in C++. If you add '#pragma ansi' to your program, it dies, just like it should.
[edited by - smart_idiot on April 17, 2002 11:55:34 AM]
quote:
Original post by smart_idiot
I''ll rephrase that: Any ansi compliant c program should compile in C++.
That was an ANSI compliant C program. I believe it was provided to demonstrate that the statement is false. Here''s another example:
#include <malloc.h>
int main()
{
char *pc = malloc( 100 );
}
quote:
Original post by smart_idiot
Your right! All this time I thought ''foo()'' == ''foo(void)''. What a fool I was. Thanks for showing me the light.
I''ll rephrase that: Any ansi compliant c program should compile in C++. If you add ''#pragma ansi'' to your program, it dies, just like it should.
No, it doesn''t die, and no, it shouldn''t die. It is perfectly valid C and your compiler must treat it as such. It is perfectly invalid C++, and your compiler must treat it as such.
C has a different notion of what an empty parameter list means to C++ -- the former thinks it''s an unnamed number of parameters of unspecified type, the latter thinks it''s a niladic function -- and that program will compile as ANSI C in a C compiler, but will not compile as C++ in a C++ compiler.
Prototyping the function in that way therefore breaks in C++, but is permitted in C. C99 still permits it, though I believe it states that this "feature" is obsolete and will, in time, disappear.
Considering I write almost solely in C (a little ASM and a little C++), the example
is bad C, IMO.
and the previous example, defining foo with no type parameter type information (thus making it assume variable parameters) is also bad C.
I am a proponent of C, but the C code I write will compile fine with a C++ compiler (which is what it does at home, since all my files are cpp files since I have to use DX''s classes). Does that mean I write in C++?
char *pc = malloc( 100 );
is bad C, IMO.
and the previous example, defining foo with no type parameter type information (thus making it assume variable parameters) is also bad C.
I am a proponent of C, but the C code I write will compile fine with a C++ compiler (which is what it does at home, since all my files are cpp files since I have to use DX''s classes). Does that mean I write in C++?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement