Proper way to learn c ?

Discussion of chess software programming and technical issues.

Moderator: Ras

stevenaaus
Posts: 613
Joined: Wed Oct 13, 2010 9:44 am
Location: Australia

Re: Proper way to learn c ?

Post by stevenaaus »

Linus rants on so much - it doesnt even make the news anymore ? :P
I totally missed his C++ rant. http://lwn.net/Articles/249460/

I'm not sure he is capable of much empathy. Obviously OS kernels are almost exclusively written in the lowest usable language available, C, but C++ is damn handy sometimes. It's the same with his Nvidia rant. The average linux gamer worships nvidia for the stability and quality of their kernel module, but Linus just gets peeved they won't come to *his* party.

Anyway - imho C is basically a supercharged assembler, and is just kindof of unavoidable. But I don't have much fondness for C++ either. Functional languages (and Tcl) are what get me excited.

What's the best way to learn C ? Having friendly C gurus at hand is my best advice. :idea:
Rein Halbersma
Posts: 751
Joined: Tue May 22, 2007 11:13 am

Re: Proper way to learn c ?

Post by Rein Halbersma »

lucasart wrote: when trying to compile this decievingly simple piece of C++ code

Code: Select all

#include <list>
#include <string>
#include <algorithm>
using namespace std;
void foo(list<string>& x) {
sort(x.begin(),x.end()); // wrong iterator type
}
I guess that demonstrates the benefit of STL and generic programming... I really miss all that when I code in C: why make simple when you can do complicated?
So you blame C++ for the fact that this particular guy did not bother to read the manual? You learn in kindergarten that std::sort only works on random access iterators (i.e. pointers into contiguous memory), and that to sort std::lists he simply needed to have written

Code: Select all

std::list x;
// fill x with elements;
x.sort();
EDIT: I agree BTW that the compiler error message should be much clearer. But don't blame the user's error on the language.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

Rein Halbersma wrote:
lucasart wrote: when trying to compile this decievingly simple piece of C++ code

Code: Select all

#include <list>
#include <string>
#include <algorithm>
using namespace std;
void foo(list<string>& x) {
sort(x.begin(),x.end()); // wrong iterator type
}
I guess that demonstrates the benefit of STL and generic programming... I really miss all that when I code in C: why make simple when you can do complicated?
So you blame C++ for the fact that this particular guy did not bother to read the manual? You learn in kindergarten that std::sort only works on random access iterators (i.e. pointers into contiguous memory), and that to sort std::lists he simply needed to have written

Code: Select all

std::list x;
// fill x with elements;
x.sort();
EDIT: I agree BTW that the compiler error message should be much clearer. But don't blame the user's error on the language.
You cannot exactly call me a beginning programmer, but in the C++ codes the errors i get, in say 25% of the cases:

a) usually refer to a different line than where the error are
b) the error message really has nothing to do with the actual mistake i made
c) there is not always determinism in the error message if at another spot you make the same mistake - it can result in a total different error
d) it's not obvoius whether you made some huge mistake somewhere or whether simply 1 of the receiving functions has different arguments than what the compiler expected

C++ is very self confusing there.

What most people forget is that humans also build the compiler and that a piece of software that has to convert an object oriented programming language to a binary is not exactly clever.

Just forgetting a semi colon behind a class already can result in a nightmare!

My conclusion is that C++ is a very usable language if you would strip it down back to the type of typical C++ code you saw start 90s.

I happen to know some of the C++ coders posting here program like that - that's the type of code that's easily readable, usable, and very good. that's how using C++ is an advantage over other languages.

Realize that for low level stuff like computerchess, their code is slower than if i would setup the same thing in the same time frame in C. *considerable slower* is their C++ code if i may say so.

Now where for most projects speed isn't total crucial, C++ in theory wold be usable when programmed like these guys here. don't underestimate how good these guys are.

Yet that's not reality in C++ land. The habit is to USE all the FEATURES the language has, just because they CAN DO it. Like girls who are following fashion. That is what PRACTICAL happens.

C++ was pretty ok from before when templates dominated C++ codes - and not even mediocre programmers will be able to bugfree handle templates, you need guys with 10 years experience there and deep insight in the code to use them.

Basically if you ship Bjarne Stroustrup to Mars with all the C++ rules he invented, you would get a good language.

Most 90s C++ codes from some bigger companies are like that. Well readable, good usable, and used of course bigtime.

Of course i didn't refer to Adobe there, the way how they setup their source code is total nuts. Some other companies that set up c++ codes from back then, with large C++ bases, that's very easily readable.

Yet it's total different from how C++ codes look nowadays. The language has been made too complex to take serious for big projects that you carry out with big teams where also the lesser programming hero's are there.

Only small projects with small teams of really good programmers would be possible. However that is expensive guys, if companies set up a new team of coders, they simply are NEVER prepared to pay salaries like that; with maybe 1 or 2 companies excepted - nowadays the hiring policy of google even it seems they have too many Indians there hiring - and those only hire soldiers who are good in saluting - but that's another thing - google still isn't hte average company there of course - here in europe no company for a team of coders would be willing to pay salaries above say 60k euro a person a year. So you simply won't be able to assemble a good C++ team EVER.

If you would use C++ for something now you really need hard definitions on what features in the language are allowed and what is not allowed. Just checking and verifying that in a project is HUGE overhead.

Projects with such police agents that force you to redo code are not nice.

In open source that would be total disaster.
lucasart
Posts: 3241
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Proper way to learn c ?

Post by lucasart »

diep wrote: Just forgetting a semi colon behind a class already can result in a nightmare!
Oh yes, I have some nice memories of that. You can spend hours on it, and never find the problem.
diep wrote: My conclusion is that C++ is a very usable language if you would strip it down back to the type of typical C++ code you saw start 90s.
Yes, if only that was possible... Bjarne often says that "yes C++
is complicated, but what you don't use, you don't pay for". This is such a lie it's not even funny. To be able to write C++ code that will be as fast and reliable as C code, you basically need to be a C++ guru, and know all the details of what the compiler is doing behind your back (object copies on the fly, exceptions thrown by STL or even the new and delete operators, how to not leak memory when exceptions are thrown etc.) And then they go on saying that C++ is easier than C for beginners, as it's object oriented and has some nice STL... It is a nightmare for newbies, and they spend most of their time trying to figure out the subtilties of the language, understand why their program leaks memory, and how to manage exceptions, why they can't call virtual functions in a constructor and so on... If all programmers were gurus, and had the skills to write a C++ compiler themselves, then yes, C++ would be OK for them to use. But the reality is that 90% of programmers are incompetent, and incompetent programmers are typically drawn into using C++, because the C++ evangelists tell them how easy it is.

Basically if C++ was C with classes, no exceptions, no iterators and so on, then it would be useable, and arguably better than C. That's what the first of C++ used to be like. I think C++ degenerated with the C++ 98 standard, and now C++0x is just ridiculously complex.

Newbies are typically attracted by the nice looking OO syntax, and using STL. But C++ is very tricky and deceptive. It is the devil's invention if you ask me.

And I don't say that out of ingnorance. In fact, after mastering C, I wanted to learn C++. And at the beginning I really loved it, and then I started indulging in template programming, and using more STL, even exceptions... The more I learnt all that non sense, the more I realized my productivity was decreasing, and I was spending much more time figuring out whether I should use a std::iterator<std::vector<std::string>>, what an iterator actually was, whether I could write my own iterator derived classes etc. when really looping with an int is fine... And they say C++ solves the technical details for you and helps you focus on the problem at hand: it's exactly the opposite!

In fact I started DoubleCheck in C++, and it was starting to get quite hairy, and object oriented. I rewrote it in C, and ever since then I really regret nothing from C++, except a few things (maybe default value in functions, function overloading would be nice in C, but the rest is BS).

If you want to write a program that manipulates complex date structures, you can write it in C (painful), or in C++ (apparently painless but appears deceptive quite quickly). But I think such programs should neither be in C nor in C++, but should be written in a proper high level language or a function language: Python is probably a good compromise.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

lucasart wrote:
diep wrote: Just forgetting a semi colon behind a class already can result in a nightmare!
Oh yes, I have some nice memories of that. You can spend hours on it, and never find the problem.
diep wrote: My conclusion is that C++ is a very usable language if you would strip it down back to the type of typical C++ code you saw start 90s.
Yes, if only that was possible... Bjarne often says that "yes C++
is complicated, but what you don't use, you don't pay for". This is such a lie it's not even funny. To be able to write C++ code that will be as fast and reliable as C code, you basically need to be a C++ guru,
2 remarks:

a) most company code is deeply layered in C++. 10 layers is nothing.
No compiler manages to optimize that well. It's turtle slow. Factor 100, as you can see in adobe software a lot, that's a slowdown we shouldn't even discuss - factor 1000 is more worrying yet that happens a lot as well.

That's code designed by exclusively guru's.

b) you really underestimate how fast the code is that some of the top guys program. For example if we line up all C++ programmers from this forum, and though i program lots of C++ code, with prospects that only the Diep engine is C and everything else i do is C++. So i would not go stand in that row in fact. Yet from that entire row, just 1 guys C++ is gonna be a lot faster than anyone else and that's of course Anthony Cozzie's C++.

Realize how difficult it is to get huge speed out of C++ even at something, from code size seen, simple as a chess engine.

Those guru's, which this forum has quite some of, they have the habit to already by default layer things. That is very nice and it's considered good habit in C++. However it already means you're gonna be slower. Not necessarily but if you add it all up, most of those guru's code, the ones i helped speedup their c++ code, always could pour out more than factor 2 speed relative easily.
and know all the details of what the compiler is doing behind your back (object copies on the fly, exceptions thrown by STL or even the new and delete operators, how to not leak memory when exceptions are thrown etc.)
I always wonder why people are busy with STL. There is commercial libraries out there way more interesting than STL (and more expensive, besides a lot of contracts you have to sign to get access to that huge codebase) for specific purposes and superior C++ quality.
And then they go on saying that C++ is easier than C for beginners, as it's object oriented and has some nice STL... It is a nightmare for newbies, and they spend most of their time trying to figure out the subtilties of the language, understand why their program leaks memory, and how to manage exceptions,
Am i seeing it correct now you confuse c++ with java?
Why doesn't that surprise me?

In java you always have to catch exceptions - not in C++.

If you write bugfree code like i do - which exceptions? :)
why they can't call virtual functions in a constructor and so on... If all programmers were gurus, and had the skills to write a C++ compiler themselves, then yes, C++ would be OK for them to use. But the reality is that 90% of programmers are incompetent, and incompetent programmers are typically drawn into using C++, because the C++ evangelists tell them how easy it is.
The problem is not so much the language here nor the guru's. the problem is that the interesting software projects get carried out by companies here which i would call incompetent in carrying out such projects, as they have the habit to first fly in a team that setups a prototype with less than half a dozen competent persons. From that half a dozen team already 1 is a teamleader and 1 is a manager and 1 is a salesman, so usually the programming team is 2-3 persons or so. Real little.

After that they sell the code to a major company and get a 'go' for the project, usually government over here. Then they throw in another 50 persons whom i wouldn't trust writing tic tac toe bugfree to 'finish' the project.

That's how it works.

The companies getting such contract jobs in general have problems breaking even with persons older than 35 years old and earning more than 3000 euro (before tax) a month. That's why the persons working there don't make that much more. In general there is 1 manager overlooking a person or 80.

In reality this government department or big company could better hire 3 real good top coders and pay them a huge salary, but that's not how reality works. The last thing they would do here is pay someone a high salary to carry out something. About 50% of the real top guys has their own company and is not cheap to hire.

Whereas the average payment to the non trusted tictactoe coders is in the order of 3000-3900 euro (before tax) a month here. In yearsalary that's 12.96 * 3600 on average = 46k - 50k euro a year. With that they're UNDER NEATH the average salary in IT here which is 52k euro a year. To compare with USA and Germany you have to add to this the pension payment the employer is doing.

Their boss is making about factor 2.6 to 3.0 on them. So catches a 150k a year a coder.

Of course they're versatile. Have a driving license and know how ipod with ears works, as well as know how to write some scripts, know a tad of oracle and such.

The fact they carry out those projects with overly large teams means that small companies never ever get access to selling themselves to such project.

This is what messes up, at least in The Netherlands, most programming teams.

If i look further then, then you see that lots of smaller companies use guys who have not too much of an education who code the C++ in companies. Note that most of them write pretty ok code if they want to. The word 'speed' or 'algorithms' one shouldn't use though. To compensate for not having had a good education, they selftaught themselves more and explore in their bosses code basically everything Bjarne Stroustrup showed up with when he was having a brainwave while herding some sheep in Iceland.

They are actually pretty happy with the above salary.

Basically if C++ was C with classes, no exceptions, no iterators and so on, then it would be useable, and arguably better than C. That's what the first of C++ used to be like. I think C++ degenerated with the C++ 98 standard, and now C++0x is just ridiculously complex.

Newbies are typically attracted by the nice looking OO syntax, and using STL. But C++ is very tricky and deceptive. It is the devil's invention if you ask me.

And I don't say that out of ingnorance. In fact, after mastering C, I wanted to learn C++. And at the beginning I really loved it, and then I started indulging in template programming, and using more STL, even exceptions... The more I learnt all that non sense, the more I realized my productivity was decreasing, and I was spending much more time figuring out whether I should use a std::iterator<std::vector<std::string>>, what an iterator actually was, whether I could write my own iterator derived classes etc. when really looping with an int is fine... And they say C++ solves the technical details for you and helps you focus on the problem at hand: it's exactly the opposite!

In fact I started DoubleCheck in C++, and it was starting to get quite hairy, and object oriented. I rewrote it in C, and ever since then I really regret nothing from C++, except a few things (maybe default value in functions, function overloading would be nice in C, but the rest is BS).

If you want to write a program that manipulates complex date structures, you can write it in C (painful), or in C++ (apparently painless but appears deceptive quite quickly). But I think such programs should neither be in C nor in C++, but should be written in a proper high level language or a function language: Python is probably a good compromise.
A discussion on what is useful or not i don't think interesting personally.

In general a language like c++ is useful if you use one of its powerful features only if there is no neat way of doing it in more simple to read code.

Again i do realize most of the C++ programmers posting on this forum write code like that - that's not the average unqualified for tic-tac-toe c++ guru coder.

If you want the best you shouldn't just look for guys who are wiling to get paid little and who get through every psychological test as 'nice personality'.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

Aleks Peshkov wrote:Any sane C-code should compiler using C++ with the same result in speed and size.

In many cases C++ source code would be shorter to identical C-variant.

Simple C++ code could be faster then C because of stricter type checking, const-correctness and using references instead of pointers.

C++ have better resource management due destructors and RAII idiom.

C++ library designed to eliminate possibility of bugs of buffer overflows and memory leaks.
Why do you post your google results here?
ZirconiumX
Posts: 1359
Joined: Sun Jul 17, 2011 11:14 am
Full name: Hannah Ravensloft

Re: Proper way to learn c ?

Post by ZirconiumX »

diep wrote:
lucasart wrote:
diep wrote: Just forgetting a semi colon behind a class already can result in a nightmare!
Oh yes, I have some nice memories of that. You can spend hours on it, and never find the problem.
diep wrote: My conclusion is that C++ is a very usable language if you would strip it down back to the type of typical C++ code you saw start 90s.
Yes, if only that was possible... Bjarne often says that "yes C++
is complicated, but what you don't use, you don't pay for". This is such a lie it's not even funny. To be able to write C++ code that will be as fast and reliable as C code, you basically need to be a C++ guru,
2 remarks:

a) most company code is deeply layered in C++. 10 layers is nothing.
No compiler manages to optimize that well. It's turtle slow. Factor 100, as you can see in adobe software a lot, that's a slowdown we shouldn't even discuss - factor 1000 is more worrying yet that happens a lot as well.

That's code designed by exclusively guru's.

b) you really underestimate how fast the code is that some of the top guys program. For example if we line up all C++ programmers from this forum, and though i program lots of C++ code, with prospects that only the Diep engine is C and everything else i do is C++. So i would not go stand in that row in fact. Yet from that entire row, just 1 guys C++ is gonna be a lot faster than anyone else and that's of course Anthony Cozzie's C++.

Realize how difficult it is to get huge speed out of C++ even at something, from code size seen, simple as a chess engine.

Those guru's, which this forum has quite some of, they have the habit to already by default layer things. That is very nice and it's considered good habit in C++. However it already means you're gonna be slower. Not necessarily but if you add it all up, most of those guru's code, the ones i helped speedup their c++ code, always could pour out more than factor 2 speed relative easily.
and know all the details of what the compiler is doing behind your back (object copies on the fly, exceptions thrown by STL or even the new and delete operators, how to not leak memory when exceptions are thrown etc.)
I always wonder why people are busy with STL. There is commercial libraries out there way more interesting than STL (and more expensive, besides a lot of contracts you have to sign to get access to that huge codebase) for specific purposes and superior C++ quality.
And then they go on saying that C++ is easier than C for beginners, as it's object oriented and has some nice STL... It is a nightmare for newbies, and they spend most of their time trying to figure out the subtilties of the language, understand why their program leaks memory, and how to manage exceptions,
Am i seeing it correct now you confuse c++ with java?
Why doesn't that surprise me?

In java you always have to catch exceptions - not in C++.

If you write bugfree code like i do - which exceptions? :)
why they can't call virtual functions in a constructor and so on... If all programmers were gurus, and had the skills to write a C++ compiler themselves, then yes, C++ would be OK for them to use. But the reality is that 90% of programmers are incompetent, and incompetent programmers are typically drawn into using C++, because the C++ evangelists tell them how easy it is.
The problem is not so much the language here nor the guru's. the problem is that the interesting software projects get carried out by companies here which i would call incompetent in carrying out such projects, as they have the habit to first fly in a team that setups a prototype with less than half a dozen competent persons. From that half a dozen team already 1 is a teamleader and 1 is a manager and 1 is a salesman, so usually the programming team is 2-3 persons or so. Real little.

After that they sell the code to a major company and get a 'go' for the project, usually government over here. Then they throw in another 50 persons whom i wouldn't trust writing tic tac toe bugfree to 'finish' the project.

That's how it works.

The companies getting such contract jobs in general have problems breaking even with persons older than 35 years old and earning more than 3000 euro (before tax) a month. That's why the persons working there don't make that much more. In general there is 1 manager overlooking a person or 80.

In reality this government department or big company could better hire 3 real good top coders and pay them a huge salary, but that's not how reality works. The last thing they would do here is pay someone a high salary to carry out something. About 50% of the real top guys has their own company and is not cheap to hire.

Whereas the average payment to the non trusted tictactoe coders is in the order of 3000-3900 euro (before tax) a month here. In yearsalary that's 12.96 * 3600 on average = 46k - 50k euro a year. With that they're UNDER NEATH the average salary in IT here which is 52k euro a year. To compare with USA and Germany you have to add to this the pension payment the employer is doing.

Their boss is making about factor 2.6 to 3.0 on them. So catches a 150k a year a coder.

Of course they're versatile. Have a driving license and know how ipod with ears works, as well as know how to write some scripts, know a tad of oracle and such.

The fact they carry out those projects with overly large teams means that small companies never ever get access to selling themselves to such project.

This is what messes up, at least in The Netherlands, most programming teams.

If i look further then, then you see that lots of smaller companies use guys who have not too much of an education who code the C++ in companies. Note that most of them write pretty ok code if they want to. The word 'speed' or 'algorithms' one shouldn't use though. To compensate for not having had a good education, they selftaught themselves more and explore in their bosses code basically everything Bjarne Stroustrup showed up with when he was having a brainwave while herding some sheep in Iceland.

They are actually pretty happy with the above salary.

Basically if C++ was C with classes, no exceptions, no iterators and so on, then it would be useable, and arguably better than C. That's what the first of C++ used to be like. I think C++ degenerated with the C++ 98 standard, and now C++0x is just ridiculously complex.

Newbies are typically attracted by the nice looking OO syntax, and using STL. But C++ is very tricky and deceptive. It is the devil's invention if you ask me.

And I don't say that out of ingnorance. In fact, after mastering C, I wanted to learn C++. And at the beginning I really loved it, and then I started indulging in template programming, and using more STL, even exceptions... The more I learnt all that non sense, the more I realized my productivity was decreasing, and I was spending much more time figuring out whether I should use a std::iterator<std::vector<std::string>>, what an iterator actually was, whether I could write my own iterator derived classes etc. when really looping with an int is fine... And they say C++ solves the technical details for you and helps you focus on the problem at hand: it's exactly the opposite!

In fact I started DoubleCheck in C++, and it was starting to get quite hairy, and object oriented. I rewrote it in C, and ever since then I really regret nothing from C++, except a few things (maybe default value in functions, function overloading would be nice in C, but the rest is BS).

If you want to write a program that manipulates complex date structures, you can write it in C (painful), or in C++ (apparently painless but appears deceptive quite quickly). But I think such programs should neither be in C nor in C++, but should be written in a proper high level language or a function language: Python is probably a good compromise.
A discussion on what is useful or not i don't think interesting personally.

In general a language like c++ is useful if you use one of its powerful features only if there is no neat way of doing it in more simple to read code.

Again i do realize most of the C++ programmers posting on this forum write code like that - that's not the average unqualified for tic-tac-toe c++ guru coder.

If you want the best you shouldn't just look for guys who are wiling to get paid little and who get through every psychological test as 'nice personality'.
Thank god Marco Costalba hasn't responded about this kindergarten yet.

As I said in my thread 'take it to Steven Edwards where you will be shot down in PASCAL flames.'

Matthew:out
tu ne cede malis, sed contra audentior ito
Rein Halbersma
Posts: 751
Joined: Tue May 22, 2007 11:13 am

Re: Proper way to learn c ?

Post by Rein Halbersma »

diep wrote: My conclusion is that C++ is a very usable language if you would strip it down back to the type of typical C++ code you saw start 90s.
Have you actually use modern C++ (the 2011 Standard) with things like auto, lambda, shared_ptr etc? It is a totally new language and not at all difficult to program error-free.
google still isn't hte average company there of course - here in europe no company for a team of coders would be willing to pay salaries above say 60k euro a person a year. So you simply won't be able to assemble a good C++ team EVER.
I agree with you: with 90s style C++ it is very hard to generate productivity that is worth 60K a year. That is why C++11 is currently so actively being taught by Microsoft, Facebook and at BoostCon (did you watch those presentations?): because it makes programmers more productive.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Proper way to learn c ?

Post by Sven »

Rein Halbersma wrote:
diep wrote: My conclusion is that C++ is a very usable language if you would strip it down back to the type of typical C++ code you saw start 90s.
Have you actually use modern C++ (the 2011 Standard) with things like auto, lambda, shared_ptr etc? It is a totally new language and not at all difficult to program error-free.
google still isn't hte average company there of course - here in europe no company for a team of coders would be willing to pay salaries above say 60k euro a person a year. So you simply won't be able to assemble a good C++ team EVER.
I agree with you: with 90s style C++ it is very hard to generate productivity that is worth 60K a year. That is why C++11 is currently so actively being taught by Microsoft, Facebook and at BoostCon (did you watch those presentations?): because it makes programmers more productive.
I propose not to argue with someone who lives in another universe :-)

Also, you get 5kB reply in each post from there ...

Sven
Rein Halbersma
Posts: 751
Joined: Tue May 22, 2007 11:13 am

Re: Proper way to learn c ?

Post by Rein Halbersma »

Sven Schüle wrote: I propose not to argue with someone who lives in another universe :-)

Also, you get 5kB reply in each post from there ...

Sven
Ah very true. But I have a soft spot for Vincent because his double null move idea is doing quite nicely for my draughts engine.