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


Defining a Macro to Return the Number of Elements in an Array

As a convenience I attempted to define this macro for providing the number of elements in a static array of an arbitrary type:

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

This did not work because the size of operator cannot be directly used in a macro. I have seen here:

http://cs-fundamentals.com/tech-interview/c/implement-sizeof-operator-in-c.phpQuestion

how to create a macro that would work for the numerator of the above #define:

#define NUMSIZE(AR) (char *)(&AR+1) - (char *)(&AR)

But what about the denominator which needs to be the size of the type? Would this work?

#define DENOMSIZE(AR) (char *)(&AR[1]) - (char *)(&AR[0])

If it did work would it still work for a single element array?

Would the above method be more expensive in processing time than giving up on the use of a macro to do this and expanding to the equivalent of (sizeof(AR)/sizeof(*AR) manually?

I use this in OpenStm32 and with other toolsets:

#define COUNT_OF(x) ((sizeof(x)/sizeof(0x)) / ((size_t)(!(sizeof(x) % sizeof(0x)))))


See thisQuestion for more info.


How are you able to get the compiler to accept the sizeof operator in your macro? When I do it I get these errors:

invalid application of ‘sizeof’ to incomplete type ‘const float[]’
invalid application of ‘sizeof’ to incomplete type ‘const uint16_t[]’
invalid application of ‘sizeof’ to incomplete type ‘const uint32_t[]’


The storage size of a float[] would be unknown to the compiler.

I wonder if you’re passing a statically allocated array into a function with a ‘const float[]’ arguement. If so, then, you’re right - that macro would not work in the context of that function. The function could be getting an array of any size and the compiler wouldn’t be able to resolve the size of the array in the context of the function.

Some options:

  • Have that function reference the statically allocated array directly
  • Add an array size parameter to the function arguement list
  • Create (or re-use) a class that knows the size
  • Create a template with a size

I have figured out why the compiler does not accept this. The array is global. Its size and initialization is done in a .c file other than the one this error appears in. It is not sufficient that a header be used to externally declare it. The size and initialization would have to be done in the header, but doing that has caused other problems. I am giving up on doing it this way. I am going to have to define a size variable in the .c file in the next line after the array is defined. For example:

int arr[] = { 1, 2, 3, 4, 5 };
size_t arr_N = sizeof(arr)/sizeof(*arr);

This is the only way to be sure the compiler knows the size of the array in advance of using the sizeof operator.

There is no advantage to using the above NUMSIZE macro over the sizeof operator. That NUMSIZE macro will work only where the sizeof operator works.

In my application I am not going to need the overload checking your COUNT_OF(x) macro provides because I am working only in C, not C++. I can see how it can be useful in C++ though.


 

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