frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Object-oriented design patterns in C and kernel development

https://oshub.org/projects/retros-32/posts/object-oriented-design-patterns-in-osdev
68•joexbayer•1d ago

Comments

ryao•22h ago
> The article describes how the Linux kernel, despite being written in C, embraces object-oriented principles by using function pointers in structures to achieve polymorphism.

This technique predates object oriented programming. It is called an abstract data type or data abstraction. A key difference between data abstraction and object oriented programming is that you can leave functions unimplemented in your abstract data type while OOP requires that the functions always be implemented.

The sanest way to have optional functions in object oriented programming that occurs to me would be to have an additional class for each optional function and inherit each one you implement alongside your base class via multiple inheritance. Then you would need to check at runtime whether the object is an instance of the additional class before using an optional function. With an abstract data type, you would just be do a simple NULL check to see if the function pointer is present before using it.

trws•17h ago
I largely agree, and use these patterns in C, but you’re neglecting the usual approach of having a default or stub implementation in the base for classic OOP. There’s also the option of using interfaces in more modern OOP or concept-style languages where you can cast to an interface type to only require the subset of the API you actually need to call. Go is a good example of this, in fact doing the lookup at runtime from effectively a table of function pointers like this.
ryao•1h ago
My point is that this pattern is not object oriented programming. As for a default behavior with it, you usually would do that by either always adding the default pointer when creating the structure or calling the default whenever the pointer is NULL.

In the Linux VFS for example, there are optimized functions for reading and writing, but if those are not implemented, a fallback to unoptimized functions is done at the call sites. Both sets are function pointers and you only need to implement one if I recall correctly.

f1shy•39m ago
To be fair, OOP is not 100% absolutely perfectly defined. Strustrup swears C++ is OOP, Alan Key, at least at some point laughed at C++, and people using CLOS have yet another definition
naasking•25m ago
> My point is that this pattern is not object oriented programming.

I think the "is/is not" question is not so clear. If you think of "is" as a whether there's a homomorphism, then it makes sense to say that it is OOP, but it can qualify as being something else too, ie. it's not an exclusionary relationaship.

mistrial9•1h ago
The concept of abstract data type is a real idea in the days of compiler design. You might as well say "compiler design predates object oriented programming". The technique described in the lead is used to implement object-oriented programming structures, just as it says. So are lots of compiler design features under the hood.

source- I wrote a windowing framework for MacOS using this pattern and others, in C with MetroWerks at the time.

ryao•1h ago
Compiler design does predate object oriented programming. The first compiler was made by John Backus et al at IBM in April 1957.

As for abstract data types, they originated in Lisp, which also predates object oriented programming.

pjmlp•1h ago
Actually, no.

"AN ALGORITHMIC THEORY OF LANGUAGE", 1962

https://apps.dtic.mil/sti/tr/pdf/AD0296998.pdf

In this paper they are known as plexes, eventually ML and CLU will show similar approaches as well.

Only much latter would Lisps evolve from plain lists and cons cells.

pavlov•1h ago
In Smalltalk and Objective-C, you just check at runtime whether an object instance responds to a message. This is the original OOP way.

It's sad that OOP was corrupted by the excessively class-centric C++ and Java design patterns.

mettamage•1h ago
Wait, so in obj-c, could you also write some kijdnof doesnotunderstand method to achieve some dynamic method dispatch?
pjmlp•1h ago
Yes, that is how microservices were implemented in the days of NeXTSTEP. with PDO.

https://en.wikipedia.org/wiki/Portable_Distributed_Objects

pjmlp•1h ago
Actually I would say that it is sad that developers learn a specific way how a technology is done in language XYZ and then use it as template everywhere else, what happened to the curiosity of learning?
1718627440•1h ago
> This technique predates object oriented programming.

I would rather say that OOP is a formalization of predating patterns and paradigma.

1718627440•2h ago
> Having to pass the object explicitly every time feels clunky, especially compared to C++ where this is implicit.

I personally don't like implicit this. You are very much passing a this instance around, as opposed to a class method. Also explicit this eliminates the problem, that you don't know if the variable is an instance variable or a global/from somewhere else.

spacechild1•58m ago
> Also explicit this eliminates the problem, that you don't know if the variable is an instance variable or a global/from somewhere else.

People typically use some kind of naming convention for their member variables, e.g. mFoo, m_Foo, m_foo, foo_, etc., so that's not an issue. I find `foo_` much more concise than `this->foo`. Also note that you can use explicity this in C++ if you really want to.

1718627440•21m ago
In code I write, I can know what variables mean. The feature loses its point, when it's not mandatory. Also being explicit allows you to be more expressive with variable name and ordering.
elteto•55m ago
...and C++ added explicit this parameters (deducing this) in C++23.
loeg•54m ago
I think the author is talking about this:

  object->ops->start(object)
Where not only is it explicit, but you need to specify the object twice (once to resolve the Vtable, and a second time to pass the object to the stateless C method implementation).
1718627440•24m ago
Yes I know. From the caller that might seem to be redundant, my argument was about the callee's side. Also it is not truely redundant, as you can write:

    object1->op->start(object2)
    superclass->op->start(object)
loeg•18m ago
I think both of these invocations are invalid. Using object1's vtable methods on object2, obviously, but in the latter case: the vtable method should just point at the superclass impl, if not overridden. And if overridden and the child impl needs to call the superclass, it can just do so without dispatching through some vtable.
tdrnl•48m ago
A talk[0] about Tmux is where I learned about this pattern in C.

I wrote about this concept[1] for my own understanding as well -- just tracing the an instance of the pattern through the tmux code.

[0] https://raw.githubusercontent.com/tmux/tmux/1536b7e206e51488... [1] https://blog.drnll.com/tmux-obj-oriented-commands

munchler•35m ago
Note that this is using interfaces (i.e. vtables, records of function pointers), not full object-orientation. Other OO features, like classes and inheritance, have much more baggage, and are often not worth the associated pain.
PhilipRoman•22m ago
Field inheritance is surprisingly natural in C, where a struct can be cast to it's first member.
1718627440•8m ago
What do you think inheritance is, if not composition of vtables? What do you think classes are, if not a composition of a vtable and scoped variables?
munchler•3m ago
Those "scoped variables" are the difference. Mutable state adds a great deal of complexity.

Nx compromised: malware uses Claude code CLI to explore the filesystem

https://semgrep.dev/blog/2025/security-alert-nx-compromised-to-steal-wallets-and-credentials/
288•neuroo•3h ago•170 comments

Monodraw

https://monodraw.helftone.com/
337•mafro•4h ago•114 comments

Object-oriented design patterns in C and kernel development

https://oshub.org/projects/retros-32/posts/object-oriented-design-patterns-in-osdev
68•joexbayer•1d ago•25 comments

Implementing Forth in Go and C

https://eli.thegreenplace.net/2025/implementing-forth-in-go-and-c/
36•Bogdanp•2h ago•4 comments

The Therac-25 Incident (2021)

https://thedailywtf.com/articles/the-therac-25-incident
283•lemper•8h ago•157 comments

SpaceX's giant Starship Mars rocket nails critical 10th test flight

https://www.space.com/space-exploration/private-spaceflight/spacex-launches-starship-flight-10-cr...
151•mpweiher•2h ago•109 comments

ASCIIFlow

https://asciiflow.com/
46•marcodiego•3h ago•9 comments

Slowing down programs is surprisingly useful

https://stefan-marr.de/2025/08/how-to-slow-down-a-program/
52•todsacerdoti•3h ago•22 comments

What We Find in the Sewers

https://www.asimov.press/p/sewers
12•surprisetalk•1h ago•5 comments

The GitHub website is slow on Safari

https://github.com/orgs/community/discussions/170758
155•talboren•5h ago•103 comments

WebLibre: The Privacy-Focused Browser

https://docs.weblibre.eu/
78•mnmalst•6h ago•51 comments

Claude for Chrome

https://www.anthropic.com/news/claude-for-chrome
730•davidbarker•20h ago•376 comments

Ember (YC F24) Is Hiring Full Stack Engineer

https://www.ycombinator.com/companies/ember/jobs/OTB0qby-full-stack-engineering-intern-summer-2026
1•charlene-wang•3h ago

F-35 pilot held 50-minute airborne conference call with engineers before crash

https://www.cnn.com/2025/08/27/us/alaska-f-35-crash-accident-report-hnk-ml
168•Michelangelo11•3h ago•235 comments

Gemini 2.5 Flash Image

https://developers.googleblog.com/en/introducing-gemini-2-5-flash-image/
1012•meetpateltech•1d ago•452 comments

QEMU 10.1.0

https://wiki.qemu.org/ChangeLog/10.1
154•dmitrijbelikov•4h ago•25 comments

Using information theory to solve Mastermind

https://www.goranssongaspar.com/mastermind
14•SchwKatze•3d ago•2 comments

Why Aren't People Going to Local and Regional In-Person Events Anymore?

https://www.brentozar.com/archive/2025/08/why-arent-people-going-to-local-and-regional-in-person-...
25•wintermute2dot0•1h ago•25 comments

Internet Access Providers Aren't Bound by DMCA Unmasking Subpoenas–In Re Cox

https://blog.ericgoldman.org/archives/2025/08/internet-access-providers-arent-bound-by-dmca-unmas...
41•hn_acker•2d ago•6 comments

Malleable Software

https://www.mdubakov.me/malleable-software-will-eat-the-saas-world/
61•tablet•7h ago•67 comments

Apple Revokes EU Distribution Rights for an App on the Alt Store

https://torrentfreak.com/apple-revokes-eu-distribution-rights-for-torrent-client-developer-left-i...
34•net01•1h ago•10 comments

Dissecting the Apple M1 GPU, the end

https://rosenzweig.io/blog/asahi-gpu-part-n.html
648•alsetmusic•13h ago•142 comments

Show HN: FilterQL – A tiny query language for filtering structured data

https://github.com/adamhl8/filterql
37•genshii•2d ago•14 comments

Word documents will be saved to the cloud automatically on Windows going forward

https://www.ghacks.net/2025/08/27/your-word-documents-will-be-saved-to-the-cloud-automatically-on...
169•speckx•5h ago•153 comments

First absolute superconducting switch developed in a magnetic device

https://phys.org/news/2025-08-absolute-superconducting-magnetic-device.html
8•warrenm•1d ago•0 comments

Light pollution prolongs avian activity

https://gizmodo.com/birds-across-the-world-are-singing-all-day-for-a-disturbing-reason-2000646257
102•gmays•4d ago•23 comments

The “Wow!” signal was likely from extraterrestrial source, and more powerful

https://www.iflscience.com/the-wow-signal-was-likely-from-an-extraterrestrial-source-and-more-pow...
179•toss1•17h ago•178 comments

GNU Artanis – A fast web application framework for Scheme

https://artanis.dev/index.html
244•smartmic•19h ago•65 comments

Delphi in the Age of AI

https://learndelphi.org/delphi-ai-ultimate-guide/
66•andsoitis•4d ago•44 comments

Rv, a new kind of Ruby management tool

https://andre.arko.net/2025/08/25/rv-a-new-kind-of-ruby-management-tool/
299•steveklabnik•1d ago•110 comments