Simplifying Concepts.
Accelerating Innovation.

Jacob's Blog

Jacob Beningo
| |

Embedded Basics – Peculiarities of the keyword const  

201508A1F1

The keyword const in C can at best be a misleading type qualifier.  One would think that const would specify that a particular symbol is a constant but unfortunately that is not the case.  The best definition a C developer can use for const is that it provides “read-only” access to the memory location that the symbol represents.  Take for example the following declaration:

const int x = 14;

The declaration is stating that x is a read-only symbol with the value of 14.  Attempts to modify x using the symbol x will all result in compiler warnings as one might expect of a constant.  The keyword const does not protect the memory location that x occupies from being manipulated in an indirect manner.  Take for example the following code:

int * y = &x;

The statement above is creating a pointer to the memory location that the read-only x symbol occupies.  One would think that if the pointer attempted to change the value that it would not be allowed, unfortunately this is not the case.  By dereferencing y and providing a new value such as 16, the memory location can be indirectly modified and the value of x is now 16.

*y = 16;  /* Completely valid! */

A developer that is paying close attention would realize that y is a pointer to a const, and the declaration could be changed to

const int * y = &x;

Attempts to dereference the pointer and change the value would now be caught by the compiler.  All now seems right with the world, except for when another developer comes along, decides they want to change the value through the pointer and now cast away the const, forcing the value to be changed as follows:

*(int *)y = 16;

Obviously this type of behavior would be highly frowned upon but it is important to realize that const behaves in this way.  Another interesting point related to declaring the pointer y is that while the pointer is pointing to a read-only integer, the pointer itself is not read-only!  Code such as the following would be perfectly legitimate:

y++;

y–;

y = y+0x1000;

What is a developer to do to make sure that the pointer can’t be changed to point to anything but x?  A confusing declaration such as the following can be used:

const int * const y = &x;

The above declaration is defining the symbol y to be a read-only pointer to an integer that is read-only.  Confused yet?

One of the advantages of using const in an embedded system is that in many cases declaring a symbol as const will force that symbol to be stored in ROM.  When the symbol is stored in ROM, the indirect methods of trying to change the value of the symbol will not work. The danger is that the behavior is not specified in the C standard!  A developer has to take care to understand what the compiler is doing and where it is storing the read-only values.

Despite the peculiarities of const, it is still important to use it where appropriate and to protect symbols that should be read-only.  One area of recommended heavy use is to protect parameters being passed to a function from being written to.

* * *

Struggling to keep your development skills up to date or facing outdated processes that slow down your team, raise costs, and impact product quality?

Here are 4 ways I can help you:

  • Embedded Software Academy: Enhance your skills, streamline your processes, and elevate your architecture. Join my academy for on-demand, hands-on workshops and cutting-edge development resources designed to transform your career and keep you ahead of the curve.
  • Consulting Services: Get personalized, expert guidance to streamline your development processes, boost efficiency, and achieve your project goals faster. Partner with us to unlock your team's full potential and drive innovation, ensuring your projects success.
  • Team Training and Development: Empower your team with the latest best practices in embedded software. Our expert-led training sessions will equip your team with the skills and knowledge to excel, innovate, and drive your projects to success.
  • Customized Design Solutions: Get design and development assistance to enhance efficiency, ensure robust testing, and streamline your development pipeline, driving your projects success.

Take action today to upgrade your skills, optimize your team, and achieve success.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.