Sign in to follow this  

What exactly makes C++ difficult?

This topic is 3096 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

I am curious. What makes C++ more difficult than any other language. It's powerful and you have to keep track of everything, but that isn't hard to do. I should probably search more, but all I've seen on the subject is that C++ requires more memory management while easier languages do that for you. Most of my experience so far comes from learning C++. I've learned a little Java and VB. However, I haven't done much of the last two I mentioned. I was wondering if anyone can point me in the right direction to truly understanding the differences between the languages and what makes them harder and why?

Share this post


Link to post
Share on other sites
That's the thing -- it's not difficult.

The problem lies in the number of pitfalls associated with the raw power of C++. While the syntax is generally easy to understand and pick up, it's the subtlties of the language grammer that catches beginners off guard.

The biggest pitfall with C++ (which it inherited from C) is the use of pointers. As powerful as they are, there are so many programmers that just don't understand how to properly use them and you start ending up with unitialized and dangling pointers both of which have the general effect of causing your program to train wreck.

Beyond the simple pitfalls is the OOP nature of C++. Many times beginners misuse and abuse classes and their most powerful features: inheritance and polymorphism. Couple this with pointer abuse and you have a recipe for disaster.

On top of all of this is the low-level memory mangement, bitwise operations and operator overloading that can be very confusing. Much of the memory management, however, is easily done for us through the use of the standard template library (STL) but its syntax can be quite intimidating to the unitiated.

The long and short of it is C++ is not a difficult language to learn. It's a difficult language to master due to all of its subtlties.

Share this post


Link to post
Share on other sites
The best way to learn the differences is to keep writing programs in all those languages. Along the way you'll write programs with bugs, and some of those bugs will be due to language deficiencies, and some of those bugs you'll spend days or weeks trying to figure out.

The language that results in the most frustration is your most difficult language.

Share this post


Link to post
Share on other sites
1) C++ is a federation of languages.
It contains a syntax similar to plain C (with enhancements for Object Oriented programming), but it also contains a template meta programming language (which can act as a compile-time functional language!), a pre-processing language and the standard library (AKA the STL).

Many people only learn about half of these things when "learning C++".

2) C++ does not try to protect you from your own stupidity.

Whereas some newer languages have friendly abstractions like "This variable is a handle to an object", C++'s abstraction is more like "This variable holds a memory address, which could be interpreted as the address of an object".
This allows you to do things like pretend that one object is a different type (deliberately or accidentally), which could cause memory corruption or other strange behaviors.

3) C++ supports OOP, but doesn't force it upon you.
People who have learnt in languages like Java might find it hard to adjust to this.

Java-style multiple inheritance is a lot easier for your average coder to use than C++ virtual inheritance.

[edit]You've also got access to C's standard library as well!
Many people who use C++ actually use the C library rather than the C++ library.

Share this post


Link to post
Share on other sites
You should read this thread. Particularly, my post in that thread contains a classic example of why C++ is hard. Also, Ravyne's post explains that the language isn't always the problem, but the non friendly error messages the compiler generates.

But there are other things you need to know in C++. For example, is the memory in std::string contiguous? Is it in std::vector? Do you know and understand the Rule of Three? Do you know how to properly handle exceptions and how to throw them, including how to throw one in a constructor and not leak any allocated memory? Do you know what undefined behavior implies (I'll answer this one: just because your program appears to be working doesn't mean you don't have a massive bug that is just waiting to blow up in your face after you've released your product)? In what range are you legally allowed to increment/decrement a pointer to a block of memory in? What identifiers are reserved for the implementation? How can you avoid the static initialization order fiasco? What is the static initialization order fiasco?

Aside from lots of these types of questions I could ask, there's also a lack of functionality in the C++ standard library. For example, Java and C# and VB all have ways for letting you connect to the internet, download things, send emails, easily handle multithreading, etc. Not so with the C++ standard library. You either have to roll your own code or google for a 3rd party library that can do all this, and then you have to take the time to get familiar with that library, probably build it yourself, link it, etc...

[edit]

ninja'd += 2;

Share this post


Link to post
Share on other sites
Learning object oriented programming?

I originally learnt C, it was a great low level language but large programs could get messy, not to mention rogue pointers. then came along Objected Oriented programming which made designing programs cleaner.

These days you shouldn't even need to learn a language, to develop a program just plug in the right components you want into the proper development tool and HEY PRESTO! you have something like what you wanted! .. maybe? Well that's what I want anyway! LOL!

padlock.

Share this post


Link to post
Share on other sites
Also noteworthy is that C/C++ is very low level and powerful. That means you can do anything with it, everyone has source code for it, and it is "compatable" with every device.
But it is NOT a RAD language out of the box. A RAD language like Python or C# gives you a lot of extra features, and covers a lot of extra cases so you don't have to think about them. This makes prototyping things very fast. Expecially when some RAD languages actually look almost like what is termed "pseudo code".
C/C++ usually means there is a lot of base work and setup involved in getting even the smallest project running. Hop over to python and you can have the same app (maybe not as fast, extensible, or complete) up and running in a usable form in only a few hours work.

It is similar to the comparison of Windows/iProduct/Wii type things vs PS3/linux type things. One may have all the features, but you need a manual to figure it out. The other holds your hand through all the common stuff, but makes your life a pain in the ass once you want to dig deeper.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
and it is "compatable" with every device.


Not exactly. I can't use C++ on an iPhone, for instance. That uses Objective-C which is totally different. I couldn't use C++ on a Nintendo 64 because that uses its own special ASM instruction set.

My toaster isn't 'compatible' with C++ either.

What it boils down to is that any environment with a C++ compiler that follows ANSI standards is likely to be able to compile any program you write so long as you don't have platform specific code in it (e.g., trying to use MessageBoxEx() on Linux won't work just as trying to use signal() won't work on Windows).

Quote:
Original post by KulSeran
2) C++ does not try to protect you from your own stupidity.


This is thee biggest point. C++ doesn't save you from yourself unlike languages like BASIC, PHP, JAVA and so on. With power comes responsibility and abuse of that responsibility results in programs that fail. C++ does exactly what you tell it to do regardless of whether the instruction is erronous or not. This often leads programs to train wreck spectacularly and just 15 years ago could easily crash an operating system (which for now will remain nameless). It is because of the fact that C++ doesn't try to correct for errors that it appears 'difficult' and many bugs come from the improper use of pointers. Probably the most annoying thing and most difficult to find error is the 'dangling pointer' -- this is an error in which the programmer has freed memory from a specific location but continues to attempt to access the memory. The program may continue for many seconds or even minutes before you get what's called a seg-fault. This is a particular problem with beginners and if you're not intimately familiar with your debugger can take weeks to track down and a great deal of frustration. This, alone, is what gives C++ its reputation of being difficult to learn (in my humble opinion, of course).

Anyway, that's far more specific than you probably cared to hear and since you're asking what makes C++ difficult you probably don't understand most of it. Suffice it to say that you'll be spending months figuring out what made your seemingly innocent program blow up which could be frustrating but once it clicks it suddenly becomes an amazing language with very little in the way of limitations.

Share this post


Link to post
Share on other sites
Quote:
Original post by leeor_net
Not exactly. I can't use C++ on an iPhone, for instance. That uses Objective-C which is totally different. I couldn't use C++ on a Nintendo 64 because that uses its own special ASM instruction set.

Sure you could. If you had a C++ compiler for the Nintendo 64 system you'd be able to use C++. And you can, actually, use C++ for the iPhone, though Objective-C is what is mainly used.

Quote:
Original post by leeor_net
My toaster isn't 'compatible' with C++ either.

You should upgrade your toaster then. It's out of date [smile]

Share this post


Link to post
Share on other sites
Quote:
It's powerful and you have to keep track of everything, but that isn't hard to do.


The concept isn't that hard. Executing it, on the other hand, often requires a fairly extensive knowledge of the language. Take for example memory management. Create things with new and be sure you free them with delete. Simple, right?

But trying to apply that knowledge to a very simple example...

class Foo
{
int* f;

public:
Foo( ) : f(new int(10)) { }
~Foo( ) { delete f; }
};

int main( )
{
Foo a, b;
b = a;

return 0;
}


...can completely blow up in your face. To fix the problem with this code, you also need to know how to write proper copy constructors and copy assignment operators. But even then, this code isn't truly safe. In a real world example, Foo will probably contain other members and will do stuff in its constructor. What happens if an exception is thrown in the constructor body? In the initialization list itself? If you don't know how to handle those, you're going to leak memory or cause other problems.

To be sure, there are tools available that simplify most of that if you take advantage of them, but it's hard to know how to use them effictively without knowing why you're using them.

Share this post


Link to post
Share on other sites
Quote:

Quote:
Original post by leeor_net
Quote:
Original post by KulSeran
and it is "compatable" with every device.

Not exactly. I can't use C++ on an iPhone, for instance.
Yes you can ;p
Obj-C is only required for parts of iPhone dev ;)


I didn't know there was a C++ compiler available for the iPhone and I'm sure there are no C++ compilers for the N64... ;)

I guess a better way of stating it is "So long as a particular platform has a C++ compiler you'll be able to build C++ programs on it."

Semantics... tsk tsk... :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
[edit]dammit, ninjad!

w00t! I keep track of how many times I've been ninja'd, but I wish I also kept track of how many times I ninja'd others. Seriously, it's like a victory every time I ninja someone because it rarely happens.

Anyway, @leeor_net, this page might interest you. Mind you I haven't actually read all of it, so I don't know how much it might actually interest you; it's just what came up when I googled "C++ iPhone."

Share this post


Link to post
Share on other sites
Quote:
Not that hard to do...
Consider that when...

Your nVidia drivers crash...
You lose an hour(s) of work on a big project because you attempted a big operation on a large amount of data...
The new game you brought home crashes to the desktop immediately...
Planes or trains crash...
etc...

For my entire life, every tech product I have used as fallen victim to pointer errors one way or another. They are an epidemic, and your average programmer does nothing but drop the ball with all the power C++ gives you.

C++ is not a bad language, but I don't think it's fair to say that it's not that difficult. There is lots of 'ands', 'ifs', or 'buts' in the language and it's libraries.

Also, as someone posted, the standard library doesn't offer much functionality when it comes to actually putting together a functional program. The library basically just fills in the most basic functionality that is missing from the core language, and doesn't offer any application level functionality.

It can be a real pain finding a bunch of libraries that fill in the app level functionality that other languages have in their standard library and getting them to all work together. Especially when they are all coded by different people, with different styles, and have varying levels of stability.

Because of these, the workflow in other languages can be very different. You open up the IDE and immediately begin work on designing the program's core functionality. Where in C++ the first step is often assembling a bunch of libraries and glueing the together, or writing a ton of low level stuff yourself. Of course, that depends on the scope of the application you are creating, or the scope of the libraries you are linking to.

Not having to manage memory so closely is also a huge relief. Memory is a low level technical detail, and often has nothing to do with the functionality of the program you are trying to create.

Share this post


Link to post
Share on other sites
Quote:
Original post by leeor_net
Quote:

Quote:
Original post by leeor_net
Quote:
Original post by KulSeran
and it is "compatable" with every device.

Not exactly. I can't use C++ on an iPhone, for instance.
Yes you can ;p
Obj-C is only required for parts of iPhone dev ;)


I didn't know there was a C++ compiler available for the iPhone and I'm sure there are no C++ compilers for the N64... ;)

I guess a better way of stating it is "So long as a particular platform has a C++ compiler you'll be able to build C++ programs on it."

Semantics... tsk tsk... :)

Well considering Objective C is just a superset of C as C++ mostly is it isn't a big leap.
You can download the sourcecode for Wolfenstein3D for the iphone and see that it's mostly C code with some Obj-C where required.
http://www.idsoftware.com/wolfenstein3dclassic/index.htm


IMO the thing that makes C++ so hard is the same thing that makes learning English supposedly the most difficult language to learn -- too many exceptions.
Due to the fact that C++ prefers speed and minimal memory usage above all it leaves things undefined or leaves them up to the compiler to define that are clearly defined and unambiguous in most other languages!

For example the Visual C++ compiler will gladly compile and run the following innocent looking program while most others like gcc won't:

#include <iostream>

int main (int argc, char * const argv[]) {
// insert code here...
std::cout < "Hello, World!\n";
return 0;
}





p.s. Finally, you know you are playing with danger when other languages call some of your features "unsafe" LOL:
"
C# Programmer's Reference
unsafe
The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.
"
and other languages outright ban it like Java.


[Edited by - daviangel on June 23, 2009 3:07:07 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by daviangel
Well considering Objective C is just a superset of C as C++ mostly is it isn't a big leap.

Technically, there is a variant of Objective-C known as Objective-C++, which is a superset of C++ in the same way that Objective-C is a superset of C. The GNU Objective-C compiler supports Objective-C++, which is how you get C++ support on iPhone.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by daviangel
Well considering Objective C is just a superset of C as C++ mostly is it isn't a big leap.

Technically, there is a variant of Objective-C known as Objective-C++, which is a superset of C++ in the same way that Objective-C is a superset of C. The GNU Objective-C compiler supports Objective-C++, which is how you get C++ support on iPhone.


Technically Objective-C++ is not a superset of C++ as there are some restrictions on the use of C++.

Going back to the topic in hand, why is C++ hard?

C++ is C with all the nice bits removed. It adds ambiguous syntax, shed loads of key words, as well as trying to integrate far too many programming paradigms whilst still retaining manual memory management and unsafe pointers. Sounds great eh?

Share this post


Link to post
Share on other sites
Quote:
Original post by Cromulent
C++ is C with all the nice bits removed. It adds ambiguous syntax, shed loads of key words, as well as trying to integrate far too many programming paradigms whilst still retaining manual memory management and unsafe pointers. Sounds great eh?
All the things that make C++ dangerous it inherited from C though ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
Original post by Cromulent
C++ is C with all the nice bits removed. It adds ambiguous syntax, shed loads of key words, as well as trying to integrate far too many programming paradigms whilst still retaining manual memory management and unsafe pointers. Sounds great eh?
All the things that make C++ dangerous it inherited from C though ;)


True but what makes the dangerous stuff with C bearable is the fact that it is an extremely simple language. C++ is just as dangerous except it is also stupidly complicated on top.

Share this post


Link to post
Share on other sites
Quote:
Original post by daviangel
For example the Visual C++ compiler will gladly compile and run the following innocent looking program while most others like gcc won't:

#include <iostream>
int main (int argc, char * const argv[]) {
// insert code here...
std::cout < "Hello, World!\n";
return 0;
}


Imho, the VCPP compiler is just too liberal by default. Not speaking from my own experience, but from what I read here on gd.net. Above code looks perfectly invalid to me, so I would personally wonder about every compiler that would compile that piece.

Beginners who are glad to get something compiled, will learn that it worked that way and do the same failure again and again, until one day they try out another compiler (or VCPP gets surprisingly more strict), and end up asking members of gd.net about why it doesn't compile anymore.

Share this post


Link to post
Share on other sites
Well, std::cout is implicitly convertible to void*, and if the compiler also decides it's OK to cast the right-hand side to void*, you'll have a perfectly good pointer comparison (Comeau online also compiles it fine). This just shows why one is recommended to avoid overloading conversion operators.

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
Well, std::cout is implicitly convertible to void*, and if the compiler also decides it's OK to cast the right-hand side to void*, you'll have a perfectly good pointer comparison (Comeau online also compiles it fine). This just shows why one is recommended to avoid overloading conversion operators.


Whoa, didn't know about ostream::operator void* *facepalm*.

As for this, it seems that operator void* is there so you can check whether "a failbit or badbit is set". If you ask me and if I understand it right, this is blatantly bad operator overloading abuse.

Share this post


Link to post
Share on other sites
On the other hand, this is what makes these work:


//getline returns std::cin which is implicitly converted to void* for testing
while (getline(s, std::cin)) {...}

int n;
//same here
if (!(std::cin >> n)) {
//bad user not inputting a number
}
if (std::cin >> n) {
//good, we have successfully read a number
}


Besides, implicit conversion to void* is considered safer than conversion to bool, since void* is good for very few things (as there is not implicit conversion from void* to other pointer types in C++).

A way to avoid some of the issues with implicit conversion is the safe bool idiom.

It looks that C++0x adds explicit conversion operators. Then streams could define an explicit conversion to bool, and it would work implicitly only in boolean contexts.

Share this post


Link to post
Share on other sites
Vast swathes of legacy code that doesn't document the assumptions it was based upon, or which has always been unsafe but has always appeared to work. That's what makes C++ hard.

It's not only C++ of course, but the potential for these kinds of things seems much greater with C++ than any other mainstream language due to the sheer number of language features and quirks.

Share this post


Link to post
Share on other sites
Quote:
For example the Visual C++ compiler will gladly compile and run the following innocent looking program while most others like gcc won't:
#include <iostream>
int main (int argc, char * const argv[]) {
// insert code here...
std::cout < "Hello, World!\n";
return 0;
}


MSVC8:
warning C4552: '<' : operator has no effect; expected operator with side-effect

MSVC9:
error C2678: binary '<' : no operator found which takes a left-hand operand of type 'std::ostream' (or there is no acceptable conversion) could be 'built-in C++ operator<(const char [15], const char [15])' or 'built-in C++ operator<(void *, void *)' while trying to match the argument list '(std::ostream, const char [15])'

Share this post


Link to post
Share on other sites

This topic is 3096 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this