17 Aug, 2009, JohnnyStarr wrote in the 1st comment:
Votes: 0
A recent thread brought up the benefit of using 'static' buffers for strings in certain situations.
The following code is in db.c of ROM 2.4:

/* * Clear a new character. */
void clear_char (CHAR_DATA * ch)
{
static CHAR_DATA ch_zero;
int i;
*ch = ch_zero;
ch->name = &str_empty[0];
ch->short_descr = &str_empty[0];
ch->long_descr = &str_empty[0];
ch->description = &str_empty[0];
ch->prompt = &str_empty[0];
ch->logon = current_time;
ch->lines = PAGELEN;

for (i = 0; i < 4; i++)
ch->armor[i] = 100;
ch->position = POS_STANDING;
ch->hit = 20; ch->max_hit = 20;
ch->mana = 100;
ch->max_mana = 100;
ch->move = 100;
ch->max_move = 100;
ch->on = NULL;

for (i = 0; i < MAX_STATS; i++)
{
ch->perm_stat[i] = 13;
ch->mod_stat[i] = 0;
}

return;}


It's obvious what the function is doing, however, by using the static keyword, it means this ch_zero is
set once, and then it is usable everywhere right? Is this now global to the mud? or just this file?
17 Aug, 2009, David Haley wrote in the 2nd comment:
Votes: 0
It means that the memory exists once, not that the variable name itself is global. In other words, when the function exits, that memory is not released; when the function starts, the memory is not cleared.

That code is actually behaving sketchily, for what it's worth. It's relying very strongly on the fact that the structure will be initialized to zeros, which is not a guarantee. I believe it is up to the compiler to decide how to initialize static variables; most do indeed initialize to zero, but that's making an implicit assumption that can get you into trouble.
17 Aug, 2009, JohnnyStarr wrote in the 3rd comment:
Votes: 0
So, if the ch_zero is not global, what is the use despite its apparent risk of not working?
Say you used it like so:
CHAR_DATA *ch;

clear_char(ch); -> sends pointer to structure


Because void clear_char takes a parameter of CHAR_DATA* what is the benefit of using static?
Because you are making changes to the data stored at the memory address, you wouldnt need to
return a reference right?
17 Aug, 2009, David Haley wrote in the 4th comment:
Votes: 0
Well, I think the author's intention was to only allocate the ch_zero memory once, and not allocate a new, fresh zero-memory CHAR_DATA at every function call. But this is a little silly, and sketchy to boot: if the author intended for the passed in CHAR_DATA* to be zeroed out, why not just memset the pointed-to structure to zeros?

But, anyhow, the advantage of static allocation in this event is that it only occurs once.

EDIT: fixed typo
17 Aug, 2009, JohnnyStarr wrote in the 5th comment:
Votes: 0
Thanks, it hurt my head a bit trying to figure out the benefit. :wink:
17 Aug, 2009, Erok wrote in the 6th comment:
Votes: 0
David Haley said:
That code is actually behaving sketchily, for what it's worth. It's relying very strongly on the fact that the structure will be initialized to zeros, which is not a guarantee. I believe it is up to the compiler to decide how to initialize static variables; most do indeed initialize to zero, but that's making an implicit assumption that can get you into trouble.


To be safe, try the following to initialize (once) the memory used by the structure to all zero. I believe this is portable…

static CHAR_DATA ch_zero = { 0 };


BTW, this link does a reasonable job explaining the different types of variable scope and storage specifiers in C.
18 Aug, 2009, JohnnyStarr wrote in the 7th comment:
Votes: 0
Erok said:
BTW, this link does a reasonable job explaining the different types of variable scope and storage specifiers in C.


I checked out your link at it says:
The static specifier was mentioned in the File Scope section, and can be used on local or
global variables to give them permanent duration for that file only. In other words, the
memory space reserved for the variable is not erased once the variable is out of its scope.

So, does that mean that if something is defined as 'static' within a function it follows file and function scope?
I wouldnt be able to access a variable specified as static from another function just because its within the same 'file'? Or by file is it
refering to something other than the *.c file? Sorry, I'm a little confused.
18 Aug, 2009, David Haley wrote in the 8th comment:
Votes: 0
Scoping rules don't change for locally scoped static variables except as far as the lifetime is concerned. Top-level static variables are (basically) reserved for that file only. (The function pointers can be passed around as normal, though.)

And yes, by 'file' it does mean a literal bla.c file.
18 Aug, 2009, Erok wrote in the 9th comment:
Votes: 0
Declaring a variable static ensures the memory for it remains allocated.

Declaring a variable static within a function or code block gives it local scope, meaning it can be referenced by name only within that function or code block.

Declaring a variable static outside a function gives it file scope, meaning it can be referenced by name only within that file.

In both cases, nothing prevents a function from returning the address of the static variable so it can be used outside of the variable scope.
0.0/9