October 4, 2023


Develop Technology For The Connected World

Programming embedded devices: polymorphism in C

5 min read

From our study of C++, it should really be very clear that, in contrast to course encapsulation and one inheritance, which were essentially free of charge in C, polymorphism in C includes some complexity and overhead.

In the very last lesson, you learned about polymorphism and observed how it performs in C++. In today’s video lesson #32, you will emulate the C++ VPTR-VTABLE design and style in C. The primary intention is comprehending how points get the job done at a lower amount, which need to enable you use polymorphism extra effectively and confidently in any language. But from the study of C++, it should be distinct that, unlike class encapsulation and solitary inheritance, which have been primarily free of charge in C, polymorphism includes some complexity and overhead. For that reason, if you intend to thoroughly use polymorphism, you will be superior off switching to C++. Having said that, suppose you build or use software libraries (these kinds of as an RTOS, interaction stack, or a state device framework). In that scenario, the complexities of polymorphism in C can be confined to the library and successfully hidden from the application builders.

Lesson #32 OOP Section-4: Polymorphism in C


The VPTR in the Base Course

In the initial move, you explicitly incorporate the VPTR as the to start with member of the attribute structure in the foundation course (like Form in the video clip). The VPTR is a pointer to the const ShapeVtable struct. This const qualifier enables the VTABLE to reside in ROM.

VTABLE and Pointers to Features in C

Even although it is termed a “table,” the VTABLE isn’t ordinarily applied in C as an array but instead as a struct of ideas to the virtual capabilities, these as attract() and space(), in the situation of the Shape course.

The C language allows you to present a pointer to a operate, just like a pointer to a variable. In both of those scenarios, a C pointer is made up of the tackle and the sort information about the entity staying pointed to. In the scenario of a pointer to a perform, the style information and facts consists of the full signature of the operate.

Virtual Contact (Late Binding)

You can carry out late binding in a handful of distinctive methods. First, you can give member capabilities for that, just like all the other functions of the foundation class. For example, Form_draw_vcall() demonstrated in the video, dereferences the “me” pointer to get the VPTR. Upcoming, it will get from the VPTR to the unique perform pointer within the VTABLE, such as (*draw). And at last, it supplies the call parameters, including the “me” pointer and probably other parameters in the function’s signature. To reduce the purpose phone overhead, the C99 conventional allows declaring the “vcall” features as inline. The more mature C89 does not provide this option, so you may will need to implement the late binding mechanism as preprocessor macros. Equally possibilities are illustrated in the video.

Placing Up VTABLE and VPTR in the Constructor

The VPTR and VTABLE setup occurs in just about every constructor (for the foundation class and each and every subclass.) Below, you outline the VTABLE as both equally static and const in ROM, and you will need to quickly initialize it with the pointers to member features. This performs nicely in the base class (e.g., Form) but needs a nasty casting in the subclasses (e.g., Rectangle). This is for the reason that the “me” pointer in the subclasses has the variety of the subclass (e.g., Rectangle*), while the VTABLE inherited from the base class expects “me” ideas of the base class sort (e.g., Form*). Pertaining to the VPTR, you have to have to assign it only soon after calling the superclass’s constructor.

Skipping the VPTR Indirection

The most routinely utilized alternate to the VPTR-VTABLE implementation is to take away the VPTR amount of indirection and embed the full VTABLE specifically inside of each and every item. The benefit in this article is a additional simple digital connect with and a nicer syntax resembling C++ for the reason that the digital attract() function, for instance, can be invoked employing the dot operator, like rect.attract(&rect,…). But the drawback is the greater RAM utilization because VTABLE is now in RAM alternatively than ROM. In addition, the VTABLE is repeated in each and every object, so you can effortlessly double or triple your RAM utilization if you have a sizable VTABLE and many objects.

Conclusion Notes

Polymorphism can be an tasteful, effective, and extensible alternative, but only when the sort facts desires to be resolved at runtime. You do not need polymorphism when the item types are recognised at compile or connection time. For instance, polymorphism is not the good system to handle computer software versions or product strains mainly because these are identified right before the application is set up. A better way is to style a clean up Board Support Package deal (BSP) interface and then supply distinctive implementations (diverse BSPs) for different merchandise. Of training course, there is significantly additional to it than just a very simple BSP abstraction. The successful management of merchandise traces involves watchful physical design, which is the artwork of partitioning your code into directories and documents, this sort of as header files and implementation files. That way, you can make the ultimate application for any products by combining several modules at website link time alternatively than utilizing methods like polymorphism at runtime. Regretably, the subject matter is not greatly recognised or appreciated. While tons of textbooks speak about rational design approaches, these types of as OOP, very couple of sources exist for actual physical style. Just one noteworthy exception is the e book “Large Scale C++ Software Design” by John Lakos [2].

[1] Miro Samek, “Object-Oriented Programming in C, code examples,” GitHub

[2] John Lakos, “Significant-Scale C++ Computer software Layout,” Addison-Wesley 1996, ISBN 978-0201633627

Dr. Miro M. Samek is the creator of the open up resource QP real-time embedded frameworks and the freeware QM graphical model-primarily based design and style instrument. He is also the founder and CEO of Quantum Leaps — the service provider of contemporary embedded application centered on lively objects and hierarchical condition machines as effectively as equipment for visible modeling, computerized code technology, and device screening of deeply embedded software package. Miro teaches the well-liked YouTube “Modern Embedded Programs Programming” online video class on which this post sequence is primarily based.

Relevant Contents:

Copyright © All rights reserved. | Newsphere by AF themes.