0

Problem description: There are 3 macros (configuration parameters) which are being used as indexes of arrays in multiple structures. Now, i want to redefine these macros as variables which read their respective values from flash memory and later being used as indexes for the arrays in structures.

struct1{
    int Array1[macro1];
    int Array2[macro2];
};

struct2{
    int Array1[macro1];
    int Array2[macro1];
};

struct3{
    int Array1[macro1];
    int Array2[macro3];
};

In case, the description is not complete, please ask.

Thank you

  • Is `macro1` the size of the VLA or an index? Note that "Variably-modified types cannot be members of structs or unions." ([ref](https://en.cppreference.com/w/c/language/array)) – Yun Sep 03 '21 at 08:37
  • similar question: https://stackoverflow.com/q/66629951/4989451 – tstanisl Sep 03 '21 at 08:48
  • `description is not complete, please ask.` I ask. I do not understand - `ArrayN` have not type, and `;` are missing. `i want to redefine these macros as variables which read` "redefine macros"? Why not define a function that reads (functions do actions, variables do nothing by themselves)? – KamilCuk Sep 03 '21 at 08:51
  • @Yun It will define the size. However, when i convert these macros to variables, it leads to error of 'variable modified in file scope'. – Gourav Kumar Sep 03 '21 at 08:51
  • Does this answer your question? [Flexible array member without having to be the last one](https://stackoverflow.com/questions/66629951/flexible-array-member-without-having-to-be-the-last-one) – Yun Sep 03 '21 at 08:54
  • In that case, this is not possible in standard C (see my ref in my previous comment). There is a GNU extension that allows it (see tstanisl's link). – Yun Sep 03 '21 at 08:55
  • @KamilCuk i included the datatype for arrays. I want to use variables instead of macros. These variables get their values from particular memory locations. I am not understanding what kind of function are you talking about. Can you elaborate? – Gourav Kumar Sep 03 '21 at 08:56
  • Kind of function `read_the_respective_value_from_flash_memeory(size_t idx)` that would just `memcpy` the data. I do not understand - so when `#define macro1 50` and `#define macro3 50` the code would just compile. Could you give examples of these macros and why the code can't be used as it is and what are you trying to achieve? (this looks like XY problem - you are trying to read from flash, so you do structs, so you ask about structs, yet your real problem is reading from flash). The `struct` keywords are missing. – KamilCuk Sep 03 '21 at 09:23

2 Answers2

0

GCC supports it but it is an "unintended" extension... really.

However, those types can only be defined at function body scope. See answer for exemplary use.

tstanisl
  • 13,520
  • 2
  • 25
  • 40
  • There's another question for this matter, too: https://stackoverflow.com/questions/12058760/undocumented-gcc-extension-vla-in-struct – Aconcagua Sep 03 '21 at 08:51
  • @Aconcagua, yes.. this extension should never be used outside GCC world – tstanisl Sep 03 '21 at 08:52
0

A call for common sense:

Assuming you want various arrays of different size depending on what value that is stored in flash, then you cannot use VLA nor dynamic memory.

Lets say that your size variable in flash is called X. There is a worst-case scenario of the largest allowed value of X, lets call that MAX_X. Your code needs to work under this worst-case scenario - it can't be allowed to access an array out of bounds and run off into the woods when the size is MAX_X.

This means that you need the following solution:

typedef struct
{
  int array [MAX_X];
  int size_used;
} some_type;

The actual array sized used is obtained by some_type my_type = { .size_used=X; };

It's nonsense to have the array as VLA because your code can't know how large X is in advance. It must always set aside MAX_X items. It's completely misguided to think there's some kind of memory being saved by allocating less bytes when the program isn't using them. Your program doesn't know how much memory that it is going to be used.

Now if anyone tells you to use malloc then you know they have likely never even seen a MCU let alone used one...

Lundin
  • 195,001
  • 40
  • 254
  • 396