cancel
Showing results for 
Search instead for 
Did you mean: 

memset & sprintf stack usage

scotzap
Associate II

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

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  

View solution in original post

7 REPLIES 7
scotzap
Associate II

i will use memset for set all values of my_array to zero

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?

 


@scotzap wrote:

i will use memset for set all values of my_array to zero


If that's just for initialisation, why not

int my_array[1024] = {0}; 

 


@scotzap wrote:

For example i create global array like my_array[1024]. 


If it's global, it will be initialised to zero anyhow.

KnarfB
Principal III

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  

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?


@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.

Using the default arm-none-eabi-gcc toolchain from STM32CubeCLT, 

int my_array[1024] = {0}; 

 compiles to a call of memset :).

And the memset implementation uses no stack space at all. 
KnarfB_1-1736274709696.png

Your mileage may vary

hth

KnarfB