[comp.windows.ms] Memory Questions

AnchorMan@cup.portal.com (Brian Keith Carlisle) (12/21/89)

I'm new at Windows programming and have a few questions about memory 
management.  For that matter, I'm fairly new at C programming, although I've
had a good amount of experience with Pascal (which always handled memory
issues so comfortably).

I'm writing a Windows bases program in C and I declare several global
variables.  Where are these global variables put?  If they're put in the 
data segment, and I don't have to go through the "get handle, local lock/
global lock, etc.," how can I be sure that these variables are valid (since
my data segment can be moved by Windows)?

Also, as long as I'm asking questions, what are the advantages and 
disadvantages of Global Memory & Local Memory?

Thanks a lot for the info.

AnchorMan

paul@cscnj.csc.COM (Paul Moody) (12/22/89)

In article <25230@cup.portal.com>, AnchorMan@cup.portal.com (Brian Keith Carlisle) writes:
> 
> I'm writing a Windows bases program in C and I declare several global
> variables.  Where are these global variables put?  If they're put in the 
> data segment, and I don't have to go through the "get handle, local lock/
> global lock, etc.," how can I be sure that these variables are valid (since
> my data segment can be moved by Windows)?
> 
> Also, as long as I'm asking questions, what are the advantages and 
> disadvantages of Global Memory & Local Memory?

Global external(static) memory lives in your data segment, offset from
register DS. Windows Global Memory lives 'out there' and is offset
from register ES. Windows Local Memory is also in your data segment
offset from register DS. Automatic variables live on the stack.

Windows can do two different types of data moveing on DS based
memory. First, from one message to the next, the entire DS segment
could move, the value in the DS register changes. The second is more
complex. 

When your program starts, all external statics, declared outside of
any function, are allocated. Your data segment is allocated memory
equal to that plus the heap size you specified in your .def file plus
some that windows needs for its own housekeeping. The external statics
are not touched by windows. Any unlocked Local heap variables that are
marked as moveable can be moved if windows needs the space.

Local memory is easier to use than global memory, but it is a limited
resource. If you want to share data with other apps, you must use
global memory.

-- 
Paul Moody			UUCP: rutgers!cscnj!paul 
Computer Sciences Corporation
# the opinions expressed are entirely imaginary			#