learning c/c++
learning c/c++
Until now, I've been writing my os in assembly simply because I don't know c or c++, but I'm tired of scrolling through multi-thousand line files
If I won't be using the standard libraries, what do I need to learn to rewrite the kernel in c++?
PS, I already know java and C#, so...
If I won't be using the standard libraries, what do I need to learn to rewrite the kernel in c++?
PS, I already know java and C#, so...
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
If you're new, check this out.
Re: learning c/c++
Use IDE supporting collapsing code (for eaxmple WinAsm)m12 wrote:.... but I'm tired of scrolling through multi-thousand line files
There are many tutorials on the web.... cprogramming.com or Wikibooks
Re: learning c/c++
At the risk of giving the obvious answer - you need to learn C. If you don't know C or C++ I'd give the idea of writing an OS in C++ miss - it's perfectly possible, but has complications that you probably don't want to get into at this stage.
- Griwes
- Member
- Posts: 374
- Joined: Sat Jul 30, 2011 10:07 am
- Libera.chat IRC: Griwes
- Location: Wrocław/Racibórz, Poland
- Contact:
Re: learning c/c++
If you know C# and Java and want to write something in proper C++, unlearn everything you've learned. Same holds for C.
C and C++, when used without standard libraries, are pretty similar. The main differences are:
1) You have real constructors and destructors, so RAII is possible.
2) You have templates (never underestimate a compile time Turing-complete language!).
3) You have easy to use polymorphism.
4) You have namespaces.
5) You have overloads.
So, basically, to write a *kernel* in C++ (as opposed to writing it in C), you'd have to learn at least those things *and* C. Of course, knowing more things in detail can be really helpful, so I would advise also writing some userspace applications in modern C++; also, there should be some serious style differences between code written in C and in C++.
C and C++, when used without standard libraries, are pretty similar. The main differences are:
1) You have real constructors and destructors, so RAII is possible.
2) You have templates (never underestimate a compile time Turing-complete language!).
3) You have easy to use polymorphism.
4) You have namespaces.
5) You have overloads.
So, basically, to write a *kernel* in C++ (as opposed to writing it in C), you'd have to learn at least those things *and* C. Of course, knowing more things in detail can be really helpful, so I would advise also writing some userspace applications in modern C++; also, there should be some serious style differences between code written in C and in C++.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Re: learning c/c++
Hi,
No point using C++ if you're just going to treat it as a superset of C. No point going with C if you're just going to end up implementing the equivalent of C++ design time / runtime support.
Cheers,
Adam
It very much depends on how C or C++ fit in with your design and existing programming style. Also, would you find one or other of these languages more useful that the other outside the OSDev world?iansjack wrote:If you don't know C or C++ I'd give the idea of writing an OS in C++ miss - it's perfectly possible, but has complications that you probably don't want to get into at this stage.
No point using C++ if you're just going to treat it as a superset of C. No point going with C if you're just going to end up implementing the equivalent of C++ design time / runtime support.
Cheers,
Adam
- Griwes
- Member
- Posts: 374
- Joined: Sat Jul 30, 2011 10:07 am
- Libera.chat IRC: Griwes
- Location: Wrocław/Racibórz, Poland
- Contact:
Re: learning c/c++
Actually, all the features I listed in my post are reasons why C++ is nicer to work with, even when used *mainly* (not totally, of course) as superset of C.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Re: learning c/c++
I'd rather use C11 than the superset of C89 implemented by C++. </flamebait>
(Yes, templates could be really useful at times. But giving up designated initialisers...?)
(Yes, templates could be really useful at times. But giving up designated initialisers...?)
Re: learning c/c++
Hi,
I'm NOT saying that C++ is bad (it is, but I'm not saying it ). What I am saying is that C is a lot closer to assembly and a lot easier for an assembly language programmer to get used to.
Cheers,
Brendan
Agreed; but if m12 is used to using assembly then I think it's very unlikely that m12's design and existing programming style are going to suit C++.AJ wrote:It very much depends on how C or C++ fit in with your design and existing programming style. Also, would you find one or other of these languages more useful that the other outside the OSDev world?
I'm NOT saying that C++ is bad (it is, but I'm not saying it ). What I am saying is that C is a lot closer to assembly and a lot easier for an assembly language programmer to get used to.
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
- Griwes
- Member
- Posts: 374
- Joined: Sat Jul 30, 2011 10:07 am
- Libera.chat IRC: Griwes
- Location: Wrocław/Racibórz, Poland
- Contact:
Re: learning c/c++
C11 is "how to go The Wrong Way". Designated initializers looks nice, but I still prefer more obvious code (and constructors)
(I mean, "_Bool", "thrd_t" and countless others - seriously...?)
(I mean, "_Bool", "thrd_t" and countless others - seriously...?)
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Re: learning c/c++
It doesn't need to be in c++ if c is easier - I already have a specialized language thought out for applications - it's just not suitable for the kernel without pointers, and I'd rather leave pointers out of it for security purposes.
Also, I have no need for any standard libraries since everything but the kernel will be in the other language.
Mainly, I'm asking about syntax. Would something along these lines be valid (in C)?:
Also, I have no need for any standard libraries since everything but the kernel will be in the other language.
Mainly, I'm asking about syntax. Would something along these lines be valid (in C)?:
Code: Select all
#define SCREEN 0xb8000
void main(){
char* screen = SCREEN;
screen* = 0x53;
screen++;
screen* = 7;
}
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
If you're new, check this out.
- Griwes
- Member
- Posts: 374
- Joined: Sat Jul 30, 2011 10:07 am
- Libera.chat IRC: Griwes
- Location: Wrocław/Racibórz, Poland
- Contact:
Re: learning c/c++
Obviously not. Get a book.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Re: learning c/c++
I'd like to add anonymous structs and unions, since we're talking about C11.Kevin wrote:I'd rather use C11 than the superset of C89 implemented by C++. </flamebait>
(Yes, templates could be really useful at times. But giving up designated initialisers...?)
- Griwes
- Member
- Posts: 374
- Joined: Sat Jul 30, 2011 10:07 am
- Libera.chat IRC: Griwes
- Location: Wrocław/Racibórz, Poland
- Contact:
Re: learning c/c++
Not really, since OP is asking how to *fulfil* it.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Re: learning c/c++
Same result: This is the domain of plain C, the control flow won't become more obvious than there. C++ isn't about obvious code, but about convenient magic.Griwes wrote:C11 is "how to go The Wrong Way". Designated initializers looks nice, but I still prefer more obvious code (and constructors)
And I think "looks nice" gives designated initialisers and compound literals too little credit. They turn potentially long sequences of assignment statements into one declarative block, which again makes the code much more obvious. You can of course have an old-style initialiser or constructors with umpteen arguments and have the same degree of declarativeness, but this becomes quickly quite non-obvious because nobody can remember what the seventh parameter was meant to be without looking it up.
Yeah, names of built-in types are the most important criterion to choose a programming language, I agree.(I mean, "_Bool", "thrd_t" and countless others - seriously...?)