One of the concepts that causes many new programmers endless frustration is pointers. It's based on the concept that every variable has a storage location in memory. Before we talk about pointers, we'll look at how a computer's memory works.
Memory consists, at its lowest level, of a bunch of bits which can be either 1 or 0. These bits are a little too small to be individually useful, and it would be highly impractical to access them one at a time. As a result, they are normally accessed in chunks. On personal computers, they are usually accessed in chunks of 8 bits. There's nothing overly special about 8. A system could just as easily work with chunks of 9, 12, 16 or 32 bits. For this tutorial, we'll use 8 bits.
Whatever a system works with is referred to as the smallest addressable block of memory. Since we are using 8 bits in our example, that means every variable will use one or more blocks of 8 bits. As a result, a sequence of several blocks of memory might look like this:
Each . represents a bit. Each block of bits is assigned a number (a memory address). A variable in C++ will occupy one or more memory blocks. For the purposes of working with memory, a char is defined to occupy 1 block (minimum of 8 bits). Other variables occupy a multiple of that many blocks. For the purposes of this tutorial, a char would occupy 1 of the memory blocks above (such as 01). An int would occupy 2 adjacent memory blocks (such as 01 and 02, or 03 and 04). A double would occupy 4 adjacent memory blocks (01, 02, 03, and 04).Code:|........||........||........||........| 01 02 03 04
Given all that, consider the following code:
If you look at a diagram of how the variables are stored in memory, It might look something like this (. is 2 bits):Code:int main() { int int1=5; double double1=3.14; int int2=10; char cha1='A',cha2='2'; return 0; }
Looking at this, the locations of the variables could be: int1: 02, double1: 04, int2: 08: cha1: 09, cha2: 10. Notice that I haven't said anything about what each variable contains, just where they're located. Also notice that a char is size 1 (as always), and that I'm assuming that int is size 2, and double is size 4. The number of bits in size 1 is often 8, but really depends on the architecture of the hardware. The purpose of a pointer is to store these addresses and provide us with access to the variables based on their addresses. You create pointers as follows:Code:|int1....||double1.........||int2....||cha1||cha2| 02&03 04-07 08&09 10 11
The & (address of) in front of int1 returns the address of int1, in this case 02. So intptr contains 02, dptr contains 04, and charptr contains 09. The * in the variable declaration indicates that the variable is a pointer to the type in front of the *. At this point, it may seem silly to use a pointer instead of just using the variable. Great, we can store a memory location. So what?Code:int main() { int int1=5; double double1=3.14; int int2=10; char cha1='A',cha2='2'; int* intptr = &int1; double* dptr = &double1; char* charptr = &cha1; return 0; }
One thing we can do is use the pointer to get access to what the variable stores. We do this with the * operator (dereferencing). That means *intptr will return 5, while intptr will return 02. The other thing you can do is use + and – on pointers to step forwards or backwards through memory. The distance +1 will move through memory depends on the type of memory the pointer looks at. Since intptr looks at ints (in this case size 2 (usually 16 bits)), if intptr contains 02, then intptr + 1 will return 04 (one int forward). Since dptr looks at doubles (in this case size 4), if dptr contains 04, dptr +1 will return 08 (one double forward).
At this point, you may think this means you can use the intptr to access a double. You can't. intptr + 1 will access the first half of double1. The result will depend on the underlying architecture. Similarly, dptr + 1 will interpret all of int2, cha1, and cha2 as a double. The primary value of pointers is receiving and working with arrays in functions. Consider the following code:
At first glance, it might appear that we passed an integer into loadarray. This is not the case. myarray is an array of ints, but the term myarray is actually a pointer to the first int in the array. When loadarray receives that pointer, it can then use int pointer thearray like an array name (yes, we have two pointers pointing to the first element of the array). The [] operator will interpret thearray as an array name and find the correct elements based on the index.Code:#include <iostream> void loadarray(int* thearray) { for (int i=0;i<10;i++) thearray[i]=i; } void printarray(int* thearray) { for (int i=0;i<10;i++) std::cout<<"element"<<i<<" "<<*(thearray+i)<<"\n"; } int main() { int myarray[10]; loadarray(myarray); printarray(myarray); }
The other major use of arrays is to create variables at run-time. Under normal circumstances, you must state in code what variables you will be working with. Sometimes, however, you don't know. When you're reading a file, you don't know how large it is, so you can't account for its size in advance. To successfully read it, you will need to grab memory as needed.
This technique is used frequently when working with data structures (to be discussed in another tutorial).Code:int main() { int* intptr = 0; //this makes sure intptr is safely set to point to NOTHING intptr = new int; //attempts to grab space for an int and assign the address to intptr intptr = new int; //attempts to grab a second space for an int and assign the address to intptr, this will result in a memory leak since the first space was claimed but no one knows where it is. *intptr = 5; //stores 5 in that space delete intptr; //frees the memory claimed earlier by the second new }
Since no one seems to like "really" good tutorial I will step forward and ask one simple question !
What's the advantage of pointers in C++?
Also rep+ !
Pointers are essential to building abstract data types and dynamic memory allocation. References in Java are equivalent to pointers in C++. If you look over my tutorials on linked lists and stacks and queues you'll see them in action.
They're different, but C++ references are very different from Java references. C++ pointers have a lot more power.
nice one Winged.. +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks