Static variables

I know this is an old thread, but my question is related.

If a function is called constantly, memory is not an issue, and values do not need to be preserved, is it more efficient at runtime to use static variable anyway because then the variables won't have to be created and destroyed each time the function is called?

Regards.

if its simple types (int, double, char arrays, ....) then the creation and destruction time is non existant. the compiler just allocates pointers on the stack and refer to those variables with relative indexing. when the function exits, the stack pointer just goes back to where it was before the call and the memory is thus reclaimed (the data still exists on the stack but is considered garbage and will be overwritten by the next call that requires local variables)

more complex types - instantiating an object - will take creation and destruction time (eg that's another reason why it's a bad idea to use the String class :slight_smile: )

Split from an old topic

Understood. Thanks for the detailed reply.