IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New OO Design in C
I've been doing a bit 'o work in C and while I am not an OO zealot, I do like some of the tools that OO programming offers me. I've loathed my experiences with C++, but I've read a couple of times that there are design patterns for doing OO style programming in C. I've only found a couple of actual implementations, though:

[link|http://www.eventhelix.com/RealtimeMantra/Object_Oriented/|http://www.eventheli.../Object_Oriented/]
[link|http://www.accu.org/acornsig/public/articles/oop_c.html|http://www.accu.org/...ticles/oop_c.html]

Neither of those is terribly exciting. While I don't expect to get inheritence or polymorphism, if anyone can point to some resources on this, it would be pretty nifty.

Mind you, I'm not terribly excited about Objective C either as I am trying to focus on things that pad my resume.
"If I heard a voice from heaven say 'live without loving',

I'd beg off. Girls are such exquisite hell." -- Ovid
New Hmm...If I were doing ObjectOrienteed in C
Depending on the level I was trying to achieve, I would...

Using standard C system multiple files, with static and private (local) members and accessor methods (data hiding techniques, some encapsulation) - however, you're looking at one file per object with no way to create objects on the fly.(*) Still it implements an interface, allowing you to change the implementation of the object at a later date without changing your main routine.

Use structs with void function pointers (nastier programming in my opinion), but that would allow you to create objects on the fly and encapsulate their member functions. (More object like - but I would use a factory to create these thingy)

But then again, I'm not sure what's that wrong with C++....(but that's just me)

(*) Correction - provide a typedef...

New Take a look at the source-code to Ruby
That demonstrates one way to do it...

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Lots of ways
The Motif, X11 junk is one way to do OO C. Its hideous.

As for padding resumes - ObjC is a lot more impressive than "I can make objects in C" as this claim is so nebulous as to be worthless (and more importatantly - it often implies the guys is clueless as to what objects are). But that is another topic.

Anyhow, one way to do it is to stick function pointers into your structs. This is useful for relatively limited sorts of "objects". Like maybe IO descriptors that need to look the same regardless of whether you are writing to a file, socket, memory, etc... You only have a few "methods" so you can afford the redundancy of function pointers in the struct.

If you care about space at all, your struct layout basically needs to have a common header that you can always count on (so at one level all structs look alike), then you put the differing stuff below that. The simplest thing done is to stick an isA pointer first that points to a "class" record of some sort. The isA points to the method dictionary and object layout info - often stored in a linked list of some sort.

If you want to see how Objective C works - go to the Apple developers site and download the runtime. There are struct definitions for what a class is, what a method descriptor looks like, info on keeping name offsets in a lookup table, etc.

There's also a book on "design and implementation of C++" something like that - this is a good read if you want to see how to do it wrong. FWIW, C++'s biggest design error (IMNAAHO) is the vtable and name mangling. Tossing out the method descriptor info and relying on positional info is just plain the stupidest thing ever in code that is going to live longer than one dev cycle (basically all code).

I could write a book on this but the best thing is to look at lots of implementations and decide what you need. X11 stuff, C++ implementation, ObjectiveC runtime, and (I think - I forget if this is right) unix IO descriptors store function pointers for some ops to implement the "everything looks like a file" illusion.




"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
New That reminds me of another thread here.
Ovid may want to look at [link|http://z.iwethey.org/forums/render/content/show?contentid=66834|this] thread here, and take a gander at [link|http://www.nongnu.org/needle/|Needle].

Just passing on a pointer, so to speak....

Cheers,
Scott.
New Re: That reminds me of another thread here.
Needle is still a work in progress, and isn't ready for prime time. But it's already making me giggle madly, so in a few months I'll have something ludicrously cool to share. :)
New Hold on here
Are you proposing to replace array of function pointers with a hash? Wouldn't it be a bit too slow? Are you saying that, say, Smalltalak does a lookup on method descriptor every time a message is passed?
--

It made Ketchup!
Sweet Ketchup!
Put it on a hot dog, put it on a burger,
Put it on your sister and she'll holler blody murder!
Sweet Ketchup.

--Tom Paxton.
New Not too slow
slower? OK sure.

We have GIGAHertz machines these days and smalltalk was originally working with decent performance on 8MHz boxes.

Now to be fair, ST has some tricks. One is that the very most common message sends to the most common objects are actually treated specially and implemented as byte code ops - its sort of the 80/20 rule. So for instance the #ifTrue: message

finished ifTrue: [ dosomething ]

is pedagogically implemented as a polymorphic message to Boolean with specializations in subclasses True and False - but this isn't how it *really* works. Instead the compiler generates special instructions for a small number of key messages (which you can change BTW). Loops are also treated specially.

Otherwise - yeah - there's a lookup. Its a pretty cheap lookup though.

Plus, having the methods in a method dictionary lets you do crazy things like create new kinds of scope, wacky fallback strategies, etc....

Smalltalk is an environment that will let you create pretty much any kind of programming model you like.






"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
New C++ also uses lookups...
...when you declare your functions as Virtual - which is really the only proper way to do methods for objects.
New No
C++ uses an index into an array of function pointers. Very fast, not terribly future-proof. Smaltallk (if I get it right) uses hashing on actual function signature. For most cases. My head is still spinning.
--

It made Ketchup!
Sweet Ketchup!
Put it on a hot dog, put it on a burger,
Put it on your sister and she'll holler blody murder!
Sweet Ketchup.

--Tom Paxton.
New Method Dispatch in Smalltalk
Smaltalk (if I get it right) uses hashing on actual function signature. For most cases. My head is still spinning.

Strickly speaking, you are correct. However, most modern Smalltalks will optimize that lookup quite heavily. A Cincom developer once described to me the mechanism they use to make method lookups (on the average) faster than the typical implementation of C++ virtual method dispatch. Fascinating stuff!
--
-- Jim Weirich jweirich@one.net [link|http://w3.one.net/~jweirich|http://w3.one.net/~jweirich]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New Yep - pretty amazing stuff
It totally depends on the implementation and there are a lot of really clever tricks. It might not seem like it - but ST actually (sometimes) gets you closer to the metal in a lot of nifty ways than something like C. Its very freaky.

For instance, the opencroquet guys are moving a huge amount of rendering into graphics hardware despite the fact that you're calling it from Smalltalk with ST data structures.




"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
New Free Book
No one seem to have mentioned this book
[link|http://www.cs.rit.edu/~ats/books/ooc.pdf|http://www.cs.rit.edu/~ats/books/ooc.pdf]
( + code examples: [link|http://www.cs.rit.edu/~ats/books/ooc-02.01.04.tar.gz|http://www.cs.rit.ed...c-02.01.04.tar.gz] and [link|ftp://ftp.informatik.uni-osnabrueck.de/pub/hanser/books/ooc-94.2.11.tar.gz|ftp://ftp.informatik...oc-94.2.11.tar.gz] )

Found at
[link|http://www.cs.rit.edu/~ats/books/index.html|http://www.cs.rit.ed.../books/index.html]

The author is called: Axel-Tobias Schreiner and his homepage is [link|http://www.cs.rit.edu/~ats/|http://www.cs.rit.edu/~ats/]

I am a complete programming beginner, well, am not just how to classify my skills but I never wrote any programs at all, but I am learning several programming languages.

That book, is far from being easy to read or follow, it seems to be a good book, just hard.
And after skimming throught it, I think that by the and, the guy create and Object-oreinted layer on top of C that looks a lot like Objective-c

From my humble experience if I have any :P , I think if you find that your application will benefit from OOP, you should use a language that helps you in that, like Objective-C, Java, Python, OCaml...

And a good programmer need to have good knowledge of several progamming languages anyway, I think programming in C should and I am sure certainly do, have it's own set of best practice, emulating OOP in C doesn't seem to be one.
     OO Design in C - (Ovid) - (12)
         Hmm...If I were doing ObjectOrienteed in C - (Simon_Jester)
         Take a look at the source-code to Ruby - (ben_tilly)
         Lots of ways - (tuberculosis) - (8)
             That reminds me of another thread here. - (Another Scott) - (1)
                 Re: That reminds me of another thread here. - (neelk)
             Hold on here - (Arkadiy) - (5)
                 Not too slow - (tuberculosis) - (4)
                     C++ also uses lookups... - (ChrisR) - (3)
                         No - (Arkadiy) - (2)
                             Method Dispatch in Smalltalk - (JimWeirich) - (1)
                                 Yep - pretty amazing stuff - (tuberculosis)
         Free Book - (systems)

...introduce an "if", and you're down the slippery slope. You add "for", and it's an avalanche. Then the "while" falls on you, and you're buried.
60 ms