Doubts about C++ new/delete support #CPP


@torres98
 

Since there isn't any documentation about how to properly set-up a c++ application i'll ask here some questions about the topic. In issue #18990 it was pointed out the use of the kernel heap pool for the new and delete operators, and since then these simply use the "user" heap pool available through the implementation of malloc. After looking around, i found out that the (supposedly) right way of setting up the size of the user heap is through the option CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE. This seems to work correctly in a really basic c++ application, but there are a couple of pretty bad drawbacks:
  • I can't find a way to make exceptions work (for them to be enabled i need to turn off CONFIG_MINIMAL_LIBC, which i can't)
    • As a direct consequence, new can't raise std::bad_alloc when the allocation fails
  • Any standard c++ library can't be enabled (CONFIG_NEWLIB_LIBC or CONFIG_LIB_CPLUSPLUS seem to automatically set to zero CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE)
Am i doing something wrong?


Carles Cufi
 

Hi there,

 

I think exceptions need some extra work to be thread-safe, but Stephanos should know more.

Regarding the lack of documentation, we do have:

https://docs.zephyrproject.org/latest/develop/languages/cpp/index.html

 

Thanks,

 

Carles

 

From: users@... <users@...> On Behalf Of lucatorresetti via lists.zephyrproject.org
Sent: 09 June 2022 14:55
To: users@...
Subject: [Zephyr-users] Doubts about C++ new/delete support #CPP

 

Since there isn't any documentation about how to properly set-up a c++ application i'll ask here some questions about the topic. In issue #18990 it was pointed out the use of the kernel heap pool for the new and delete operators, and since then these simply use the "user" heap pool available through the implementation of malloc. After looking around, i found out that the (supposedly) right way of setting up the size of the user heap is through the option CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE. This seems to work correctly in a really basic c++ application, but there are a couple of pretty bad drawbacks:

  • I can't find a way to make exceptions work (for them to be enabled i need to turn off CONFIG_MINIMAL_LIBC, which i can't)
    • As a direct consequence, new can't raise std::bad_alloc when the allocation fails
  • Any standard c++ library can't be enabled (CONFIG_NEWLIB_LIBC or CONFIG_LIB_CPLUSPLUS seem to automatically set to zero CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE)

Am i doing something wrong?


Stephanos Ioannidis
 

Hi,

Since there isn't any documentation about how to properly set-up a c++ application
We have recently added https://docs.zephyrproject.org/latest/develop/languages/cpp/index.html

Any standard c++ library can't be enabled
C++ standard library support requires a fully featured C library bundled in the toolchain, which is the Newlib in case of the Zephyr SDK. You cannot use the toolchain-bundled C++ standard library with the Zephyr minimal libc.

these simply use the "user" heap pool available through the implementation of malloc
The `new` and `delete` operators internally make use of the `malloc` and `free` C library functions.

As noted above, if you need the C++ standard library support, you must enable the Newlib -- refer to the following documentation for more details on how the Zephyr Newlib integration handles dynamic memory management:

https://docs.zephyrproject.org/latest/develop/languages/c/newlib.html#dynamic-memory-management

Regards,

Stephanos


@torres98
 

Thank you so much to both of you, that solved my doubts.