Author Topic: Effect of variable size in microcontrollers.  (Read 1368 times)

0 Members and 1 Guest are viewing this topic.

Offline DG41WVTopic starter

  • Regular Contributor
  • *
  • Posts: 51
  • Country: lk
Effect of variable size in microcontrollers.
« on: July 09, 2018, 02:24:02 pm »
Hi, I would like to know if there is any advantage in using variables that are smaller than the ram size. for example the STM32F103C8 seems to have a 32bit wide ram. would a 8bit variable take up the whole address or can the compiler put more variables in that memory. I'm using the GCC compiler btw.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22436
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Effect of variable size in microcontrollers.
« Reply #1 on: July 09, 2018, 02:35:02 pm »
The compiler will do whatever you tell it to.

What do you really want to do?  Optimize for code size?  RAM use?  Speed?

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: DG41WV

Offline exit_failure

  • Regular Contributor
  • *
  • Posts: 111
  • Country: de
Re: Effect of variable size in microcontrollers.
« Reply #2 on: July 09, 2018, 02:43:08 pm »
This is not something that can be answered universally as it depends on the interplay of the processors architecture and the exact compilation options you choose.

For example there is alignment and padding. This becomes especially important if you use data types of different lengths. These describe how your data is stored in RAM. There always will be a trade-off between memory usage and access time. Usually this it not something that is determined on a hardware level but by compiler options

The Wikipedia article explains it quite well:
https://en.wikipedia.org/wiki/Data_structure_alignment


Please feel free to ask, if you don't understand something or have any other questions left.
 
The following users thanked this post: DG41WV

Offline stmdude

  • Frequent Contributor
  • **
  • Posts: 479
  • Country: se
Re: Effect of variable size in microcontrollers.
« Reply #3 on: July 09, 2018, 02:46:09 pm »
In _this particular case_, GCC would behave like this:

Code: [Select]
uint8_t a,b;
printf(%p,%p\n",&a,&b);
0x10000000,0x10000004

Because it's more efficient (faster) for it to fetch on a 4-byte boundary.

However, you can tell GCC that you care more about size than speed like this:
Code: [Select]
#pragma pack(1) // Set packing/alignment to 1 byte
uint8_t a,b;
#pragma pack() // Restore default packing
printf(%p,%p\n",&a,&b);
0x10000000,0x10000001

(All this is typed from memory, so.. No guarantees, but this is basically how it works)
 
The following users thanked this post: DG41WV

Offline DG41WVTopic starter

  • Regular Contributor
  • *
  • Posts: 51
  • Country: lk
Re: Effect of variable size in microcontrollers.
« Reply #4 on: July 09, 2018, 03:17:09 pm »
Thanks for the answers.     
I'm not particularly trying to do something specific, but was just curious of what would happen if I used a uint16_t instead of a uint8_t. So, from the answers on this thread it would be safe to assume unless you ask the compiler to optimize for space, it would use a whole address space on the ram, right?
 

Offline exit_failure

  • Regular Contributor
  • *
  • Posts: 111
  • Country: de
Re: Effect of variable size in microcontrollers.
« Reply #5 on: July 09, 2018, 04:20:23 pm »
Quote from: DG41WV on Today at 01:17:09 am
Thanks for the answers.     
I'm not particularly trying to do something specific, but was just curious of what would happen if I used a uint16_t instead of a uint8_t. So, from the answers on this thread it would be safe to assume unless you ask the compiler to optimize for space, it would use a whole address space on the ram, right?

With 32bit word length you can fit a two 16 bit types into one word as long as they are aligned with that word. And as far as I know, this is also what usually happens. Doing so is in most aspects advantageous. You can load two words with one memory access and you save space.

Consider the following two examples:

You have two structs:
Code: [Select]
struct {
    uint16_t a,
    uint16_t b,
} sixteen_sixteen;

This struct would be placed in the memory like this:
|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|b|b|b|b|b|b|b|b|b|b|b|b|b|b|b|b|


Code: [Select]
struct {
    char c,
    uint16_t a,
    uint16_t b,
} one_sixteen_sixteen;

This struct would be placed in the memory like this (p=padding):
|c|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|
|b|b|b|b|b|b|b|b|b|b|b|b|b|b|b|b|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|

Do you understand why?
« Last Edit: July 09, 2018, 04:23:45 pm by exit_failure »
 
The following users thanked this post: DG41WV

Offline DG41WVTopic starter

  • Regular Contributor
  • *
  • Posts: 51
  • Country: lk
Re: Effect of variable size in microcontrollers.
« Reply #6 on: July 10, 2018, 12:45:32 pm »
Quote

Code: [Select]
struct {
    char c,
    uint16_t a,
    uint16_t b,
} one_sixteen_sixteen;

This struct would be placed in the memory like this (p=padding):
|c|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|
|b|b|b|b|b|b|b|b|b|b|b|b|b|b|b|b|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|

Do you understand why?

because the variables will be placed at 16bit intervals. like base address +16 bits*n ?
 

Offline exit_failure

  • Regular Contributor
  • *
  • Posts: 111
  • Country: de
Re: Effect of variable size in microcontrollers.
« Reply #7 on: July 10, 2018, 02:03:39 pm »
because the variables will be placed at 16bit intervals. like base address +16 bits*n ?

Exactly. I just wanted to make sure there was no misunderstanding because people in university seemed to have a lot of difficulties to get their heads around that for some reason...
 
The following users thanked this post: DG41WV


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf