Page 1 of 2

Objective C vs C for kernel space?

Posted: Wed Jun 03, 2009 8:22 pm
by thomashc
Which would be better? Me and a few others are writing a kernel and we'd like to know how C stacks up against Objective-C. None of us really like C++.

Re: Objective C vs C for kernel space?

Posted: Wed Jun 03, 2009 8:38 pm
by Firestryke31
IMO Obj-C is C++ with a bizarre syntax. There are probably things native to Obj-C that C++ doesn't do, just like there are probably things C++ does that Obj-C doesn't do.

Given the choice, I personally would choose C++ due to the not-freakish syntax, but that's my choice.

Re: Objective C vs C for kernel space?

Posted: Wed Jun 03, 2009 9:01 pm
by thomashc
Thanks, but like I said we DO NOT want to use C++.
Mainly we will probably end up using C and Objective C. However, we'd like to know which would be better for kernel space.

Re: Objective C vs C for kernel space?

Posted: Wed Jun 03, 2009 9:31 pm
by whowhatwhere
thomashc wrote:Thanks, but like I said we DO NOT want to use C++.
Mainly we will probably end up using C and Objective C. However, we'd like to know which would be better for kernel space.
Don't.

For one, the runtime itself is over 800KB of just source code in C. Then add all the extras and it's a waste.

Re: Objective C vs C for kernel space?

Posted: Wed Jun 03, 2009 9:46 pm
by Firestryke31
Any particular reason you don't want to use C++? It's not like it's any harder to write the kernel in than C, and you don't have to implement the entire std namespace just to get the kernel working.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 12:45 am
by kop99
Any way, linux kernel is made by c.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 1:01 am
by Solar
Not much of an argument, that one. Hey, Singularity is written in C#. So what?

But I'd advocate for C, too, if only for the sheer popularity of the language. In the very least, there are many more people competent to help you with a C problem than there are for Obj-C.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 1:14 am
by Colonel Kernel
The Objective-C syntax is ok once you get used to it. I actually find it helpful to have whitespace between objects and methods to emphasize that they are not tightly bound to one another. Also, the square brackets remind me of envelopes -- a nice visual reminder of the message-passing nature of the language. :) YMMV.

However, I don't think using Objective-C in a kernel is at all useful. BTW, this has all been discussed before here:

http://forum.osdev.org/viewtopic.php?f= ... bjective+C

@Solar: C# has something important that Objective-C lacks: A very large subset that is verifiably type-safe. Also, its virtual method dispatch speed is on par with that of C++, while Objective-C message passing is quite a bit slower, although more flexible. However, in a kernel the speed is more important than the flexibility.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 1:38 am
by inx
Firestryke31 wrote:IMO Obj-C is C++ with a bizarre syntax.
IMO C++ is Obj-C with a bizarre syntax. Any C source compiles perfectly as objective C, they just added classes and the supporting
syntax. C++ not being any harder is entirely dependent on your background. C++ is not simply an extension to C, it is its own
C-like language.

That aside, I do agree that Objective-C is probably a waste for in-kernel use. Objective-C is very introspective, which takes a lot
of runtime support, and allows for a lot of flexibility. This is why the runtime is so gargantuan, and also why it doesn't make much
sense for kernel mode.

While you could write a pared down runtime that does not support a lot of the introspection and dynamism which is part of
Objective-C, you would most likely end up having to do major modifications to the compiler just to remove functionality.
Personally, my way around this has been to just use C in an object-oriented manner in-kernel, and Objective C in userland.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 3:09 am
by Solar
I always enjoy a discussion on the pro's and con's of programming languages, as you tend to learn some from them. Like, that C source can be compiled with an ObjC compiler.

But that's also true for C source and a C++ compiler, by design, so I don't think it's fair to say that ObjC is an extension to C while denying C++ that label.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 3:51 am
by inx
Solar wrote:But that's also true for C source and a C++ compiler, by design, so I don't think it's fair to say that ObjC is an extension to C while denying C++ that label.
That was ignorance on my part, I was not aware of that. Thank you for the clarification.

I kind of messed with picking up C++ a few years back, and thought I remembered some sort of odd casting problem, but I tested a couple things, and
it was entirely a misconception on my part. I probably just had some difficulty with operator overloading or something. Never did quite understand the
point of that.

Anyway, looking at it from this renewed viewpoint, Objective C's syntax is more bizarre than C++'s, coming from a C function-calling standpoint. On the
other hand, however, as Colonel Kernel pointed out, sending a message to an object in Objective C is not cognate to calling a method in C++. There
are fundamental differences in the underlying architecture, which again comes back to Objective C's massive runtime dependence and the more
active participation of the Objective C runtime compared to most other compiled languages.

</thread hijacking> =p

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 5:16 am
by quanganht
C is better choice

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 7:28 am
by steveklabnik
Solar wrote:But that's also true for C source and a C++ compiler, by design, so I don't think it's fair to say that ObjC is an extension to C while denying C++ that label.

Objective C is a strict superset of C, whereas C++ is not. Therefore, Objective C is a C extension, and C++ isn't. It's a pretty fair thing to say.


That said, while Objective C is a pretty cool language, I'm not sure that it'd really be any better than just straight up C for kernel development. That shouldn't stop you from doing so, though...

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 8:28 am
by whowhatwhere
When I was interested in kernel-space Objective-C, I had an idea for method dispatch that used co-routines that I thoroughly believed would be faster and better suited to system-level programming. Then I saw the runtime and how many ugly hacks were put to use just to make it work with obscure architectures and quickly put down that idea. Don't try it, believe me. It's horrid.

Re: Objective C vs C for kernel space?

Posted: Thu Jun 04, 2009 9:12 am
by Solar
steveklabnik wrote:Objective C is a strict superset of C, whereas C++ is not. Therefore, Objective C is a C extension, and C++ isn't.
Because you say so, or because of some facts you're just not willing to share with us?