Skip to main content
NCatt.1
Associate III
July 5, 2022
Solved

Expand struct in flash without change location of next data

Hello everyone,

I currently have devices with a specific FW released on market. In this FW there is a structure saved in internal flash memory (with "const" and defined areas on linker file).

In the new version I am working on, I need to expand an array placed in the middle of this structure. The problem is that data next to this array will change location by doing this, what I would like to avoid.

Is there a way to add the new data of the array at the end of the structure ? Or any other idea ?

Thanks a lot for your help !

This topic has been closed for replies.
Andrew Neil
Andrew NeilBest answer
Super User
July 5, 2022

"Is there a way to add the new data of the array at the end of the structure ?"

You'd have to add another, separate array - and your code would have to cope with concatenating the two parts:

struct
{
 :
 :
 original_array[ORIGINAL_SIZE];
 next_item;
 :
 :
 extra_array[EXTRA_SIZE]; 
}

"any other idea ?"

If you have plenty of Flash space to spare, Code which handles the new, bigger array just ignores the original_array, and takes everything from the extra_array.

struct
 {
  :
  :
  original_array[ORIGINAL_SIZE]; // No longer used
  next_item;
  :
  :
  extra_array[ORIGINAL_SIZE+ EXTRA_SIZE]; // Everything is now here
 }

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.

9 replies

Andrew Neil
Andrew NeilBest answer
Super User
July 5, 2022

"Is there a way to add the new data of the array at the end of the structure ?"

You'd have to add another, separate array - and your code would have to cope with concatenating the two parts:

struct
{
 :
 :
 original_array[ORIGINAL_SIZE];
 next_item;
 :
 :
 extra_array[EXTRA_SIZE]; 
}

"any other idea ?"

If you have plenty of Flash space to spare, Code which handles the new, bigger array just ignores the original_array, and takes everything from the extra_array.

struct
 {
  :
  :
  original_array[ORIGINAL_SIZE]; // No longer used
  next_item;
  :
  :
  extra_array[ORIGINAL_SIZE+ EXTRA_SIZE]; // Everything is now here
 }

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
NCatt.1
NCatt.1Author
Associate III
July 5, 2022

This is what I thought I would have to do. This involves many changes in my code for managing it but no choice.

Thanks anyway for your answer !

Andrew Neil
Super User
July 5, 2022

@NCatt.1​ ​  "This involves many changes in my code"

Then this may be a good opportunity to re-factor your code so that's no longer the case?

eg, make "getters" for retrieving stuff from your struct - then the only change would be in the "getter" ...

If your question is now resolved, please mark the solution:

0693W000008y9fZQAQ.png

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
July 5, 2022

hang on a minute:

@NCatt.1​  "The problem is that data next to this array will change location by doing this, what I would like to avoid."

Why is that a problem that you want to avoid?

Surely, the whole point of a struct is that you access its members by name - so it shouldn't matter where they appear within the struct?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
NCatt.1
NCatt.1Author
Associate III
July 5, 2022

Because I store device configuration in this structure, and at restart I use it to reset or not the device.

If there is a new variable between 2 restarts, variables after this new will be shifted in memory and data will be lost

waclawek.jan
Super User
July 5, 2022

> If there is a new variable between 2 restarts, variables after this new will be shifted in memory and data will be lost

I don't understand, can you please elaborate more on this? Perhaps giving a simplified example? Or illustrate it with some drawing?

Are you talking about migration between old and new version of the firmware?

JW

Andrew Neil
Super User
July 5, 2022

@Community member​ "I don't understand"

Neither do I.

For the struct to change in Flash, the code running on the STM32 would have to update it - so that update should ensure that data is not lost?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.