Pointers and Dynamic Arrays - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Pointers and Dynamic Arrays

Description:

double * t is NOT a char variable. P is NOT an int variable. q, r are ... Use casting to change the type of a pointer if needed, eg, r = (double *) p; x. int ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 22
Provided by: hku99
Category:

less

Transcript and Presenter's Notes

Title: Pointers and Dynamic Arrays


1
Pointers and Dynamic Arrays
  • Recall that a variable is a symbolic name of a
    memory cell. A memory cell has an address.
  • The value of a variable is the value stored in
    the cell
  • The address of a variable is the address of the
    cell
  • A pointer is the memory address of a variable

2
Basic components of a computer
3
  • Main Memory (Primary Memory)
  • Individually accessible storage cells with
    addresses
  • Each cell stores 1 byte (8 bits)
  • Random access memory (RAM)
  • Read write
  • Electronic, silicon chips
  • Fast
  • Volatile
  • (Data are lost when power is off.)

4
Pointers are addresses
  • RAM addresses are commonly shown as arrows in
    figures and thus called pointers.
  • In most cases, programmers do not care the exact
    value of an address.

5
Pointer variables
  • A pointer variable contains a memory address. In
    the example below,
  • x is an int variable
  • p is a pointer variable for storing the addr of
    an int value
  • x (1001100) is the memory address of x
  • p (5) is the int value in the memory cell
    pointed by p

int x 5, p p x cout ltlt The value of p
is ltlt p
6
Declaration of pointer variables
  • In a declaration, we specify the type of value
    pointed by a pointer variable. The general form
    is lttypegt ltptr_var1gt, ltptr_var2gt,
  • Examples
  • char t
  • int p
  • double q, r
  • Student_rec s NULL
  • NULL (/) is a pointer constant. It is commonly
    used to indicate that a pointer variable is
    pointing to nowhere.

t is NOT a char variable. P is NOT an int
variable. q, r are NOT double variables. s is NOT
a variable of type Student_rec. They are pointer
variables.
7
Pointer operations
  • (Assignment, binary) assigns an address to a
    pointer
  • variables, eg, p x q NULL r
    q
  • (Address-of, unary) gives the address of a
    variable, eg, x
  • (Dereferencing, unary) refers to the memory
    cell pointed
  • by a pointer
  • p 5 //put the value 5 in the cell
    pointed by p
  • x p //assign x to the value in the
    cell pointed by p
  • , ! (Comparison, binary)
  • checks whether two pointer values are
    equal or not
  • eg, if (p NULL)
  • if (q ! r)

8
Type compatibility
  • There are many types of pointers. The type of a
    pointer depends on the type of the value it
    points to.
  • Assigning a pointer to a pointer variable of
    different type is not allowed, eg,
  • int x, p
  • double q, r
  • q x
  • r p
  • Use casting to change the type of a pointer if
    needed, eg, r (double ) p

9
A quiz on pointers
Give the values for the following x is
x is x is
p is p is
p is
5
1001100
???
1001100
1100100
5
What is wrong in this example?
int x, p x 10 p x
p is undefined, so is p. If a pointer variable
is not initialized, its value is undefined until
it is assigned a value.
10
Swapping the values of two variables (A C
version)
int main() int x 5, y 0 swap
(x, y) cout ltlt " x " ltlt x
ltlt "\n y " ltlt y return 0
0
5
x 0 y 5
void swap( int a, int b) int t
t a a b b t
5
11
C supports pass-by-value only!
  • The pass-by-reference mechanism is an additional
    feature of C.
  • In C, the effect of pass-by-reference is achieved
    by passing down addresses explicitly.
  • The program in next page is a pure C program
    saved with the extension .c instead of .cpp, eg,
    swap.c. It uses the output function printf() in
    the C library ltstdio.hgt, instead of cout in the
    C library ltiostreamgt. A C program can be
    compiled using a C compiler.
  • The function swap( a, b) exchanges the values of
    two variables that match the parameters a and b.

12
Swapping the values of two variables (A C version)
int main() int x 5, y 0 swap
(x, y) printf( "x d\ny d\n",
x, y) return 0
0
5
x 0 y 5
void swap( int a, int b) int t
t a a b b t
5
13
The name of an array is a pointer variable
  • int, double, char, bool are primitive data types.
    The value of a variable of the
    primitive type is stored in the memory cell
    represented by the variable.
  • On the other hand, an array variable stores
    the address of the first entry of the
    array. The array name can be
    used as a constant pointer variable

double a 6., 3., 12., 5., 7.1, 5.
cout ltlt "a0 " ltlt a0 ltlt endl cout ltlt "a
" ltlt a ltlt endl
a0 6 a 6
14
Accessing an array using pointer arithmetic
  • double a 6., 3., 12., 5., 7.1, 5.
  • for (int i0 ilt6 i)
  • cout ltlt ai ltlt " "
  • cout ltlt endl
  • double p a
  • for (int i0 ilt6 i)
  • cout ltlt p ltlt " "
  • p
  • cout ltlt endl

7688
a
6.0 3.0 12.0 5.0 7.1 5.0
7688
a0
a1
a2
The amount of increment is the no. of bytes
representing the value pointed by p.
It is 8 in this case.
p
7688
6 3 12 5 7.1 5 6 3 12 5 7.1 5
15
An array is passed to a function as a pointer
variable
  • When we pass an array (by value) to a function,
    the values of the array are NOT copied to the
    corresponding parameter of the function. Instead,
    the array name is treated as a pointer
    variable--the address stored (address of the 1st
    entry) is passed to the corresponding parameter.

16
  • int main()
  • double a 6. , 3., 12.0, 5.0, 7.1, 5.0
  • . . .
  • cout ltlt search( a, 6, 5.0) ltlt endl
  • . . .

int search( double x, int length, double
key) . . .
17
The values of an array can be changed in a
function
  • There is only one copy of the array. The change
    of array elements in a function retains after the
    execution of the function ends.

int main() int a3 1, 2, 3
cout ltlt " Address of a0 is " ltlt a0
ltlt endl cout ltlt " Address stored in
a is " ltlt a ltlt endl check( a)
cout ltlt " a0 " ltlt a0 ltlt endl)
1
9
2
3
void check( int x) cout ltlt " Address
stored in x is " ltlt x ltlt endl x0
9
Address of a0 is 0x22ff68 Address stored in
a is 0x22ff68 Address stored in x is
0x22ff68 a0 9
18
Dynamic Arrays
  • Sometimes, we need to use huge arrays but their
    sizes are not known when the programs are
    written. In some cases, we do not need to use all
    the arrays at the same time. To save storage
    resource, we shall create these arrays when
    needed during execution, and delete the arrays as
    soon as their utilization are done.
  • A dynamic array is an array that is created using
    the new operator during execution.
  • A dynamic array can be much bigger than an
    ordinary array. Eg, in a specific PC, the size of
    an ordinary array is bounded by 2M bytes while a
    dynamic array can be as big as 256M bytes.

19
The freestore of the OS
  • When a program is executed, the OS allocates
    memory for storing the object code and for the
    variables of the program. This piece of memory is
    called program area
  • An ordinary (automatic) array resides in the
    program area
  • The freestore is a collection of all free memory
    cells in a computer. It is managed by the OS.
  • A dynamic array resides in the freestore of the
    OS
  • The new operator is used to allocate memory cells
    from the freestore
  • The delete operator is used to release memory
    cells to the freestore

20
Programming dynamic arrays
char x1048576 //Ordinary array
x1048575 'A' cout ltlt x1048575 ltlt
endl const int n 1024 1024
256 char y //Dynamic array (a
pointer to a character) y new charn
//Creation of a dynamic array yn-1
'B' //Use y as an ordinary array
cout ltlt yn-1 ltlt endl delete y
//Release the dynamic array to free store
y
x
Program area

A B

Freestore
21
Declaration, creation, referencing and deletion
  • The declaration of a dynamic array is the same as
    a pointer to an array element, eg, int p
  • A dynamic array is created using the new operator
    in the form ltarray-namegt new lttypegt
    ltno-of-elmsgt Eg, p new int10000000
  • The referencing of a dynamic array is the same as
    an ordinary array, eg, cin gtgt p0 n pi
    p2 3
  • Once the use of a dynamic array is finished,
    release the memory cells it occupies to the
    freestore using the delete operator, eg, delete
    p
  • YES THAT IS ALL FOLKS!!!!!!!!!!!!!!
Write a Comment
User Comments (0)
About PowerShow.com