Every Windows program has at least one data segment called the default, or automatic, data segment. A program's DS and SS segment registers both point to this segment. In contrast to the ”global memory“ that Windows manages, this automatic data segment is called your program's ”local memory.“ Within Windows' global memory organization, your program's automatic data segment is most often a moveable but nondiscardable segment. The segment is called DGROUP.
In both regular MS-DOS C programs and Windows programs, the memory within DGROUP is organized into four areas, as shown in Figure 7-2. These four areas are described below:
Initialized static data—This area contains initialized variables defined outside of functions, initialized static variables within functions, and explicit strings and floating-point numbers.
Uninitialized static data—This area has uninitialized variables that are defined outside of functions and uninitialized variables defined as static within functions. In accordance with C standards, all uninitialized static variables are initialized to 0 when the data segment is created in memory.
Stack—This area is used for ”automatic“ data items defined within functions (variables not defined as static), for data passed to functions, and for return addresses during function calls.
Local heap—This is free memory available for dynamic allocation by the program.
The module definition (.DEF) file specifies your program's stack and local heap size:
HEAPSIZE 1024
STACKSIZE 8192
In a regular C program, you can allocate memory from the local heap using the malloc and calloc functions. In Windows programs, you can also allocate memory from the local heap, but you'll want to use the Windows memory allocation functions rather than the C functions. When you use Windows functions to allocate local memory, Windows organizes the local heap just like global memory, as shown in Figure 7-3. Although the stack is fixed in size, Windows can dynamically expand the local heap if you attempt to allocate more local memory than is specified in your module definition file. Windows can even move your data segment if that is necessary to expand the local heap.