If you have:
extern void some_function(char * s, int l);
void do_it(void) {
char str[] = "I'm doing it!";
some_function(str, sizeof(str) );
}
This would turn into something like (in psudo asm for the processed processor):
.data
local .do_it.str ; The contents of str are stored in a static variable
.text ; text is where code lives within the executable or object file
do_it:
subtract (sizeof(str)) from stack_pointer ; This reserves the space for str, sizeof(str)
; and a pointer to str on the stack
copy (sizeof(str)) bytes from .do_it.str to [stack_pointer+0] ; this loads the local variable
; using to the memory at the top of the stack
; This copy can be a function call or inline code.
push sizeof(str) ; push second argument first
push stack_pointer+4 ; assuming that we have 4 byte integers,
; this is the memory just on the other side of where we pushed
; sizeof(str), which is where str[0] ended up
call some_function
add (sizeof(str)+8) to stack_pointer ; reclaim the memory used by str, sizeof(str),
; and the pointer to str from the stack
return
From this you can see that your assumption about how the local variable str is created is not entirely correct, but it is still not as efficient as it could be.
If you did
void do_it(void) {
static str[] = "I'm doing it!";
. some_function str, ( ) do_it ( ) str.
some_function :
extern void some_function(const char * s, int l);
, , , str do_it, , str , str static.