topics that matter; ideas worth sharing

share a tip, submit a link, add something new

Pointers, uniqueness, and uniform behavior

January 28, 2001, 05:59 PM —  ITworld.com — 

In last week's column, we studied the rule of identity. We saw several ways in which identity is implemented, including relational database keys, file paths, and -- most significantly -- pointers.

Think back to the introduction of Pascal and C. Those languages quickly overtook FORTRAN and COBOL as the standards for application development. They did so because they offered two major advancements: structured programming (see sidebar) and pointers. The addition of pointers is what made those languages so powerful.

The beauty of structured programming

Pascal and C brought the concept of structured programming to application development. Structured programming encourages top-down design by breaking a sequential process down into discrete blocks of code. Conditional (if) and iterative (while) statements, rather than arbitrary jumps, control the flow of execution. In the purest form of structured programming, each block of code has only one entry point and one exit point. That greatly improved the readability and maintainability of source code and turned programming into a discipline.

--Michael L. Perry

For the first time, a high-level language permitted us to implement linked lists, binary trees, heaps, and other dynamic structures. Furthermore, it allowed us to represent those abstract data types with ease, type safety, and clarity of code. Pointers allowed us to traverse complex data structures with loops or recursive functions. They made it easier for producers and consumers of data to communicate. All of that was possible because the pointer is a natural implementation of the rule of identity.

Natural implementation

The rule of identity states that every object has unique identity independent of state. Every pointer is unique by virtue of the fact that no two objects can occupy the same memory. The rule also states that all clients accessing the same object can expect uniform behavior. Since a memory location may be in only one state at a time, pointers follow that clause implicitly.

Location independence was not an issue when Pascal and C first appeared, as applications were machine-centric. So pointers implemented identity quite naturally.

Problems with pointers

Having said all that, let me emphasize that pointers are not synonymous with identity. In some cases in which identity is required, pointers are insufficient, as we will discuss when we get into location independence. In others in which identity is not called for, pointers can accomplish other goals, as we will find when we examine the state pattern.

The following example illustrates that better than any words can.

Suppose that the class CMatrix represents a real-valued square matrix such as those used to solve systems of linear equations. One would expect, when using that class, to express the matrix multiplication in code as follows:


  CMatrix a, b, c;
// ... Fill in a and b. ...
c =a*b;

To support that syntax, we write a multiplication operator that returns a CMatrix object:


  CMatrix CMatrix::operator *(const CMatrix &that)
{
  CMatrix result;
  // ... Multiply "this" and "that" into "result". ...
  return result;
}

Can you spot the problem in the above code? It is not a bug, per se, but it is something that your users will complain about. We will discuss the problem next week, and discover what pointers and identity have to do with its solution.

» posted by abennett

ITworld.com

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources