This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using C preprocessor to limit array size during compilation

Hello,

I want to use the #error directive to limit the size of a statically allocated array during compilation (it must not exceed a certain limit, and I want to fail compilation, not startup, if it is too large), but I can only work with constant expressions, of course. Is there a way to do this...?

Thanks.

Parents
  • "I don't want to limit the number of elements but the amount of RAM the array consumes - and to enforce that during compilation."

    Then ignoring your "various types" requirement, Andy's example becomes:

    #define MY_ARRAY_SIZE_MAX  4096    /* Limit array to 4K total */
    
    my_type my_array[MY_ARRAY_SIZE_MAX / sizeof(my_type)];
    

Reply
  • "I don't want to limit the number of elements but the amount of RAM the array consumes - and to enforce that during compilation."

    Then ignoring your "various types" requirement, Andy's example becomes:

    #define MY_ARRAY_SIZE_MAX  4096    /* Limit array to 4K total */
    
    my_type my_array[MY_ARRAY_SIZE_MAX / sizeof(my_type)];
    

Children