0% found this document useful (0 votes)
23 views3 pages

CISY 111 Lecture4 Pointers

Lecture notes on pointers

Uploaded by

nicholas riungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

CISY 111 Lecture4 Pointers

Lecture notes on pointers

Uploaded by

nicholas riungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3/7/2012

 When we want to manipulate data values in


our program, we must provide some form of
storage/memory for the values.
 Memory can be allocated in two ways
 Statically at compile time
LECTURE 4:  Dynamically at runtime
POINTERS AND DYNAMIC MEMORY  Binding refers to the association of a name
ALLOCATION with a memory location.
 The time when a variable is associated to a
memory location is called binding time.
 We have compile-time binding and runtime
binding.
CISY 111 Structured Programming October 2011 2

 When memory is allocated at runtime, we A pointer is a variable that holds a reference


use pointers. to another variable.
 A variable with runtime binding is said to be  It stores an address to a memory location.
dynamic.  Thevalues stored is hexadecimal
 Characteristics of dynamic memory  Pointers
form the key concept in the
allocation implementation of dynamic data structures.
 Structures can grow or shrink  They allow runtime memory allocation and
 Memory can be allocated and de-allocated deallocation.

CISY 111 Structured Programming October 2011 3 CISY 111 Structured Programming October 2011 4

 In C++ a we declare a pointer type before  In order to request dynamic memory we use
using it. the operator new followed by a data type
 Declaration takes the form: specifier.
 Data_type * name  If a sequence of more than one element is
 For example required- the number of elements is placed
 Int * nptr within brackets [].
 This declares a pointer to an integer value.  This returns a pointer to the beginning of the
new block of memory allocated.

CISY 111 Structured Programming October 2011 5 CISY 111 Structured Programming October 2011 6

1
3/7/2012

 General format:  Once memory is no longer needed it should


 pointer = new type be freed so that it becomes available for
 pointer = new type[number_of_elements] other requests of dynamic memory.
 Examples:  To achieve this, we use the operator delete
 int *num = new int(5);  General format is:
 int * p;  delete pointer;
 p= new (nothrow) int[i];  delete [] pointer;

CISY 111 Structured Programming October 2011 7 CISY 111 Structured Programming October 2011 8

 The
first expression should be used to delete A pointer does not store data values.
memory allocated for a single element,  It stores the reference to a data values
 The second one for memory allocated for arrays  The value pointed to by a pointer may be of
of elements. any valid data type.
 The value passed as argument to delete must  The operations on the data values depend
be either a pointer to a memory block or a purely on its type.
null pointer
 To reference the value pointed to by a
 In the case of a null pointer, delete produces no
pointer,
effect.
 *pointer
 For example
 *p refers to the value pointed to be p.
CISY 111 Structured Programming October 2011 9 CISY 111 Structured Programming October 2011 10

 Pointersstore a hexadecimal value.  Passing a pointer as a function argument is


 We optionally use the hex and dec operators same as passing a parameter by reference.
in output, to alternate between decimal and  Given a pointer called ptr, and a variable
hexadecimal output format. called var1, the following statements have
same effect
 Findsquare(ptr);
 Findsquare(&var1)
A function can also return a pointer type.

CISY 111 Structured Programming October 2011 11 CISY 111 Structured Programming October 2011 12

2
3/7/2012

 Write a C++ program that declares a pointer  Structures.


to an integer value, uses it to store a value,
the outputs both the address and the value.
 Write a C++ program that uses a pointer to
determine the area of a rectangle.
 Write a C++ program that uses a pointer to
add ten numbers.
 Write a C++ program that uses a pointer to
an array to store and display ten numbers.

CISY 111 Structured Programming October 2011 13 CISY 111 Structured Programming October 2011 14

You might also like