Proper way to learn c ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Proper way to learn c ?

Post by mar »

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:
syzygy
Posts: 5563
Joined: Tue Feb 28, 2012 11:56 pm

Re: Proper way to learn c ?

Post by syzygy »

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

For application developers it would not make any difference at all. They would never notice.

Glibc is in C (with some assembly). The glibc code is separate from the Linux kernel code. Glibc is also used on other OS kernels such as GNU Hurd and maybe some BSDs (but I'm not sure of that).
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Proper way to learn c ?

Post by mar »

The benefit of using C++ in the kernel would be that kernel developers could use C++
Reminds me of absolute rhyme :)
No seriously I don't see any benefit in that. They can write kernel in basic if they want, but it will interface through a C library. Do you see the irony?
EDIT: couldn't resist: http://www.youtube.com/watch?v=Jne9t8sHpUc
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

Sven Schüle wrote:
mar wrote:Huh. C++ is a superset of C. So I really don't see why Torvalds wanted to rewrite the kernel in C++,
that's completely stupid as it would break existing C programs. :shock:
I see two reasons why that "C++ rewrite" of the Linux kernel failed:

1) You cannot simply "rewrite" something as complex as an OS kernel in C++ unless you also redesign it. A procedural-style software of that magnitude will remain procedural even if you introduce some classes, inheritance, something here and something there. I don't say that Linus made no attempt of a redesign but at least "rewriting in C++" does not suggest to me he did so.

2) In many cases the best programming language for a given task is the one that a developer likes most, and is most experienced in.

Sven
You forgot biggest problem

3) when doing kernel in C++ you need a machine with 64GB RAM and a single core at 100Ghz, as it'll not only be factor 1000 slower as dudes with those deep layered kernel primitives will mess up in C++ of course resulting in triggering everywhere allocates and deallocates, but also it'll eat factor 1000 more RAM :)

Now of course C is a high level language and not that fast like assembler. Windows kernel team seems to have optimized critical functions in assembler, which shows itself in some specific cases, it's factor 2 to 3 faster there. Yet such performance we won't soon see as it already means by definition something is hurting you bad :)

I really agree with Linus on this subject. Speed and RAM consumption is too critical to even consider object orientation.

The fundamental problem of object orientation is that what makes it a nice language also is one of the slowest things on the processor - namely allocating and deallocating objects.

The idea of having things private and/or protected no matter how nice the thought is, also means you don't know what happens with the deep down layered functions, and kernel sure has deep layers.

So you don't know simply what actually gets triggered. Debugging that would be a total nightmare and only Linus would do it anyway.

It would be possible in theory to create a good kernel in c++. Yet that's a pipe dream, as at most only 1 in each 10000 C++ programmers is good enough to program at that level. And from all the guys who know c++ here maybe 1 or 2 would be good enough. 90% of all c++ programmers posting here simply isn't good enough low level to program such kernel. Knowing this is already the topguys at many spots of the programming foodchain, that's telling you the big fundamental problem for such project.

That said - it's true that i believe the kernel must be made more safe, yet you have to admit Linus is 100% correct here in C versus C++.

Please note that from the last 100k lines C/C++ i programmed, about 90k were C++ codes...
Last edited by diep on Wed Jul 11, 2012 2:49 am, edited 1 time in total.
syzygy
Posts: 5563
Joined: Tue Feb 28, 2012 11:56 pm

Re: Proper way to learn c ?

Post by syzygy »

mar wrote:
The benefit of using C++ in the kernel would be that kernel developers could use C++
Reminds me of absolute rhyme :)
No seriously I don't see any benefit in that.
Then I guess you are not a kernel developer wishing to develop in C++. I'm not either. I'm also not asking the Linux kernel to be converted to C++.
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Proper way to learn c ?

Post by mar »

syzygy wrote: Then I guess you are not a kernel developer wishing to develop in C++. I'm not either. I'm also not asking the Linux kernel to be converted to C++.
Then I'm glad we agree.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Proper way to learn c ?

Post by diep »

mar wrote:
syzygy wrote: Then I guess you are not a kernel developer wishing to develop in C++. I'm not either. I'm also not asking the Linux kernel to be converted to C++.
Then I'm glad we agree.
C++ would be the ideal language to create security holes for organisations and companies which the coming 1000 years wouldn't get found.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Proper way to learn c ?

Post by lucasart »

Daniel Shawul wrote:
The original post is about someone who wants to learn C, so we pointed out some of the best material for that.
Yes and that someone also knows two OOP languages. This is not another this language is better debate. Infact you will have more knowlege about optimization if you work your way from a high-level language not some pseudo-assembly like C. Get it?
But regarding your ill-advised notion that C is disguised assembly and C++ is a high-level language, maybe Linus Torvaldes can enlighten you. And before you use that argument: Linus knows C++ better than any of us, and he has at one point rewritten the kernel in C++, which helped him to understand why this language is a total abortion (he then rewrote it back in C, what a waste of time):
Calm down. Your opinions seem to come from worshiping what someone says without even considering why he said it. Ofcourse he is not going to say C++ is better when he is already written his kernel with C 8-) But you missed the whole point of my argument. Why go low level specially when the OP knows two OOP languages ? Why don't you write your program with FORTRAN which has no pointers and will definately be faster ? And better yet why not assembly?
Read this and tell me again that C++ is great:
www.fefe.de/c++/c%2B%2B-talk.pdf
I love the warmup:

He got the following error

Code: Select all

readply.cpp&#58;109&#58; conversion from ‘_List_iterator<list<basic_string<c
har,string_char_traits<char>,__default_alloc_template<true,0> >,allo
cator<basic_string<char,string_char_traits<char>,__default_alloc_tem
plate<true,0> > > >,const list<basic_string<char,string_char_traits<
char>,__default_alloc_template<true,0> >,allocator<basic_string<char
,string_char_traits<char>,__default_alloc_template<true,0> > > > &,c
onst list<basic_string<char,string_char_traits<char>,__default_alloc
_template<true,0> >,allocator<basic_string<char,string_char_traits<c
har>,__default_alloc_template<true,0> > > > *>’ to non-scalar type ‘
list<basic_string<char,string_char_traits<char>,__default_alloc_temp
late<true,0> >,allocator<basic_string<char,string_char_traits<char>,
__default_alloc_template<true,0> > > >’ requested
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&#40;list<string>& x&#41; &#123;
sort&#40;x.begin&#40;),x.end&#40;)); // wrong iterator type
&#125;
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?
kbhearn
Posts: 411
Joined: Thu Dec 30, 2010 4:48 am

Re: Proper way to learn c ?

Post by kbhearn »

That pdf is a tad lopsided.

Templates are a handy way to not need to replicate almost-identical code. Yes, their compile errors are often verbose and unreadable, but that can be and has been improving.

Providing pathological cases of where the syntax is confusing is pointless - you first have to answer the question of why someone decided to write it that way in the first place (answer: they wanted to rant about how much better their preferred language was).

Most features in C++ can be avoided if you don't like em. If you just want the handiness of constructors/destructors for your structures and operator overloading for types that are pretty close to primitive, that's an option. If you want to add virtual functions in for a particular class, you can do that, with much easier to read syntax than C function pointers. If you come across a set of functions that you want to run slightly differently in a finite number of contexts there's templates with the runtime efficiency of separate functions and the code advantage of no duplication.

If you're doing complicated inheritance relationships, then i guess you're either making a library or you're a masochist, because in general there's no reason you need to deal with that headache other than to provide a slick API to other programmers.
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: Proper way to learn c ?

Post by Aleks Peshkov »

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.