cancel
Showing results for 
Search instead for 
Did you mean: 

ThreadX, using new and delete with no static allocation

Gil1
Associate II

On the STM32H753, I have configured ThreadX with static allocation. When doing a new and delete, where does the memory come from? As well, where does the memory come from when a, for example, std::vector is created on the stack and then increased in size with a concatenation?

1 ACCEPTED SOLUTION

Accepted Solutions

The memory allocated by tx_byte_allocate is from a statically-allocated pool as it mentions in the note, not part of the heap. It dynamically allocates memory from a statically allocated pool. In that manner, you can always be assured that at least X bytes are available to allocate per thread.

New/delete (or malloc and free) come from the heap, which is typically after static allocation and before the stack.

 

> My linker file does not specify a heap location/size

You sure? It probably does. At least the location is probably specified, not the size. Look at the _sbrk implementation to see where it grabs memory from. Sometimes there's a check so it doesn't overflow the stack.

 

(You can edit your post with the down arrow menu at the top-right of your post.)

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
TDK
Super User

Unless you use tx_byte_allocate to allocate the memory, it will come from the normal place. In this case, the heap for std::vectors.

TDK_0-1758837059201.png

I would argue this is not really static allocation, despite what they are calling it. It still comes from a pool and is assigned as needed at runtime. That's just the heap with extra steps.

If you feel a post has answered your question, please click "Accept as Solution".
Gil1
Associate II

Thanks TDK,

My linker file does not specify a heap location/size, so I'm sure from where this memory is coming and the max size? As well, I am able to use 'new' and 'delete', does this memory come from the same heap? I agree with your point about this configuration not being dynamic.

 

Gil

Gil1
Associate II

Correction: "I agree with your point about this memory configuration not being static.

The memory allocated by tx_byte_allocate is from a statically-allocated pool as it mentions in the note, not part of the heap. It dynamically allocates memory from a statically allocated pool. In that manner, you can always be assured that at least X bytes are available to allocate per thread.

New/delete (or malloc and free) come from the heap, which is typically after static allocation and before the stack.

 

> My linker file does not specify a heap location/size

You sure? It probably does. At least the location is probably specified, not the size. Look at the _sbrk implementation to see where it grabs memory from. Sometimes there's a check so it doesn't overflow the stack.

 

(You can edit your post with the down arrow menu at the top-right of your post.)

If you feel a post has answered your question, please click "Accept as Solution".