Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


Unspecified Bounds Error When an Array is Passed to a Macro

I attempted to use this macro:

#define ARSIZE(AR) (((char *)(&AR+1) - (char *)(&AR))/( (char *)(&AR[1]) - (char *)(&AR[0]) ))

in code such as this:

int array[] = {1,2,3,4,5};
size_t N =  ARSIZE(array);

The compiler outputs the error:

invalid use of array with unspecified bounds

on the attempt to expand the macro in the last line above.

My expectation was the compiler would simply plug in the text “array” where the AR argument is in the macro definition and compile the code. What I find instead is the same error as if the array were attempted to be passed to any function this way.

How can an array be passed to this macro?

I have done some experimenting and I believe I understand what has happened here. The source of the error is this part of the macro:

&AR+1

This would work if the compiler knew the size of the array AR. Although the global arrays passed as AR are static, and their fixed sizes are known at compile time, they are defined in a .c file other than the ones this macro is expanded in. All the compiler has to go by is the declaration in the header which does not now have size information.

For the same reason the sizeof operator gets the same error &AR+1 does. It appears this macro has no advantage over using the sizeof operator this way:

#define ARSIZE(AR) sizeof(AR)/sizeof(*AR)

Earlier I attempted to define the size and initial contents of the array in the header. I got errors. Should it be possible to do this? An example syntax I used in the header is:

extern int arr = { 1, 2, 3, 4, 5 };

The error was these arrays were being defined more than once, in spite of using the typical compiler directive ifndef to assure that everything is declared only once, and the first time the header is included.

I am seeking to ease code maintenance and reduce opportunities for the code to be accidentally broken later should these array size be changed latter. I want the compiler or code execution to determine number of elements in these arrays, and do it with as few variables or macros as possible.


 

Newest Forum Posts

  1. reservation car service Seattle by Jamesprede, 2025-05-01 10:06
  2. Last day: drone bonus by Danielrug, 2025-04-19 16:55
  3. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  6. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  7. Insightful Perspectives on This Subject by davidsycle, 2025-03-04 05:45
  8. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  9. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  10. Build a project in "release" mode by tang, 2025-02-20 10:36

Last-Modified Blogs