Defining Programming Standards   
for Professional Programmers 
  

         

Home

Contents

1: Standards

2: Psychological Factors

3: General Principles

4: Commenting

5: Naming

6: Code Layout

7: File Layout

8: Language Usage

9: Data Usage

10: Programming Usage

11: Implementing Standards

A: Example Standard

B: References

C: Glossary

Syque

About

Share this page:

Google
C Style
syque.com
Web

 

 

Books and
more at:

USA:

In association with amazon.com

UK:

In Association with Amazon.co.uk

Canada:

In Association with amazon.ca

 

 

CHAPTER 9 : Data Usage

PART 4 : USAGE

CHAPTER 9 : Data Usage
9.1 Declarations
9.2 Using floating point numbers
9.3 Using 'typedef'
9.4 Using global data
9.5 Using Structures
9.6 Using Unions
9.7 Using Arrays
9.8 Using Pointers
9.9 Using bit structures
9.10 Using Constants
9.11 Using 'static' declarations
9.12 Initializing variables
9.13 Summary

<--Prev page | Next page -->

 

9.4  Using global data

When data is to be shared between a number of functions, and has no owner, it is common to define a set of global data which is referenced by all relevant functions.

Global data, however is generally recognized as a bad thing, particularly if it appears in large amounts and with little control. It is difficult to debug and maintain, as it is usually far from clear who does what to which, when and where. It can also be hazardous in multi-threaded programs.

There are some alternatives to using global data:

9.4.1  Pass the pointer

The first alternative to global data is to have an owner function for the data, which either declares it as static data or allocates it from dynamic memory. This function now passes a pointer to this data to functions that it calls (and these may pass on the pointer to functions that they call).

 

/* in kbdmgr.c */
static KB_BUFFER Keyboard;       /* buffer owned by keyboard manager */
...
ReadKbdStatus( &Keyboard );      /* call passes address of buffer    */

/* in kbdread.c */
void ReadKbdStatus( KB_BUFFER *pKeyboard );
{ ...

------------------------------------------------------------------------

This provides more control over the data than using straight global data, but can suffer from many of the problems. When the pointer is passed around below the owner's level, it can be very unclear who is doing what to the data, especially if it is large and complex.

9.4.2  Insulate the data

The second alternative to global data is for all operations on the data to be performed by a limited set of access routines. Other routines may still be passed a pointer to the data or, preferably, a 'handle' which only the access routines can interpret:

 

KbdStatus = ReadKbdStatus( hKeyboard );

 

9.4.3  Hide the data

The final alternative is to completely hide the data, although this is only possible if there is one set of data. The data is now completely owned by the accessing functions:

 

KbdStatus = ReadKbdStatus();

 

<--Prev page | Next page -->

 

 

  © Syque 1995-2010

Massive Content -- Maximum Speed