How we change what others think, feel, believe and do

| Menu | Quick | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 9 : Data 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 -->


Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |


You can buy books here

More Kindle books:

And the big
paperback book

Look inside


Please help and share:


| Home | Top | Menu |

© Changing Works 2002-
Massive Content -- Maximum Speed