[A89] Re: Variables in C


[Prev][Next][Index][Thread]

[A89] Re: Variables in C



>1.) There is no such thing as "the global variable stack."  Global variables
>are NOT stored on the stack; they're stored within the program's file and
>access to them is fundamentally different from local variables on the stack.
>You are correct that using global variables would be faster here, but the
>wrong reason, and your logic does not correctly extend to the general case
>(writing to global variables is much less efficient than writing to local
>variables, as writes to globals can't be PC-relative and thus require
>entries in the relocation table, greatly bloating executable size -- the
>real reason globals are faster here is that this data is only read from,
>never written to).

Tell that to Herbert Schildt, the author of "The Complete Reference to C 4th ed."
His source code implies that global variables are stored on a stack.

>2.) Making the sprite global will make the program SMALLER, NOT BIGGER!  In
>fact, the original poster made that observation in his post.  With a global
>sprite, you need only the global data.  With a local sprite, you need a
>global copy of the initial data, plus the code within the function to load
>it locally onto the stack.  This is always true for sprites, but note that
>it isn't always true for other kinds of variables/data.  In some cases,
>global variables that are written to a lot will bloat the program size a
>lot, because of the abovementioned relocation table entries required.

You can tell that to the authors of TIGCC.  Their informational file says just the opposite.

>3.) Declaring the variable as static is NOT a correct workaround and a VERY
>BAD IDEA.  The static keyword is complicated, and while it will eliminate
>the bloat here, it has side effects that you don't realize and still isn't
>the optimal solution.  The correct answer is to declare the array const (at
>which point global or not is irrelevant, but convention is to make it global
>because that extends its scope without any cost).

You know, this isn't really helping the original poster either; it is more of a personal critisism towards me.

>4.) Your "declar[ing] sprites locally and then moving them into a global
>struct" is just wrong and makes no sense whatsoever.  I don't know what the
>hell you're thinking, but I'm not going to try to figure it out.  This
>advice is bizarre and just plain wrong.

By declaring variables locally, especially arrays, you can assign a struct member the value of its pointer, thus allowing another function to obtain the array by using the member's pointer value.  Did you even SEE the sample code in the e-mail?

>The original poster would be well advised to ignore Jude's advice and refer
>instead to my earlier post, which gives a correct explanation and solution.

d00d, the least you could have done was send this directly to me instead of the news group and practically ridicule me in front of OVER HALF THE TI COMMMUNITY!

Jude Nelson

PS  Fuck you

Follow-Ups: