News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Global Variables - Programming Method

Started by raleeper, August 21, 2011, 03:51:10 AM

Previous topic - Next topic

raleeper

Why not use global variables - whenever possible, or whenever you feel like it, or whenever you have a feeling that there might be some future benefit or whatever?

Is it purely a matter of style and making your programs accessible to others (and more easily understandable to yourself) or could excessive use of global variables cause crashes or incorrect operation?

I don't see how it could matter one way or the other to execution, but I am coming to realize the value of doing things the way others do unless there is a good reason to the contrary.

Thanks, robert

hutch--

Robert,

Its basically a matter of what scope you need for a variable, if it will only be used in one proc then make it LOCAL as it allows you to reuse the name in other procs. If you need the variable visible across the entire module, put it in the DATA section. The exception is when you are writing re-entrant code that may be called many times in parallel, in this context its easier to create a local structure and pass the address of the structure to any subroutines it may call.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

raleeper

Quote from: hutch-- on August 21, 2011, 04:42:29 AM
Robert,

Its basically a matter of what scope you need for a variable, if it will only be used in one proc then make it LOCAL as it allows you to reuse the name in other procs. If you need the variable visible across the entire module, put it in the DATA section. The exception is when you are writing re-entrant code that may be called many times in parallel, in this context its easier to create a local structure and pass the address of the structure to any subroutines it may call.

That makes sense to me.Thank you. So if, as a general rule, I make all my variables local unless there is a good reason to make the global, and I oneday do something re-entrant, I don't need to worry about this particular bug source (or something like that)>

Thanks.  And thanks for the Forum (or what I gather is your large part in creating and maintaining it).