Advanced usage of the static_assert function?

Hi happy holidays. :beer:

I have a question about whether I can create more elaborate error messages using the static_assert function.

Below is a snippet of a program I've made, which saves a struct in multiple locations on an external I2C FRAM. I'm running the program on an ESP32.

The example struct is 20 bytes in size. You alter the "numWriteLocations" variable to specify the number of separate 20 byte memory locations that you want the struct to be stored in.

#include <assert.h>

struct __attribute__((packed)) STRUCT {
  uint32_t counter;    
  float variableOne;
  uint32_t variableTwo;
  uint32_t variableThree;
  uint32_t CRC;
} testStruct;

const uint8_t sizeOfData = sizeof(testStruct);

const uint8_t numWriteLocations = 2;

static_assert(((sizeOfData * numWriteLocations) < 70), "Error: too many write locations");

if you set numWriteLocations = 2, that means the struct is saved in 2 separate locations (so 40 bytes of FRAM total). These are then saved in first 20 bytes of the FRAM (addresses 0-19), and then the second 20 bytes of FRAM (addresses 20-39).

I am using the static_assert function, to set a limit on the amount of memory that can be allocated for all of these copies of the struct.

If the limit is set at 70 bytes, then you get an error message if you set the number of write locations (numWriteLocations) to equal 4, as this will take up 80 bytes (20*4) of memory.

This basic error message above works as expected.

The question:

I am wondering if I can print out more information about the error, such as how many bytes over the limit I am, and the maximum amount of allowable write locations I can have.

So, using the previous example, if I set numWriteLocations = 4, and I am 10 bytes over the limit, the error message would say;

"Error: too many write locations, you are 10 bytes over the 70 byte limit. The maximum allowable write locations are 3"

This desired error message would then adapt to changes in the sketch. For example, if you modified the struct to be 30 bytes in size, kept the numWriteLocations = 4, and kept the 70 byte limit, then you would get the error;

"Error: too many write locations, you are 50 bytes over the 70 byte limit. The maximum allowable write locations are 2"

Is this all possible? Would be handy.

thanks

Since message has to be a string literal, it cannot contain dynamic information or even a constant expression that is not a string literal itself.

So the answer is no

i'm confused by what you're trying to do

asserts are typically used to recognize conditions that should never occur, usually during development, and usually at run time. but if it looks like your size limit can easily be detected at compile time with a #if

i'm also guessing that your struct may grow in size and exceed some limit, but this is not clear.

and it looks like you recognize as an error that the value is "< 70" when i would have thought it should be "> SomeLimit"

not clear. are you attempting to save debug information in non-volatile memory, like a log file or just parameters such as states that can be restored at next power up or configuration set just once

With some trickery, you can report some useful information, e.g.,

template <bool condition, int num, int maximum> void myAssert() {
  static_assert(condition, "Error: too many write locations.");
}

void setup() {
  int const size {20};
  int const num {4};
  myAssert<num * size < 70, num, 70 / size>();
}

Output:

test.ino: In instantiation of 'void myAssert() [with bool condition = false; int num = 4; int maximum = 3]':
test.ino:9:45:   required from here
test.ino:2:3: error: static assertion failed: Error: too many write locations.
   static_assert(condition, "Error: too many write locations.");
1 Like

Thanks @jfjlaros, that sounds like a good compromise. Your textparser library is also coming in handy for debugging this project :wink:

@gcjr I'm working on a power loss protection feature, which is best described here:

The static asserts are just used to make sure I have enough FRAM memory for some other things I may add down the track.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.