492 pdfsam Cplusplus Primer Plus 4th Edition

This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks Stor...

0 downloads 27 Views 3KB Size
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks

Storage Duration, Scope, and Linkage Now that you've seen a multifile program, it's a good time to extend the discussion of memory schemes in Chapter 4, "Compound Types," for storage categories affect how information can be shared across files. It might have been awhile since you last read Chapter 4, so let's review what it said about memory. C++ uses three separate schemes for storing data, and the schemes differ in how long they preserve data in memory. Variables declared inside a function definition have automatic storage duration; that includes function parameters. They are created when program execution enters the function or block in which they are defined, and the memory used for them is freed when execution leaves the function or block. C++ has two kinds of automatic storage duration variables. Variables defined outside of a function definition or else by using the keyword static have static storage duration. They persist for the entire time a program is running. C++ has three kinds of static storage duration variables. Memory allocated by the new operator persists until freed with the delete operator or until the program ends, whichever comes first. This memory has dynamic

storage duration and sometimes is termed the free store. We get the rest of the story now, including fascinating details about when variables of different types are in scope, or visible (usable by the program), and about linkage, which determines what information is shared across files.

Scope and Linkage Scope describes how widely visible a name is in a file (translation unit). For example, a variable defined in a function can be used in that function, but not in another, whereas a variable defined in a file above the function definitions can be used in all the functions.

Linkage describes how a name can be shared in different units. A name with external linkage can be shared across files, and a name with internal linkage can be shared by functions within a single file. Names of automatic variables have no linkage, for they are not shared. A C++ variable can have one of several scopes. A variable having local scope (also