Lecture 4: Functions and Modularization II


Data Attributes

All data in a C++ program can be described by its data attributes. Where in memory each variable is defined and its storage class determines these attributes. The five data attributes are:

  • Data Type: By the data type of a variable is meant:

    • how much memory does the variable require

    • how is that memory structured

    • how is that memory accessed by functions and operators.

  • Storage Class: By storage class is meant what type of memory (e.g. registers, stack or heap) is assigned to the variable. In C++ there are five storage classes:

    • automatic is defined on the stack

    • register is defined in the CPU's registers or defaults to the stack

    • static is defined on the heap

    • external is defined on the heap

    • dynamically allocated is defined on the heap

  • Duration or Lifetime: By duration or lifetime of a variable is meant how long a variable will be assigned its memory location. The lifetimes of a C++ variable are:

    • life of the program

    • life of a function

    • life of an object of a class

    • life of a block of code

  • Linkage: By the linkage of a variable is meant the ability of the variable to have its memory or its memory location shared between different files within the same program.

  • Scope or Visibility: By the scope of a variable is meant where the variable is visible or where can it be accessed. In C++ there are five scopes of a variable:

    • block

    • function

    • file

    • class or class object

    • program

Automatic Storage Class

A variable that is defined within a block or a function may be an automatic variable. A variable may become an automatic variable by preceding the definition of the variable with the keyword auto as in the following:

image from book

 auto float grossAmount; 

image from book

If auto is not explicitly specified for variables (nor any other storage class label) that are defined within a block or function, then automatic is the default. Some programmers refer to these as local variables but they are not the only type of local variables. See SCOPE1.CPP

Automatic variables:

  • may be any data type including programmer defined.

  • are defined in a block (or function) and are only visible within the block in which they are defined or in any nested blocks.

  • must be initialized each time the block is called otherwise they will contain "garbage".

  • are allocated memory on the stack each time the block is called. This can save memory except when used in a recursive function. In this case extensive use can cause stack overflow.

  • only exist during the lifetime of the block.

  • can not be linked with other files in the program.

See SCOPE2.CPP.

Register Storage Class

Register variables are similar to automatic variable except that they are requested to be stored in the CPU's register and therefore have no address. This type of variable is specified by using the keyword register in the definition of the variable as in the following:

image from book

 register short index; 

image from book

This definition is only a request for storage in the register and is not a demand. If the request is refused, the variable defaults to automatic. The attributes of register are therefore the same as automatic except for where it may be stored.

Any request for register variables should only be a short and the variable should be defined near its use to increase the request's possibility. The modifier short should be used rather than int because of the available storage in different CPU's registers. This type of variable is used to potentially increase the speed of the program. However this storage type should be checked for the system on which the program is designed to run on because not all compilers or operating systems permit the use of the register to store variables. See REGISTER.CPP Note: putch() was replaced with _putch() for security reasons in Visual Studio .NET 2005.

Static Storage Class

Static variables are defined by preceding the definition with the keyword static as in:

image from book

 static double invoiceAmount; 

image from book

Static variables

  • may be any data type including programmer defined.

  • are only visible within their area of definition

  • may be defined in one of the following areas

    • in a block,

    • in a function,

    • in a class

    • in a file within the program and outside of all functions.

  • are initialized when the program is compiled to ASCII 0 which may be changed but what ever value it has, the value is retained until the end of the program.

  • are allocated memory in the heap when the program is executed.

  • can not be linked with other files in the program.

The programmer must guard against too many static variables because they may use all available heap memory. See STATIC1.CPP

External Storage Class

External variables are not created by using any keyword in their definition. However if an external variable is to be accessed between files within the program, it must be declared in each of those files where it is not defined using a declaration that includes the keyword extern as in the following:

image from book

 extern float amount; 

image from book

External variables

  • must be defined outside of all functions and blocks

  • are visible anywhere within the program.

  • are initialized when the program begins to ASCII 0

  • have a lifetime of the program.

  • are allocated memory in the heap when the program is executed.

  • can be linked with other files in the program.

The programmer must guard against too many external variables because they may use all available heap memory.

For an example of using an external variable in a program create a project and use the following two files: externalheader.h and external.cpp

For additional examples on scope, see SCOPE3.CPP and SCOPPRTR.CPP.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net