Académique Documents
Professionnel Documents
Culture Documents
Fall 2016
Malek Smaoui
Outline
Learn about pointers and dynamic memory
allocation
Chapter 2 from textbook
Chapter 6 www.learncpp.com
Malek Smaoui
Pointers
C/C++ allows the programmer to have access to
memory addresses through pointers: one of the most
powerful and confusing aspects of the C language.
A pointer is a variable that stores the address of
another variable.
Pointer declaration: type * pointer_name;
The type corresponds to the data item being pointed to
by that pointer
exp:
int *pnPtr; // a pointer to an integer value
double *pdPtr; // a pointer to a double value
Malek Smaoui
Interpreted as
0x28ff40
0x28ff44
pnPtr
0x28ff44
nValue
5
Malek Smaoui
Dereferencing pointers
Pointers can be used to read and write to the variable or
memory location they point to via the dereference
operator *
A dereferenced pointer evaluates to the contents of the
address it is pointing to.
int nValue = 5;
cout << &nValue; // prints address of nValue
cout << nValue; // prints contents of nValue
int *pnPtr = &nValue; // pnPtr points to nValue
cout << pnPtr; // prints address held in pnPtr,
// which is &nValue
cout << *pnPtr; // prints contents pointed to
//by pnPtr, which is contents
//of nValue
Malek Smaoui
Another example
int x, y;
int *p1, *p2;
x = 42;
y = 163
p1 = &y;
p2 = &x;
alternatively
Malek Smaoui
Another example
p1 = p2;
*p1 = 17;
alternatively
Malek Smaoui
*p1 = *p2;
Pointer to struct
Pointers can point to any custom data type
Selecting a struct field from a dereferenced pointer to
the struct uses different operators:
(->) instead of the (*) and the (.)
exp:
struct Something
{
int nX, nY, nZ;
};
Something sValue;
Something *psValue = &sValue;
psValue->nX = 3;
psValue->nY = psValue->nX+1;
Malek Smaoui
Simple example
int values[20];
int *pValues;
pValues = values;
pValues[10] = 201;
//same as values[10]= 201;
*values = 20;
// same as *pValues = 20
// also same as values[0]=20;
Malek Smaoui
Pointer arithmetic
C allows integer addition or subtraction operations on
pointers. But, no pointer addition or subtraction.
Pointer arithmetic is not address arithmetic: adding 1
to a pointer is equivalent to adding
sizeof(pointer_type) to the address stored in the
pointer i.e. pointing to the next element of the pointer
type
If the pointer points to an array, pointer arithmetic
allow pointing to different elements if the array
commonly used case of pointer arithmetic
most common use is in loops, although indices are
preferred
Malek Smaoui
Example
int nValue = 7;
int *pnPtr = &nValue;
Malek Smaoui
Malek Smaoui
Example
int *pnValue = new int; // dynamically
// allocate an integer
*pnValue = 7; // assign 7 to this integer
delete pnValue; // free that integer
Another example
Dynamically allocated memory has no variable
name, only accessible through the pointer
name
char * ptr;
0x12FF80
ptr
???
*ptr = B;
0x12FF80
0x12FF80
ptr
0x28ff40
ptr
0x28ff40
0x28ff40
0x28ff40
66 (B)
Another example
cout << *ptr;
// prints B
delete ptr;
0x12FF80
ptr
???
Malek Smaoui
Good practices:
- Initialize pointers to 0 or
NULL if not allocated at
declaration.
- Deallocated pointers are
not set to 0 automatically.
Setting to 0 freed pointers
reduces sources of
pointer misuse
Program memory
int global = 0;
int main (int arg)
{
float local;
char *ptr;
ptr = new char[100];
local = 0;
local += 10*5;
..
.
foo();
. /* return addr */
.
return 0;
Local variables
Return address
Dynamically
allocated
Global variables
Program code
}
Malek Smaoui
Memory reallocation
Memory allocated with new can NOT be extended or
reduced on demand
unless programmer implements the necessary mechanism
Linked lists
A data structure to store aggregate data
allocating memory on demand for each new
element
Storage is not necessarily contiguous in
memory: linked lists are not indexable
Each element has two components:
actual data (int, float, char, struct, enum, .)
pointer to next element
Malek Smaoui
Linked lists
Malek Smaoui
Malek Smaoui
Linked lists
Advantage of linked lists over arrays:
No needed previous knowledge of number of
elements: memory allocated on demand
insertion and deletion of element is trivial, does
not need to move data around
Example of use
struct node {
int x;
node *next;
};
int main()
{
node *root = 0; // This will be the unchanging first
root = new node; // Now root points to a node struct
root->next = 0; // The node root points to has its
// next pointer set equal to a null pointer
root->x = 5; // By using the -> operator, you can modify the
// node a pointer (root in this case) points to.
node *newNode = new node; // adding a new element
root->next = newNode;
newNode->x = 6;
newNode->next = 0;
}
Implementation
Effective use of a linked list requires the
implementation of few routines according to
the users needs:
add an element at the end
find element (eventually return position)
insert element at position
delete element
print elements
Malek Smaoui
Exercise
Using the linked list definition in the example,
write the following functions:
void addNode(node *root, int x);
int findNode(node *root, int y, node *found);
void printList(node *root);
void insertNode(node *root, int x, int pos);
void deleteNode(node *root, int y);
Malek Smaoui