Open Blog >> | wadah untuk bertukar informasi — open ...



Day 8

o Pointers

▪ What Is a Pointer?

▪ Figure 8.1.

▪ Listing 8.1. Demonstrating address of variables

▪ Figure 8.2.

▪ Storing the Address in a Pointer

▪ Pointer Names

▪ The Indirection Operator

▪ Pointers, Addresses, and Variables

▪ Figure 8.3.

▪ Manipulating Data by Using Pointers

▪ Listing 8.2. Manipulating data by using pointers

▪ Examining the Address

▪ Listing 8.3. Finding out what is stored in pointers

▪ Pointers

▪ Why Would You Use Pointers?

▪ The Stack and the Free Store

▪ new

▪ delete

▪ Listing 8.4. Allocating, using, and deleting pointers.

▪ Memory Leaks

▪ Creating Objects on the Free Store

▪ Deleting Objects

▪ Listing 8.5. Creating and deleting objects on the free store

▪ Accessing Data Members

▪ Listing 8.6. Accessing member data of objects

▪ on the free store.

▪ Member Data on the Free Store

▪ Listing 8.7. Pointers as member data

▪ The this Pointer

▪ Listing 8.8. Using the this pointer

▪ Stray or Dangling Pointers

▪ Listing 8.9. Creating a stray pointer

▪ const Pointers

▪ const Pointers and const Member Functions

▪ Listing 8.10. Using pointers to const objects

▪ const this Pointers

▪ Summary

▪ Q&A

▪ Workshop

▪ Quiz

▪ Exercises

[pic]

Day 8

Pointers

One of the most powerful tools available to a C++ programmer is the ability to manipulate computer memory directly by using pointers. Today you will learn

• What pointers are.

• How to declare and use pointers.

• What the free store is and how to manipulate memory.

Pointers present two special challenges when learning C++: They can be somewhat confusing, and it isn't immediately obvious why they are needed. This chapter explains how pointers work, step by step. You will fully understand the need for pointers, however, only as the book progresses.

What Is a Pointer?

[pic]

New Term: A pointer is a variable that holds a memory address.

[pic]

To understand pointers, you must know a little about computer memory. Computer memory is divided into sequentially numbered memory locations. Each variable is located at a unique location in memory, known as its address. (This is discussed in the "Extra Credit" section following Day 5, "Functions.") Figure 8.1 shows a schematic representation of the storage of an unsigned long integer variable theAge.

Figure 8.1. A schematic representation of theAge.

Different computers number this memory using different, complex schemes. Usually programmers don't need to know the particular address of any given variable, because the compiler handles the details. If you want this information, though, you can use the address of operator (&), which is illustrated in Listing 8.1.

Listing 8.1. Demonstrating address of variables.

1: // Listing 8.1 Demonstrates address of operator

2: // and addresses of local variables

3:

4: #include

5:

6: int main()

7: {

8: unsigned short shortVar=5;

9: unsigned long longVar=65535;

10: long sVar = -65535;

11:

12: cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download