Sie sind auf Seite 1von 28

sara_youssef90@yahoo.

com

Introduction
We can reach any place by it's name or it's address.
The same is for memory slots: we can access them by identifiers or memory addresses( using pointers)
myNumber x

25
3000 3001

77
3002 3003

Agenda
1. What is a Pointer? 2. Retrieving a Variable's Memory Address 3. Defining Pointer Variables 4. Accessing and Manipulating the Variable Pointed to 5. Passing by Reference to Function Using Pointers 6. Pointers and Arrays 7. Dynamic Allocation and Memory Management 8. Pointer Arithmetics 9. Linked List Example

1. What is a Pointer ?
Pointer is a variable that has the ability to point to a

certain slot within memory.


Pointer can do this by holding the memory address as

its value.
25

myPointer

myPointer 25 3001

3000

3001

3002

3003

2. Retrieving a Variable's Memory Address


to do so we need the address-of operator & .
int main () { int myNumber = 5 ;
cout << myNumber << endl; cout << &myNumber << endl ; // use the address-of & operator to retrieve the memory address of myNumber return 0 ; } 5
output:
myNumber

0xbff2630e

0xbff26312

0xbff26316

5 0xbff26312

2. Retrieving a Variable's Memory Address (cont.)


int andy = 25; int fred = andy; int* ted = &andy;

3. Defining Pointer Variables


Just like defining ordinary variables but with the

character " * " being added after the variable type and befor the variable name.
int * pPointer ;
One thing to take care about is that pointer type must be

the same as the type of the variable it will point to.

3. Defining Pointer Variables (cont.)


int main () { int* pPointer ; // declaring a pointer to integer int myNumber = 5 ; pointer = &myNumber ; // assign the memory address of myNumber to the pointer pPointer cout << myNumber << endl; cout << &myNumber << endl ; cout << pPointer << endl ; cout << &pPointer << endl ; cout << *pPointer << endl ;

return 0 ;
}

myNumber

pPointer

5 0xbff2630e

............

0xbff2630e 0xbff26324

what is the output ??

4. Accessing and Manipulating the Variable Pointed to


Process accessing a variable's value by a pointer is called

indirection, since the value of variable is accessed indirectly.


In the previous example: value of myNumber can be now indirectly accessed with a use of pPointer pointer. To do that we need to dereference a pointer with

dereference operator " * " which needs to be placed before a pointer variable name. cout << *pPointer << endl ;

4. Accessing and Manipulating the Variable Pointed to. (cont.)


int beth = *ted;

4. Accessing and Manipulating the Variable Pointed to. (cont.)


int main () { int* pPointer ; // declaring a pointer to integer int myNumber = 5 ; pPointer = &myNumber ;// assign the memory address of myNumber to the pointer pPointer cout << myNumber << endl ; cout << *pPointer << endl ; myNumber++ ; cout << "myNumber after direct increment: " << myNumber << endl ; (*pPointer)++ ; // brackets are essential because of precedence cout << "myNumber after indirect increment: " << myNumber << endl ; return 0 ; }

4. Accessing and Manipulating the Variable Pointed to. (cont.)


output:
5 5 myNumber after direct increment: 6 myNumber after indirect increment: 7

5. Passing by Reference to Function Using Pointers


In addition to passing by value and reference, passing arguments to a function

can be also done by using pointers.

void increment ( int* a ) { (*a)++ ; // brackets are essential because of precedence } int main () { int myNumber = 5 ; increment( &myNumber );

cout << myNumber << endl ; return 0 ;


}

6. Pointers and Arrays


an array name is a constant pointer to its first element. The relationship between c++ pointers and arrays are

closely related and its use is almost interchangeable.


grades

int grades[4] ;

6. Pointers and Arrays (cont.)


int main() { int Marks [10]= {1,2,3,4,5,6,7,8,9,0}; // Print out the memory address of an array name cout << Marks << endl; // Print out the memory address of a first element of an array cout << &Marks[0] << endl; // Print out value of the first element by dereferencing a array name cout << *Marks << endl; return 0; }

output:
0xbf83d3fc 0xbf83d3fc 1

6. Pointers and Arrays (cont.)


An array name is indeed a pointer to its first element. Therefore it is legal to access the array elements by a

const pointer.
The 6th element of an array can be referenced with a

pointer expression *(Marks + 5), the " + " sign tells the compiler to move 5 elements from the start of the array.

6. Pointers and Arrays (cont.)


int main() { int Marks [10]= {1,2,3,4,5,6,7,8,9,0}; // Create a constant pointer to Marks array const int *pMarks = Marks; // Access a 6th element of an array by pMarks pointer cout << *(pMarks + 5) << endl; // Access a 6th element by dereferencing array name cout << *(Marks + 5) << endl; // Access a 6th element of an array cout << Marks[5] << endl; return 0; }

OUTPUT
6 6 6

6. Pointers and Arrays (cont.)


In our example the elements are integers
Integer is usually 4 bytes

So in ( *(Marks+5) ) the " + " sign will cause the

pointer to point to a memory address 20 bytes behind the address reserved by the first array element and thus pointing to the 6th element.

6. Pointers and Arrays (cont.)


int main() { int Marks [10]= {1,2,3,4,5,6,7,8,9,0}; // Create a constant pointer to Marks array const int *pMarks = Marks;

for (int i=0, bytes=0; i < 10; ++i, bytes+=4) { cout << "Element " << i << ": " << pMarks << " + "; cout << bytes << " bytes = " << (pMarks + i) << endl; } return 0;
}

7. Dynamic Allocation and Memory Management


7.1 Stack and Heap Memory:
Stack ( static allocation ): size determined in the source code allocated by the compiler, at compile time once allocated doesn't change

Heap( dynamic allocation ): allocated by program at run time.

7. Dynamic Allocation and Memory Management (cont.)


7.2 Allocating a memory with new operator:
int main() { int * pPointer; 31 pPointer = new int; *pPointer = 31; pPointer

cout << *pPointer << endl; return 0;


}

Stack

Heap

7. Dynamic Allocation and Memory Management (cont.)


to dynamically allocate a sequence of more than one

element, we use the new[] operator, with the number of elements within the square brackets pPointer = new int[4];

pPointer

Stack

Heap

7. Dynamic Allocation and Memory Management (cont.)


7.3 Deallocating memory with delete operator:
we use the delete operator to free the dynamically

allocated memory

int main() { int * pPointer; pPointer = new int; *pPointer = 31; pPointer delete pPointer; cout << *pPointer << endl; return 0;
} Stack

31

Heap

7. Dynamic Allocation and Memory Management (cont.)


to deallocate a sequence of elements we use the delete[ ]

operator:

delete[] pPointer;
Exercise: allocation of dynamic 2D array and

deallocation.

8. Pointer Arithmetics
char *mychar; short *myshort; long *mylong;
//suppose that they point to memory locations 1000, 2000 and 3000 respectively

mychar++; myshort++; mylong++;

9. Linked List Example

References
http://linuxconfig.org/c-understanding-pointers
http://www.cplusplus.com/doc/tutorial/dynamic/ http://www.cplusplus.com/doc/tutorial/pointers/