#### Archived

This topic is now archived and is closed to further replies.

# C?

This topic is 5760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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++]

##### Share on other sites
Well C++ is superset of C and C can be compiled on a C++ compiler so the differences cannot be too vast.
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]

##### Share on other sites
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).

new initiates a call to
_nh_malloc_dbg(nSize, nhFlag, _NORMAL_BLOCK, NULL, 0);

malloc initiates a call to
_nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);

Spot the difference! The second parameter seems to be a flag to determine what initiated the call i.e. either malloc or new. When called with new this flag appears to be 1 and when called with malloc its 0. Tracing into the functions still deeper reveals that they are basically the same. The only difference being that malloc will always return a pointer that was returned by a failed call to a heap allocation function (which should always be NULL) but new will just return NULL. Big difference eh? I realise elis-cool that some of this might be a bit technical for where you are at the moment. But I hope that you get the jist.

Kind regards,
Kevin

##### Share on other sites
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.

##### Share on other sites
quote:
Original post by DrPizza
No it isn''t.
No it can''t.

CEO Plunder Studios

##### Share on other sites
quote:
Original post by elis-cool

That doesn''t make it incorrect.

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
http://www.research.att.com/~bs/bs_faq.html

How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

##### Share on other sites
quote:
Original post by siaspete
http://www.research.att.com/~bs/bs_faq.html

That''s another good link that I considered putting in my sig.

##### Share on other sites

Empathise with the person that you are responding to.
Don''t use dishonest tactics to ''win'' an argument. No matter how big it makes you feel.

You obviously can be helpful when you try. Just make sure that you''re responding because you want to help; not to make a point of how clever you are. I''m sure that you are appreciated, much more, when you are being nice. Try to be nicer in future...

Kind regards,
Kevin

##### Share on other sites

  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.

##### Share on other sites
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...
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:

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++]

##### Share on other sites
The following ought not compile in C++ but is reasonable in C (as in, it''ll compile with nary a warning).

  #include <stdio.h>void foo();int main(void){    foo(1, 2, 3);    return 0;}void foo(int a, int b, int c){    printf("%d %d %d\n", a, b, c);}

For instance.

##### Share on other sites
SabreMan, I''m sorry if I may have offended you. It was not my intention. You did (and still do) make some very valid points.

You are right to say that it wasn''t totally accurate to say what I did about new and delete. I could have mentioned that malloc can''t allocate a class for example (and maybe I should have) and I could have explicitly said that the use of malloc and free are not part of the specification for new and delete. But the reason that I didn''t was to not complicated his learning this early on. It is information that he''ll find out in due course. Let me try to explain. My first degree is in physics and so I will use that subject as a simple illustration. When a student is learning physics at high school, he/she is taught many things that are not 100% true. Is this done to confuse them in later life? No, its done to aid the pupil''s learning of a new and potentially complicated subject. They are given an idea of a principle in a way that is easy for them to understand that holds true for everything that they will ever need in high school. If they decide to continue studying the subject at university then at that level they are given a more accurate (and hence more complicated) explanation of the theory. The point is, that they come into the more complicated theories only when they are ready. Now you could argue that this is nothing more than treating intelligent people like fools, but I don''t think that''s its foolish to not burden the shoulders of beginners with information that they won''t need right now and I''m certainly glad that my physics teacher at high school did not bamboozle us with information that we did not need to know. I''m not sure that you''ll understand/agree with my arguments. You *appear* to be more of a ''sink-or-swim'' type of person. I''m more of a ''give them armbands and we''ll make Olympic swimmers of them yet? type of person. But I''m sure that we can agree to disagree? :-)

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

If you would like to continue this discussion by email SabreMan then feel free to do so.

Kind regards,
Kevin

##### Share on other sites
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]

##### Share on other sites
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]

##### Share on other sites
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 );}

##### Share on other sites
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.

##### Share on other sites
Considering I write almost solely in C (a little ASM and a little C++), the example

  	char *pc = malloc( 100 );

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++?

##### Share on other sites
Standard C can be compiled on Standard c++ compilers (unless there are variable names that are standard c++ keywords, which can be handled by simple #defines )

##### Share on other sites
quote:
Original post by daerid
Standard C can be compiled on Standard c++ compilers (unless there are variable names that are standard c++ keywords, which can be handled by simple #defines )

No, it can''t, because there are valid C programs that are ill-formed C++ programs.

##### Share on other sites
Those are specific cases. Saying that "C can''t be compiled on a c++ compiler" is just too generalized. More correctly it would be: "There are some C programs that can''t be compiled with a C++ compiler".

The C++ standard basically says you should be able to compile a C program on a C++ compiler.

But like everything, there are exceptions to the rule.

##### Share on other sites
quote:
Original post by daerid
Those are specific cases.

They''re extremely common. For instance, any code that uses malloc() has a high probability of breaking (in C, it''s never correct to cast malloc()''s return value; in C++, it''s mandatory except for when assigning to void* pointers).

quote:
Saying that "C can''t be compiled on a c++ compiler" is just too generalized.

Fine. ANSI C can''t be compiled on a C++ compiler. Better?

quote:
More correctly it would be: "There are some C programs that can''t be compiled with a C++ compiler".

IOW, a C++ compiler cannot be used as a C compiler.

quote:
The C++ standard basically says you should be able to compile a C program on a C++ compiler.

It says nothing of the sort.

quote:
But like everything, there are exceptions to the rule.

Exceptions, such as writing ANSI C code.