TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

233 点作者 dreampeppers993 个月前

13 条评论

inopinatus3 个月前
This is an excellent pattern in C. The Dovecot mail server has many fine examples of the style as well e.g.<p><pre><code> struct dict dict_driver_ldap = { .name = &quot;ldap&quot;, .v = { .init = ldap_dict_init, .deinit = ldap_dict_deinit, .wait = ldap_dict_wait, .lookup = ldap_dict_lookup, .lookup_async = ldap_dict_lookup_async, .switch_ioloop = ldap_dict_switch_ioloop, } }; </code></pre> defines the virtual function table for the LDAP module, and any other subsystem that looks things up via the abstract dict interface can consequently be configured to use the ldap service without concrete knowledge of it.<p>(those interested in a deeper dive might start at <a href="https:&#x2F;&#x2F;github.com&#x2F;dovecot&#x2F;core&#x2F;blob&#x2F;main&#x2F;src&#x2F;lib-dict&#x2F;dict-private.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dovecot&#x2F;core&#x2F;blob&#x2F;main&#x2F;src&#x2F;lib-dict&#x2F;dict-...</a>)
评论 #43319338 未加载
评论 #43319996 未加载
social_quotient3 个月前
I spend a ton of time in FFmpeg, and I’m still blown away by how it uses abstractions to stay modular—especially for a project that’s been around forever and still feels so relevant. Those filtergraphs pulling off polymorphism-like tricks in C? It’s such an elegant way to manage complex pipelines. e.g.<p>ffmpeg -i input.wav -filter_complex &quot; [0:a]asplit=2[a1][a2]; [a1]lowpass=f=500[a1_low]; [a2]highpass=f=500[a2_high]; [a1_low]volume=0.5[a1_low_vol]; [a2_high]volume=1.5[a2_high_vol]; [a1_low_vol][a2_high_vol]amix=inputs=2[a_mixed]; [a_mixed]aecho=0.8:0.9:1000:0.3[a_reverb] &quot; -map &quot;[a_reverb]&quot; output.wav<p>That said, keeping those interfaces clean and consistent as the codebase grows (and ages) takes some real dedication.<p>Also recently joined the mailing lists and it’s been awesome to get a step closer to the pulse of the project. I recommend if you want to casually get more exposure to the breadth of the project.<p><a href="https:&#x2F;&#x2F;ffmpeg.org&#x2F;mailman&#x2F;listinfo" rel="nofollow">https:&#x2F;&#x2F;ffmpeg.org&#x2F;mailman&#x2F;listinfo</a>
评论 #43316229 未加载
cbarrick3 个月前
For the record, this design pattern is called a virtual method table, or vtable.<p>I&#x27;m surprised that this article never mentioned the term.<p>C++ programmers will know this pattern from the `virtual` keyword.
评论 #43317728 未加载
评论 #43319762 未加载
评论 #43325022 未加载
KerrAvon3 个月前
&gt; The interface type in golang is much more powerful than Java’s similar construct because its definition is totally disconnected from the implementation and vice versa. We could even make each codec a ReadWriter and use it all around.<p>This paragraph completely derailed me — I’m not familiar with golang, but `interface` in Java is like `@protocol` in Objective-C — it defines an interface without an implementation for the class to implement, decoupling it entirely from the implementation. Seems to be exactly the same thing?
评论 #43315028 未加载
评论 #43315892 未加载
sitkack3 个月前
I&#x27;d say at 20kloc of C, <a href="https:&#x2F;&#x2F;www.lua.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.lua.org&#x2F;</a> gets you as far up the Object Oriented tower as you want.
评论 #43319494 未加载
codr73 个月前
No discussion about polymorphism in C is complete without mentioning this macro:<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;15832301&#x2F;understanding-container-of-macro-in-the-linux-kernel" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;15832301&#x2F;understanding-c...</a>
glouwbug3 个月前
<p><pre><code> *int (*encode)(*int); </code></pre> Why not compile your snippets? Heads up to the author.
评论 #43316941 未加载
favorited3 个月前
&gt; for instance, Linux handles network socket, special files (like &#x2F;proc&#x2F;cpuinfo) or even USB devices as files. This is a powerful idea that can make easy to write or use programs for linux since we can rely in a set of well known operations from this abstraction called file.<p>Benno Rice gave a fantastic talk a few years ago called &quot;What UNIX Cost Us,&quot; which he starts off by showing how to write some USB device code in macOS, Windows, and Linux. It only takes a few minutes to demonstrate how pretending that everything is a file can be a pretty poor abstraction, and result in far more confusing code, which is why everyone ends up using libusb instead of sysfs.<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=9-IWMbJXoLM#t=134s" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=9-IWMbJXoLM#t=134s</a>
评论 #43332148 未加载
loph3 个月前
My recollection (which could be rusty, it has been &gt;30 years) is that the Motif API, coded in C, implemented a kind of polymorphism.
jdefr893 个月前
What is with the incorrect function declarations? I see:<p><i>int (</i>func)().<p>Maybe you meant: int * (*func)(void)?<p>Don&#x27;t mean to be pedantic. Just wanted to point it out so you can fix it.
brcmthrowaway3 个月前
Does ffmpeg support SVE?
pjmlp3 个月前
The language is called Go.<p>Other than that, yeah doing by hand what C++ and Objective-C do automatically.
评论 #43326070 未加载
high_na_euv3 个月前
Who would have thought that OOP could be useful!
评论 #43319697 未加载