16.2 Storing Data

Windows supports seven types of data storage, each of which is appropriate for different situations. The following list describes each type of storage, and suggests how to decide which type to use.

Type Description

Static data Includes all C-language variables that the application source code implicitly or explicitly declares by using the static keyword. Static data also includes all C-language variables declared as external, either explicitly (using the extern keyword) or by default (by declaring it outside the functions).
Automatic data Includes all variables that are allocated in the stack at the time a function is called. The variables include the function parameters and any locally declared variables. For more information about automatic data, see Section 16.2.1, “Managing Automatic Data Segments.”
Local dynamic data Includes all data that is allocated by using the LocalAlloc function. Local dynamic data is allocated out of a local heap in the automatic data segment to which an application's DS register is set. Allocating memory objects from the local heap of a Windows application is similar to allocating memory by using the malloc C run-time library function in a non-Windows application that uses the small- or medium-memory model. For more information about local dynamic data, see Section 16.2.2, “Managing Local Dynamic-Data Objects.”
Global dynamic data Includes all data that is allocated out of the Windows global heap by using the GlobalAlloc function. The global heap is a system-wide memory resource. Allocating memory objects from the global heap is roughly equivalent to using the malloc function in a non-Windows application that uses the compact-or large-memory model. The difference is that in Windows, your application allocates memory objects out of a heap potentially shared by other applications, while a non-Windows application essentially has the whole heap to itself. For more information about global dynamic data, see Section 16.2.3, “Managing Global Memory Objects.”
Window extra bytes Specifies extra bytes that are allocated in the data structure that Windows maintains internally for a window created by your application. To create this kind of window, register a class for it (by calling the RegisterClass function) and request that extra bytes be allocated for each window that is a member of this class. You request the extra bytes by specifying a nonzero value for the cbWndExtra member of the WNDCLASS structure that you pass to RegisterClass. You can then store data in and retrieve data from this area by making calls to the SetWindowWord, SetWindowLong, GetWindowWord, and GetWindowLong functions. For more information about Window extra bytes, see Section 16.2.4, “Using Extra Bytes in Window and Class Data Structures.”
Class extra bytes Specifies extra bytes that are allocated at the end of the WNDCLASS structure created for a window class. When you register the window class, you specify a nonzero value for the cbClsExtra member. You can then store and retrieve data from this area by making calls to the functions SetClassWord, SetClassLong, GetClassWord and GetClassLong. For more information about using class extra bytes, see Section 16.2.4, “Using Extra Bytes in Window and Class Data Structures.”
Resources Specifies nonmodifiable collections of data stored in the resource portion of an executable file. This data can be loaded into memory where your application can use it conveniently. You can define private resources that contain whatever kind of read-only data you want to store. You compile a resource into your executable (.EXE) or .DLL file by using Microsoft Windows Resource Compiler (RC). At run time, you can then access the resource data by using various Windows library functions. For more information about resources, see Section 16.2.5, “Managing Resources.”