Microcontroller Embedded C Programming Lecture 130| Placements of ‘const’ variables in memory

  • Post author:
  • Post category:Blog

 

Placements of ‘const‘ variables in memory

 

 

 

Placement of ‘const‘ variables in memory

  • All local const variables are just like non-const variables as far as memory placement is concerned. They are placed in RAM. The only speciality of a constant variable is, it is read-only.
  • All global const variables are stored in ROM or FLASH. This also further depends on linker script rules and the hardware on which the code runs. 
  • In STM32 target hardware(that is in our embedded hardware), all global const variables live in FLASH memory. So, when you try to modify the const variable using its address, the operation has no effect. Because flash memory of the microcontroller is write-protected.

By default, all global const variables will be stored in read-only memory or the FLASH.

FLASH is nothing but code space and code space is usually write-protected. You cannot just write into different flash addresses using pointers in your code. Because that has no effect. Because the code space or the flash memory is write-protected by default.

 

In the below program, let’s put the uint8_t const data =10; statement in the global section. Here ‘data’ is a read-only variable in the global scope. 

Placements of 'const' variables in memory
Figure 1. code

 

Execute that program and let’s see what happens. Here the program crashes, as shown in Figure 2.

Placements of const variables in memory
Figure 2. Program crashes

 

Why is that? 

Because the compiler has placed that read-only variable in the read-only section or in the read-only memory. So, the operating system expects that so no one should change the address where that variable value is placed. That is protected.

But what you did was, you used a pointer way and you try to modify that particular address which is write-protected. That’s why the operating system detected that activity and the application crashed. 

If you attempt this on your target hardware that is on your embedded target like an STM32 microcontroller, then your application will not crash but the operation that you just did has no effect.

Always remember that modifying the value of the global const variable using its address has undefined behavior. You should never try to do that. That’s why to use a ‘const‘ as a safety feature to guard your variable.

When someone tries to change the value of the variable deep inside the program, then the compiler alerts him by throwing an error saying ‘you are trying to change the value of the read-only variable’. So, that’s why you should use the const qualifier as a guard to your data.

Use the ‘const’ keyword to guard your data from modification in the project. If you unknowingly try to change the read-only variable compiler will alert you.

In the following article, we’ll understand const pointers and different case studies. 

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.