4

Since C is commonly used in micro-controllers and it is possible to do object oriented programming in C, is it advisable to implement object oriented micro-controller programming using C? What are the pros and cons?

7
  • This belongs on programmers.se. It's not specific enough to be answered here. Commented Dec 28, 2010 at 14:57
  • It is possible to do object oriented programming in C?? Commented Dec 28, 2010 at 15:05
  • 1
    @taskinoor: Yes, just like it's possible to do functional programming in Java - possible, strictly speaking, but not pretty or easy or advisable. See e.g. Object-Oriented Programming With ANSI-C. Commented Dec 28, 2010 at 15:53
  • 2
    @taskinoor: For that matter, on my shelf is a copy of a book entitled "Object-Oriented Assembly Language".... Commented Dec 28, 2010 at 21:35
  • @delnan and Brooks Moses, thanks a lot. This is a completely new thing to me. Commented Dec 29, 2010 at 15:44

6 Answers 6

4

My short answer is no. Microcontrollers are highly restricted when it comes to program code memory, execution speed, and RAM. Keeping things as simple as possible with C is advisable. C is not meant to be an object-oriented language. The most you should consider doing is using pointers and structs, but don't try faking polymorphism or anything fancy like that.

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this, you CAN, at least on some bigger micros, for fun, but just not practical to do so.
3

As long as you do not need polymorpism it is ok to pass structs around. But as soon as you use polymorphism/virtual function and putting functions inside these structs it might become very cryptic.

It also depends what you need to do. For drivers you do not need OO, maybe for application. Keep in mind that microcontrolers are low whit RAM, any you will need to keep low RAM footprint all the time in any case.

If you do not plan to write more than 40k lines of application plain C is good enough and without fancy OO tricks.

9 Comments

Passing structs (or pointers to structs) around is not OO. It's plain old procedural programming.
There is so little difference between struct and class.
@delnan A struct which contains both data elements and pointers to functions which operate on that data is an object in the OOP meaning of the word.
@Chris: Yes, but structs alone aren't. The virtual methods (function pointers) make it objects. Your answer seems to imply that it would still be some kind of OO without virtual methods.
@delnan, I don't see how you can read an implication of "without" into a comment that explicitly contains the word "both"
|
3

Yes, it's not only possible but in my opinion sometimes advisable.

On a small system, you need to be very aware of the costs of how you choose to do things. At the same time, there can be some real advantages of "lightweight" object orientation for organizing your code, particularly if you need to make a flexible toolkit for quickly implementing customizations, or even to permit runtime hotplugging. A do-it-yourself lightweight object implementation (done for example with structs and function pointers) in C can be a good compromise.

Perhaps the best known example of this is the linux kernel.

Comments

3

Yes, but if your tool-chain supports C++, you'd be better off using that. If the micro-controller is particularly resource constrained, or the application has hard real-time requirements, you will want to be fairly conservative in your use of C++, in particular the standard library, but you should use it nonetheless over C if the design and implementation are OO.

Comments

2

It's true, you can use OOP with C. You can also use #define to change keywords to look more like Python. However, I would not suggest doing either.

When I've seen someone try to do more complex OOP with C, it always ends up in unreadable code. When I see C code, I expect it to look like C, not someone's idea of how OOP in C should work.

If you want OOP on a micro, use C++. Many/most new micros support it. Ignore those who say that micros don't have enough memory or speed because they have no idea how fast your micro is, how much memory it has, and what your performance constraints are. Well written C++ will beat poorly written C in size and speed any day.

Comments

-2

For my next embedded project, I will definitely be using parts of C++ and build an clean interface/class/static object based application with typedefs, defines and all the other nasty c left out. The things that I plan to utilize are:

  • Classes. This allows me to encapsulate code and unit test with stub objects by means of configuration.
  • Interfaces. Gives me the power of a clear contract on each class compared to c header files which people tend to use as garbage cans for all kinds of typedefs, defines and stuff. Also, interfaces gives me separation of definition and implementation and allows for unit testing with stub objects.
  • Static objects. I foresee no dynamic memory, so all objects will be static. Probably one application class will define and initialize everything and thus be the configuration of the application.

All in all, it will compile into something that is as efficient as c, but with a much better overview.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.