Proper way to learn c ?

Discussion of chess software programming and technical issues.

Moderator: Ras

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:
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.
All the novelties introduced since 1999 into C++ and some stuff that made it into the 1999 standard have dramatically deteriorated C++.

a) made it more complex
b) i have hard evidence of that the many features of C++ let students mess up more than in C.

I'll give one obvious example. One student project i had carried out, the students programmed a program in C++. One of the programmers actually had written C++ codes before and not little. So several years experience in programming, at least one of them.

After some months, their code crashed everywhere. Lots of communication from me to them and vice versa, they just didn't get it working. in the code i saw of course their bugs. they used templates whre they didn't need a t emplate at all.

Just because their C++ book says that templates are one of the 'advantages of C++ over C'.
Sure, if you know how to bugfree use them and if you have a spot where you can use 'em.
Then i talked to them and they agreed translate the code to C code.
That's what they did.
Within 2 days it ran without crashing.

Weeks of debugging from their side the C++ code didn't help, but just 2 days converting it to C and a bit of debugging got enough bugs out of it to find it acceptable.

That's the hard truth about C++.

I hope you realize that when i was at university and in functional languages professors were more optimistic about lambda type notations than the devil about hell. When i saw some years ago that C++ nowadays has that to i realized C++ had gone to the devil as a language.

With that concept i wrote a checkers program in a functional language called gofer.

As it was so slow, i also translated it to C language within a week and that was factor 10k faster.

then a PHD took 6 months time to study my small project in gofer, note that i avoided of course haskell notation as that is IMPERATIVE. I wanted it to be functional.

After this 6 months he managed to improve it by factor 2 in speed.

My C code checkers program, i spend then another day to and it was also a factor 2 faster ,resulting again in a speed difference of factor 10k.

Actually you could also compile the gofer to C (turbo C at the time).

That limited the factor 10k a bit, but it was a big effort to translate it from gofer to C and then compile it. That was about factor 60 slower.

All this are highly theoretical concepts that require a high IQ.
For guys in an university who have all time of the world it might challenge their mind to be capable in a language with high level concepts that THEY understand, but a company is there to make a profit.

I'll tell it even stronger to you. The aim of a company as defined in the definition of a company is that it is striving after profit.

So they want to make a profit, they are not charity organisations.

In short they want to get things done.

Now if in YOUR time you prefer to challenge your mind and toy with things that slow down your code, i suggest you do that.

But considering the tons of new features in C++ to *somehow* improve productivity, whereas by definition of what IQ is, we can prove that the new features demand a higher IQ, then your statement is of course far from the truth.

Truth is that in order to bugfree program being busy imperative is most effective and most simple way of getting things done.

So the more features you add to a language, regardless how enthusiastically a few nerds are about that, it won't improve the language from companies viewpoint.

From that viewpoint i fully understand that many companies standardized upon java and have simply kicked out or are replacing all their C++ codes by Java, as after all, nowadays most coding work is what was the factory worker of some decades ago.

Within all that coding work, the guru's fix the most complicated things, yet that shouldn't take care kicking out the average coder. He should be able to also find his way in the guru's code.

It's obvious that you don't care about all this. Feel free, we live in a society where having different thoughts is considered productive :)

If i would be a manager there, i would do exactly the same though, and kick out all the C++ codes and replace it by simplistic java codes made by highschool level coders. Cheaper and it works.

That said, they're also trying to increase difficulty of java and i'm not sure whether that's a rather good idea, yet i try to stay away from java as far as i can, as i won't get well paid contract jobs there.

As for programming low level, C++ can be used to do that, but it isn't easy.
Too many ways to slow you down. 99% of the C++ programmers will fall for one of those temptations.

The fact that most of the guru's make no chance in hell low level against a guy like me, whereas most of what i've been doing has focussed upon algorithms, should be a warning for you that there is something fundamental wrong with C++.

The idea of object orientation is just plain wrong for todays hardware, that's the underlying problem.

You can try to make 1 language that's object oriented and also have enough modifications to it, or have it that compatible that you also can have it function as an imperative language like C, yet it won't change the nature of the beast that object orientation is simply a concept that doesn't cast well onto todays hardware, not to mention GPU's where you have just 1-4 kilobytes of register data a streamcore and where you've got a very tiny instruction cache to feed all the streamcores the same code. At AMD gpu's you've got 8 kilobyte L1i, and at Nvidia a tad more but not that much more (you can chose there how to divide your L1), note you lose L1 then if you choose more instructions.

And the L2 isn't doing a great job to host instructions, unlike it is doing so great at todays cpu's.

So there is an increasing gap between object orientation and reality of hardware.

Note the same criticism of object orientation is for Java.

So for low level coding I stick to C for now :)
Daniel Shawul
Posts: 4186
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Proper way to learn c ?

Post by Daniel Shawul »

Also, you get 5kB reply in each post from there ...
I am sure zip will do a fine job compressing it .. :)
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Proper way to learn c ?

Post by mcostalba »

diep wrote: So for low level coding I stick to C for now :)
Well, I have to say that I find myself in agreement with Vincent (amazing! never tought to write this!).

If you have large masses of casual / low quality programmers and you have a big company code base and you want to keep the (possibly already huge) crap level from escalating even more than I agree Java or C# are viable solution to avoid the Joe's at the keyboards to shot themself in the foot. The fact that the code base is mainly used internally in the company and not sold outside helps a lot becuase tools for internal use normally have very low quality and only main code paths have been tested /works, and nobody cares.

I also agree that for low level coding C is more then enough and you can live happy with it. How many are the programs that can be called 100% low level application is another thing...perhaps less than what Vincent thinks.

But, being this a chess forum, it would be interesting also to focus this discussion on chess programming.

I don't know what is the best language. I even dont know if this question makes sense.

I am happy to code in C++ the chess engine becuase:

1) I really love that language

2) A chess engine is small, and is getting even smaller after each version (now we are at around 7000 lines of code). And you end up going again and again across the same code, every time changing and improving a small bit. This kind of developing process has nothing to do with "productivity" it has more to do with craftsman, and C++ is the craftsman's language.

3) Chess engine although small is not trivial and offers a lot of opportunities to really stretch the language at its best. It is a really magical balance between high level stuff and low level stuff.

4) C old timers tend to concentrate on low level optimizations but without any kind of guide line this yelds quickly to a crappy and awful mess of low level tricks that serve no purposes but doing the code base unmantainable, difficult to read and very rigid and difficult to change. I much prefer a clean, readable and naturally written code to a pile of bit tricks and obscure pseudo-optimimizations. It is true that indulging in OO or other very high level approaches without thinking can be naive and is a hint of a poor programmer. But is a poor programmer also the one that spend 90% of his time in writing low level tricks and hacks, completely loosing the big picture, the program structure and design that instead is the most important thing. The only difference between the two types of programmers is that the latter thinks to be a good one :-)
lucasart
Posts: 3241
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Proper way to learn c ?

Post by lucasart »

syzygy wrote:
mar wrote:
syzygy wrote:No, the normal way is to link against glibc, which will then invoke the kernel using a system call. Making a system call can only be implemented in assembly and you will not notice any difference between C and C++ there.
So what's the benefit of using C++ in kernel then? glibc is C, not? :wink:
The benefit of using C++ in the kernel would be that kernel developers could use C++ (if that is a benefit at all... Linus thinks it is not). Also developers of kernel modules could use C++.
Anyone who suggest this is an heretic and gets burnt to the stake: metaphorically speaking that is, IOW insulted on the Linux kernel newsgroup, and escorted to the door... And *not just by Linus*, but by all kernel developpers, which are way more competent than the average of even this forum. So they must have good reasons...

Being able to use C++ in Linux is not a benefit, all the contrary. And on top of things it attracts incompetent and arrogant programmers (who typically started with Java and then C++ and want to revolution Linux with OO BS and STL+exceptions). And as Linus explains, that is a very good reason in itself to forbid C++ in the kernel: keeping the idiots out !

Just to give you an idea iof how C++ can mess up a project:
portability issuesportability: https://developer.mozilla.org/en/C++_Portability_Guide
flaws of the language: www.fefe.de/c++/c%2B%2B-talk.pdf

Anyway, I'm very happy to see that the "dictatorship" of Linus is still alive. The day he goes, I hope his fellow kernel developpers will fight as hard as he did to keep the heretics out :wink:
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Proper way to learn c ?

Post by mcostalba »

lucasart wrote: And *not just by Linus*, but by all kernel developpers, which are way more competent than the average of even this forum. So they must have good reasons...
Yes Linux kernel developers are really monsters !! I had the chance to join their comunity for a little time some years ago when I have also sent some stupid patch to lkml and I can say that I have never meet such an high quality like there: their level was so much better than mine that I really felt like they come form another universe (never felt like this before, and never felt like this after). I am not just talking of their C skills (C is anyhow an easy language to master), I am talking of their attitude and competence as programmers, something language inependent.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

mcostalba wrote:
diep wrote: So for low level coding I stick to C for now :)
Well, I have to say that I find myself in agreement with Vincent (amazing! never tought to write this!).

If you have large masses of casual / low quality programmers and you have a big company code base and you want to keep the (possibly already huge) crap level from escalating even more than I agree Java or C# are viable solution to avoid the Joe's at the keyboards to shot themself in the foot. The fact that the code base is mainly used internally in the company and not sold outside helps a lot becuase tools for internal use normally have very low quality and only main code paths have been tested /works, and nobody cares.
I didn't post that with much joy either, but majority of the code that gets written now, say around 95%, the coders doing that, they were the factory productionline workers from 100 years ago.

That many financials now also struggle with the fact that their codebase is so slow as a result of just hiring financials who 'by accident' can write some code in java as well, that's a different discussion :)

We must face however that nearly all software gets set up cheaply and that the larger the team the more unlikely it'll be in C++ nowadays, simply as companies do not want to pay for top programmers salaries and they care for maintainability.

In C++ you cannot garantuee maintainability as they don't know when they can hire the next c++ guru, if ever. Not in the last place because the C++ guru's have no time for serious software as they're too busy learning the next new features in C++ which have been written down, as it is so many new features, in a 1000 page a4 book.

You still can find some C++ coders in their end 30s, but for companies they prefer to hire new workers underneath age 35 and preferably just out of college.

They pay for .NET and Java and whatever new languages show up there that are slow and simple.

.NET and Java give that maintainability garantuee that C++ no longer gives.

C++ is putting itself slowly on the sideline there.
I also agree that for low level coding C is more then enough and you can live happy with it. How many are the programs that can be called 100% low level application is another thing...perhaps less than what Vincent thinks.
From that remaining 5% of there is simply a solid need for imperative low level programming language.

That happens to be C and will remain so for quite some time, as no bigger company has done effort to make an 'improved C' language.

The succes of .NET and Java and initially C++ was GUI programming. Whatever language can build me a great GUI is also the language i'll program. As my clients want basically interfaces, that's what i build and C++ happened to work for it.

That's also why C++ initially gained some popularity. You could use it for building an interface very well as the API's basically supported C++ very well and not C.

Right now that's .NET and Java, especially java.

Not my favourite language, but that's the fact. With each new addition to the C++ language a company will have more problems setting up new projects in C++.

In that sense C++ gets slowly pushed towards a similar small corner, be it a different corner of the programming spectrum, as Fortran.

That's reality.
[snip] But is a poor programmer also the one that spend 90% of his time in writing low level tricks and hacks, completely loosing the big picture, the program structure and design that instead is the most important thing.
C has a clear low level function.

C++ however digs in the same area where .NET and Java also dig and C++ is losing that fight.

The huge base of C++ codes that many companies have built up will not disappear just like that, yet look around you in the job market.

All sorts of types of well paid jobs that some years ago would have been carried out in C++, it's all Java now.
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: Anyway, I'm very happy to see that the "dictatorship" of Linus is still alive.
The day he goes, I hope his fellow kernel developpers will fight as hard as he did to keep the heretics out :wink:
Might Linus get a heart attack and drop dead to the ground, that's a day i will mourn.

What happens to many projects is that some sort of company tries to take over. After a while they sell that project or whatever, but the result is that suddenly they go charge money and go get commercial.

Linus avoided all that.

The guy after him, will he have more authority than a big company that takes over?

Democracy doesn't work there, as democracy means no one sticks out as the guy to follow him up. In a democratic fight like that a company always wins and they will do it a few years, slowly add masses of lines of codes of themselves to the source code base and then in the end the dollar signs in their eyes will win it.

Redhat and Suse having been taken over by commercial companies also gave major blows to the linux community.

Ubuntu is there now, but this guy who owns ubuntu also has sold his previous project commercially to a company.

Yet there is alternatives for distro's, new ones will keep popping up there.

There is no alternative for the linux kernel however.

For now the simple solution is keep Linus alive, even if that requires a javascript that runs his breathing equipment.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

stevenaaus wrote: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:
As for Nvidia - There is a problem with Nvidia simply and the linux kernel.

I've tried to setup some old hardware recently and had big problems putting in nvidia cards into the hardware.

It doesn't boot in textmode in fact even into linux.

That's not Linus fault.

In fact i had the probem with bunches of nvidia cards, different generations of cards from past years and it all was just a textmode issue, as my ftp server and firewall are exclusively in textmode.

Using debian for that.

In the end i put in both servers videocards not manufactured by Nvidia.

If you realize i have a bunch of Tesla's here produced by Nvidia, donated by Nvidia to me, you get the feeling...
syzygy
Posts: 5713
Joined: Tue Feb 28, 2012 11:56 pm

Re: Proper way to learn c ?

Post by syzygy »

lucasart wrote:
syzygy wrote:
mar wrote:
syzygy wrote:No, the normal way is to link against glibc, which will then invoke the kernel using a system call. Making a system call can only be implemented in assembly and you will not notice any difference between C and C++ there.
So what's the benefit of using C++ in kernel then? glibc is C, not? :wink:
The benefit of using C++ in the kernel would be that kernel developers could use C++ (if that is a benefit at all... Linus thinks it is not). Also developers of kernel modules could use C++.
Anyone who suggest this is an heretic and gets burnt to the stake: metaphorically speaking that is, IOW insulted on the Linux kernel newsgroup, and escorted to the door... And *not just by Linus*, but by all kernel developpers, which are way more competent than the average of even this forum. So they must have good reasons...
Note that I was only giving a theoretical answer to a theoretical question. My point is that it only makes a difference for kernel-level programming. (There was apparently some perception that a C++ kernel could not run C programs, which of course is wrong.)
Just to give you an idea iof how C++ can mess up a project:
If you carefully read what I write, you don't have to give me any ideas....
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Proper way to learn c ?

Post by mcostalba »

diep wrote: In that sense C++ gets slowly pushed towards a similar small corner, be it a different corner of the programming spectrum, as Fortran.

That's reality.
I agree, but it's a nice corner.

C++ is required by big and technically demanding applications: game developing, platform applications like browsers, databases, wordprocessors, compilers, CAD, etc. A corner where you need skilled and professional (not casual) programmers and where C simply does not seem the best fit due to lesser attitude at modelling the problem space in an effective way at such big and complex scales.

C++ is not for everybody and is not the best choice for every scenario: but this IMHO is not a limit.