SoFunction
Updated on 2025-04-13

Example of automatic address allocation in C language

1. Background introduction

In our embedded software development work, address allocation is a very important part of the content, and there are two advantages of the automatic address allocation method to be introduced in this article.

(1) There are fewer modification points when assigning addresses to new data in the data of two assigned addresses.

(2) The assigned addresses are not prone to conflicts and will not cause any errors that are out of bounds.

2. Application examples

For example, the current function we want to implement is to store several different data into the EEPROM, and the stored data are called data 1, data 2, and data 3 respectively. Their three data occupy 100 bytes of size respectively. The base address of the storage is 0, so the general idea is as follows:

//Data length#define LEN_FIRST_DATA   100
#define LEN_SECOND_DATA  100
#define LEN_THIRD_DATA   100

//Data address#define ADR_FIRST_DATA    0
#define ADR_SECOND_DATA   100
#define ADR_THIRD_DATA    200

If we want to insert new data into two data, we need to modify all macros after inserting new data. The problem caused by this processing is that too many macros will waste a lot of time. Therefore, in our actual work, we use the automatic allocation of addresses to avoid this problem.

1. Automatic address assignment method 1

When defining the macro, we can make the address of the first data = base address, the address of the second data = address of the first data + length of the first data, the address of the third data = address of the second data + length of the second data, and so on. In this way, you only need to modify two places when inserting new elements.

//Data length#define LEN_FIRST_DATA   100
#define LEN_SECOND_DATA  100
#define LEN_THIRD_DATA   100

//Data address#define ADR_BASE_DATA    0
#define ADR_FIRST_DATA   ADR_BASE_DATA 
#define ADR_SECOND_DATA  ADR_FIRST_DATA   + LEN_FIRST_DATA
#define ADR_THIRD_DATA   ADR_SECOND_DATAZ + LEN_SECOND_DATA

2. Automatic address assignment method 2

There is another wiser way to write it, which is to use the entire EE address as a structure, and then use the cast conversion technique to directly obtain the allocated data address data. The specific way is as follows.

typedef struct
{
  unsigned char  _FirstData[100];
  unsigned char  _SecondData[100];
  unsigned char  _ThirdData[100];
}St_Data

#define GET_ADDR(data)   ((unsigned int)&((St_Data*)ADR_BASE_DATA)->data))

If you write this way, you only need to call GET_ADDR(data) to read the EE address. The function of this macro is to directly return a 4-byte unsigned int type address through the data. For example, if we want to obtain the EE address of the first data, we can directly call GET_ADDR(_FirstData).

Personally, the second method is better. It not only saves a lot of macro definitions, but also has only one modification point when inserting new elements, and the code is readable better.

This is the end of this article about the example of automatic address allocation in C language. For more related C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!