LEMUEL  ADANE
LEMUEL ADANE

Reputation: 8818

Programming a Microprocessor using C in Object Oriented Paradigm, is it Advisable?

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?

Upvotes: 4

Views: 2307

Answers (6)

Christian Madsen
Christian Madsen

Reputation: 1748

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.

Upvotes: -2

Clifford
Clifford

Reputation: 93446

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.

Upvotes: 3

Chris Stratton
Chris Stratton

Reputation: 40337

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.

Upvotes: 3

Kyle Heironimus
Kyle Heironimus

Reputation: 8041

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.

Upvotes: 2

Reinderien
Reinderien

Reputation: 15211

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.

Upvotes: 4

Luka Rahne
Luka Rahne

Reputation: 10447

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.

Upvotes: 3

Related Questions