Jump to content
  • Advertisement

Archived

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest 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).

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.

P.S. The same goes for free and delete.

Kind regards,
Kevin

Share this post


Link to post
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 this post


Link to post
Share on other sites
quote:
Original post by elis-cool
That bascily contradicts everything ive read!



That doesn''t make it incorrect.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!