Solutions for Industrial Computing Blog

Solutions for Industrial Computing

The Solutions for Industrial Computing Blog is the place for conversation and discussion about industrial computers, systems and controllers; communications and connectivity; software and control; and power strategies. Here, you'll find everything from application ideas, to news and industry trends, to hot topics and cutting edge innovations.

Previous in Blog: C++ for Embedded Systems (1 of 2)   Next in Blog: When Should You Upgrade Operating Systems and Other Software?
Close
Close
Close
2 comments
Rate Comments: Nested

C++ for Embedded Systems (2 of 2)

Posted March 14, 2008 1:45 AM by ITPro

C++ has proven to be a robust language for object-oriented programming, much of its longevity owed to its stability on Win32 platforms and its maturity in producing comprehensive operating systems and end-user applications rich with features. However, developers have come to rely on drafting techniques to make memory use more efficient. While some programmable logic devices are NVRAM (e.g., they retain their memory after power less) memory allocation is still at a premium. This led to the necessity of EC++ being a true but condensed subset of the full C++ library (see part 1 of 2 in this series for details on what didn't make the cut). Let's look at one very simple example. Here's a simple output statement in a standard C++ program:

#include <iostream>
using std::cout;
using std::endl;
int main()
{
int num1 = 6;
cout << num1 << endl;

return 0;
}

The first line is a preprocessor directive ordering inclusion of the input/output stream header. The second and third lines declare using the standard stream output (cout) and end line (endl) operators of the std namespace. The int main() is simply a call to the main function, indicative of beginning program execution, and the assignment of 6 to an integer variable. The cout is followed by the stream insertion operator "<<", a string statement in quotes, another stream insertion operator, and finally the end of the line. The return statement is simply to mark the end of program execution and indicate the program returns no values.

Now, let's look at an identical program as would be seen in EC++:

#include <stream.h>
int main()
{
int num1=6;
cout << num1 <<"\n";
}

Notice several things. No "using" statements are necessary, neither are we using the iostream header, instead utilizing the stream header as a matter of expediency. Also note the removal of the endl; in favor of the "\n", the newline string with an escape character (the backslash). The newline character is also available in standard C++ but should not necessarily be viewed as serving the same purpose in the case of EC++.

Another common technique involves operator overloading, which simply put is a way of declaring a same function name to execute the same ops on different data types multiple times. This allows developers to "destroy" temporary objects once they are processed and utilized rather than keeping them reserved in memory until the program fully executes. Here is a simple example:

#include <defs.h>
inline float max(float val1, float val2)
{
return val1 > val2 ? val1:val2;
}
inline double max(double val3, doubleval4)
{
return val3 < val4 ? val3:val4;
}

In this case, we have quickly utilized the inline max() functions of the definitions header (really just a "dummy" include file) to compare and return whether it is true that value one is greater than value two. In the first function the values are floats, but in the second notice they are now doubles. Same function, different data types. Neat! You have now seen just a couple of small ways EC++ is used in implementation and the differences to standard C++. For further reading, two texts are recommended: "Embedded Software: The Works" by Colin Walls, and an O'Reilly text that's considered a classic on the subject, "Programming Embedded Systems in C and C++" by Michael Barr. Stay tuned for Saturday's topic of interest!

Editor's Note: Click here for Part 1 of this series.

Reply

Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Participant

Join Date: Mar 2008
Posts: 1
#1

Re: C++ for Embedded Systems (2 of 2)

03/21/2008 10:20 AM

Hi ITPro, Thank you for your work.

Nowadays, in our company, we are considering to convert our project written in C to C++. At the end of your post, you recommended two books:

"For further reading, two texts are recommended: "Embedded Software: The Works" by Colin Walls, and an O'Reilly text that's considered a classic on the subject, "Programming Embedded Systems in C and C++" by Michael Barr."

I checked these books from internet and I come a cross to a bad review for the first book (Embedded Software: The Works). Here is the link for the review:

http://www.amazon.com/review/R3CFB8F82FWHA7/ref=cm_cr_pr_viewpnt#R3CFB8F82FWHA7

What do you think about it? Can we relay the book?

I also come across to a newer edition to the second book(Programming Embedded Systems in C and C++). It is "Programming Embedded Systems: With C and GNU Development second edition"

As a conclusion which book(s) / links do you recommend for UML and C++ for a realtime embedded environment?

Best regards,

Oguz.

Reply
Active Contributor

Join Date: Mar 2008
Posts: 15
Good Answers: 1
#2
In reply to #1

Re: C++ for Embedded Systems (2 of 2)

03/21/2008 2:52 PM

odilmac, thank you for your comments. I have looked at the review you cited; I note that it is only one of nine others that all render high marks for the text, so I'm confused as to why you're weighing one negative review over a number of positive ones.

Your other question really has two parts: books for UML (e.g., Embedded Systems Design) I might recommend could include Real Time UML by Bruce Powel Douglass (along with the Workshop companion book) and Agile Software Development: Principles, Patterns, and Practices by Robert C. Martin.

As to development and transform from C to EC++, Embedded.com would be my very first stop for no other reason than the availability of free content found there. I would start with reading C++ In Embedded Systems: Myth and Reality on the site. As to texts, the difficulty in making a good recommendation always falls back on the type of embedded systems you are programming, coupled with the expertise of your developers; hence, it would be difficult to provide you with more information without specifics.

The reason I recommended Barr's book was due to his contrasting between C (the iconic favorite of ES programming) and C++ along with his practical credentials. In some circles he is considered a foremost authority on the subject. The second edition to which you referred would also be useful, but I wouldn't let that dissuade you from purchasing the first. Best wishes with your project.

Reply
Reply to Blog Entry 2 comments

Previous in Blog: C++ for Embedded Systems (1 of 2)   Next in Blog: When Should You Upgrade Operating Systems and Other Software?
You might be interested in: Video Cables, Cable Assemblies, Serial Cables

Advertisement