Skip to main content
Graduate
January 7, 2025
Solved

memset & sprintf stack usage

  • January 7, 2025
  • 3 replies
  • 1748 views

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?

 

 

    This topic has been closed for replies.
    Best answer by KnarfB

    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  

    3 replies

    scotzapAuthor
    Graduate
    January 7, 2025

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

    Super User
    January 7, 2025

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

    scotzapAuthor
    Graduate
    January 7, 2025

    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?

    Super User
    January 7, 2025

    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?

     

    KnarfBAnswer
    Super User
    January 7, 2025

    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