2025-01-07 09:30 AM - last edited on 2025-01-07 10:33 AM by Andrew Neil
hi i have a question about stack memory.
is memset or sprintf function take space on ram stack memory?
For example i create global array like my_array[1024].
when i call memset in my private function, does memset function take extra space on ram(stack memory).
What is the disadvantage using a lots of memset or sprintf usage in my code?
Solved! Go to Solution.
2025-01-07 09:50 AM
memset is an optimized for loop and will consume no or nearly no space on stack because is needs only very few registers. sprintf will probably use more bytes on stack, but this shouldn't be an issue. The stack amount is constant and only used during the lifetime of the function call, so it doesn't add up even when you have lots of calls.
It is relatively easy to fill the stack with a known byte pattern and check from time to time the portion of the stack that is still untouched.
hth
KnarfB
2025-01-07 09:34 AM
i will use memset for set all values of my_array to zero
2025-01-07 09:36 AM
In both cases it's you that provides the buffer into which they put their output - so the only stack space they're going to need is for temporary internal housekeeping.
@scotzap wrote:What is the disadvantage using a lots of memset or sprintf usage in my code?
Relative to what?
2025-01-07 09:40 AM
2025-01-07 09:50 AM
memset is an optimized for loop and will consume no or nearly no space on stack because is needs only very few registers. sprintf will probably use more bytes on stack, but this shouldn't be an issue. The stack amount is constant and only used during the lifetime of the function call, so it doesn't add up even when you have lots of calls.
It is relatively easy to fill the stack with a known byte pattern and check from time to time the portion of the stack that is still untouched.
hth
KnarfB
2025-01-07 10:03 AM
I need to reset the buffer I defined globally in different parts of my code. And sometimes I have to do this repeatedly. If I define my stack size to be smaller than 1024, will it cause a stack overflow when I want to reset my buffer using memset in a function I define? For example, I set my stack size to 800 bytes, let the sum of the sizes of the variables I defined in my function be 200 bytes, and I need to reset my 1024 byte global array using memset in this function, and I will reset it at 10 ms intervals in a for loop and fill it with my sensor data again. Does resetting a high buffer repeatedly using memset() cause any problems other than slowness, especially stack overflow?
2025-01-07 10:26 AM
@scotzap wrote:I need to reset the buffer I defined globally in different parts of my code. And sometimes I have to do this repeatedly. ?
OK - in that case, memset is the way to do it.
@scotzap wrote:If I define my stack size to be smaller than 1024, will it cause a stack overflow when I want to reset my buffer using memset in a function I define?
See @KnarfB's reply - the stack space used by memset is minimal.
memset writes direct to your buffer - it doesn't need to create a whole buffer of its own.
@scotzap wrote:Does resetting a high buffer repeatedly using memset() cause any problems other than slowness, especially stack overflow?
No problems.
2025-01-07 10:32 AM
Using the default arm-none-eabi-gcc toolchain from STM32CubeCLT,
int my_array[1024] = {0};
compiles to a call of memset :).
Your mileage may vary
hth
KnarfB