Title: Ch.6. An Introduction to Pointers
1Ch.6. An Introduction to Pointers
2Contents
- Addresses and Pointers
- Pointers to Array Elements
- Pointers in Function References
- Problem Solving Applied Seismic Event Detection
- Dynamic Memory Allocation
- A Quicksort Algorithm
-
36.1 Addresses and Pointers
- Address
- A uniquely defined memory location which is
assigned to a variable. - A positive integer value
ltAn analogy with post boxgt
4Notation for memory snapshot
memory address
contents
identifier
5Address operator
- To refer the address of a variable
- Address operator
- Ex) scanf statement
-
- scanf ( f, x)
- the value from the keyboard is to be stored at
x, - the address of x.
- FILE sensor
- sensor fopen ( sensor.dat, r )
- fscanf ( sensor, f f, t, motion )
6(No Transcript)
7Results of Program Chapter6_1
1
a
2
b
a 1 address of a 65524 b 2 address of b
65532
8(No Transcript)
9Results of Program Chapter6_2
?
a
?
b
a 0 address of a 65524 b 150 address of
b 65522
10Pointer Assignment
- Pointer
- Special type of variable to store the address
of a memory location - Pointer operator
- dereferencing (or indirection) operator
- Ex) int a, b, ptr
a
ptr
b
?
?
?
(The arrow indicates that the ptr is a pointer
variable)
11or
12bptr b is assigned the value pointed to by
ptr
13ptr b the value pointed to by ptr is
assigned the value in b.
14a
b
p
?
int a 1, b 2, p p a b p
1
2
a
b
p
1
2
a
b
p
b a
1
1
15- int i 3, j 5, p i, q j, r
- double x
-
- expression value why?
- ( p ( i ) ) 1
- ( ( p ) ) 3
- r ( x ) illegal
- ((( 7(p) ))/( q ))7 11
- ((r (j))) (p) 15
16(No Transcript)
17Results of Program Chapter6_3
18Give memory snapshots after each of these sets
of statements are executed.
19- Address Arithmetic
- (pointer) (pointer)
- (pointer) (pointer) (int)
- (pointer) or (pointer) --
- (int) (pointer) (pointer)
- (pointer) 0 or NULL
20Pointers to the same variable
- int x -5, y 8, ptr_1, ptr_2
- ptr_1 x
- ptr_2 ptr_1
21Some Common errors
- y ptr_1 attempt to change the address of y
- ptr_1 y attempt to change ptr_1 to a
nonaddress value - ptr_1 ptr_2 attempt to move an address to an
integer variable - ptr_1 ptr_2 attempt to change ptr_1 to a
nonaddress value
22- The memory locations assigned to the variables
-
- - No relationships!!
- - System dependent
-
- But for an array
- guaranteed to be a sequential group of memory
locations
?
a
int a, b
?
b
236.2 Pointers to Array Elements
ptr_x
memory allocation
?
?
?
?
?
x0
x1
x2
x3
x4
The memory location for x1 is immediately
follow the memory location of x0
24To point to x1
assign the address of x1
- ptr_x x1
- ptr_x x0
- ptr_x
- ptr_x
- ptr_x 1
-
increment ptr_x to point to the next value in
memory
Be careful!! Pointer arithmetic is different
from integer arithmetic
25- define N 100
- int aN, i, p, sum 0
- ai
- (ai)
-
- p a0 pa
- pi
- (p i)
- p a1
- p a 1
a p a a 2 a
Wrong!!! Why?
26- Pointer Arithmetic
- depends on the machine used
- depends on the variable type
- For examples,
- Short integers ( 2 byte )
-
- Floating point values ( 4 byte )
beginning ptr 45530 after ptr ptr 45532
beginning ptr 50200 after ptr ptr 50204
27(No Transcript)
28(No Transcript)
29Offset
double x61.5, 2.2, 4.3, 7.5, 9.1,
10.5 double ptr x0
- ptr refer to x0 value
- ( ptr1 ) refer to x1 value
- ? ?
- ( ptrk ) refer to xk value
k is an offset
30- int k
- double x6, sum0
- for (k0 klt5 k)
-
- sum xk
-
- int k
- double x6, sum 0, ptr x0
- for (k0 klt5 k)
-
- sum (ptr k)
-
31 ptr x0 ptr x
32(No Transcript)
33Two-Dimensional Arrays
Array Definition int s23 2,4,6,
1,5,3
Offset
Memory allocation
34int s23, srow2, scol3, i, j, sum 0 for (
i 0 i lt srow-1 i ) for ( j 0 j lt
scol-1 j ) sum sij
int s23, s_count 6, k, sum 0, ptr
s00 for ( k 0 k lt s_count-1 k)
sum (ptr k)
35(No Transcript)
36Offset Calculation for 2-D arrays
Offset
- Offset for sij
- offset i scols j
376.3 Pointers in Function References
- Function references in C,
- Call-by-value references
- One exception Arrays as function parameters
- Using pointers as function parameters.
- implements call-by-address references
- allows to modify the values by statements within
a called function
38Call-by-value references
main () int x 5, y -2 void switch( int
a, int b) switch ( x, y )
void switch (int a, int y) int hold hold
a a b b hold return
39The values have been switched in the formal
parameters, but these values are not transferred
back to the actual parameters!!!
40Call-by-address references
main () int x 5, y -2 void switch2( int
x, int y) switch2 ( x, y )
void switch2 (int a, int b) int
hold hold a a b b
hold return
41In the beginning of the function
a
5
x
b
-2
y
After the function executed
a
-2
x
b
5
y
426.4 Problem Solving Applied Seismic Event
Detection
- Seismometer
- Special sensor to collect earth motion
information - Seismic Events
- Identify possible earthquakes
- Using a power ratio
431. PROBLEM STATEMENT
- Determine the locations of possible seismic
events using a set of seismometer measurements
from a data file.
2. INPUT/OUTPUT DESCRIPTION
443. HAND EXAMPLE
- Suppose that a data file contains
-
- 11 0.01
- 1 2 1 1 1 5 4 2 1 1 1
- This Data file means
-
- Number of points 11
- Time interval 0.01
- eleven values that correspond to a sequence of
values x0, x1, ...x10
45- We assume
- Short-time power measurement 2 samples
- Long-time power measurement 5 samples
Point x4 Short-time power (11)/2
1 Long-time power (11141)/5 1.6 Ratio
1/1.6 0.63
46Point x5 Short-time power (251)/2
13 Long-time power (251114)/5 6.4 Ratio
13/6.4 2.03
Point x6 Short-time power (1625)/2
20.5 Long-time power (1625111)/5
8.8 Ratio 20.5/8.8 2.33
47Point x7 Short-time power (416)/2
10 Long-time power (4162511)/5
9.4 Ratio 10/9.4 1.06
Point x8 Short-time power (14)/2
2.5 Long-time power (1416251)/5
9.4 Ratio 2.5/9.4 0.27
48Point x9 Short-time power (11)/2
1 Long-time power (1141625)/5 9.4 Ratio
1/9.4 0.11
Point x10 Short-time power (11)/2
1 Long-time power (111416)/5 4.6 Ratio
1/4.6 0.22
49Setting the threshold to 1.5, Seismic events
occur at points x5, x6 The time interval between
points is 0.01 second, Seismic events are 0.05
and 0.06 second
504. ALGORITHM DEVELOPMENT
- Decomposition Outline
- Read seismic data from the data file and read
numbers of measurement for power from the
keyboard. - Compute power ratios and print possible seismic
event times.
51- Refinement in Pseudocode
- main set threshold to 1.5
- read npts and time-interval
- read the values into sensor array
- read short-window, long-window from keyboard
- set k to long-window - 1
- while k lt npts - 1
- set short-power to power(sensor,
short-window, k) - set long-power to power(sensor, long-window,
k) - set ratio to short-power/long-power
- if ratio gt threshold
- print ktime-interval
- increment k by 1
52- power(x, length, n)
- set xsquare to zero
- set k to n
- while k gt n-length1
- add xkxk to xsquare
- k--
- return xsquare/length
53Coding
- /------------------------------------------------
--------/ - / Program chapter6_4
/ - /
/ - / This program reads a seisic data file and then
/ - / estimates the times of possible seismic
events. / - include ltstdio.hgt
- include ltstdlib.hgt
- define FILENAME "seismic.dat"
- define MAX_SIZE 1000
- define THRESHOLD 1.5
- main()
- / Declare variables and function prototypes.
/ - int k, npts, short_window, long_window
- double sensorMAX_SIZE, time_incr,
short_power, long_power, ratio - FILE file_ptr double power_w(double x, int
length, int n)
54 / Read data file. / file_ptr
fopen(FILENAME,"r") fscanf(file_ptr,"i
lf",npts,time_incr) if (npts gt MAX_SIZE)
printf("Data file too large for array.
\n") return EXIT_FAILURE else
/ Read data into an array. / for (k0
kltnpts-1 k) fscanf(file_ptr,"lf",sensor
k) / Read window sizes from the
keyboard. / printf("Enter number of points
for short-window \n") scanf("i",short_windo
w) printf("Enter number of points for
long-window \n") scanf("i",long_window)
55 / Compute power ratios and search for events.
/ for (klong_window-1 kltnpts-1 k)
short_power power_w(sensor,short_window,k)
long_power power_w(sensor,long_window,k)
ratio short_power/long_power if (ratio
gt THRESHOLD) printf("Possible event at f
seconds \n", time_incrk) /
Close file and exit program. /
fclose(file_ptr) return EXIT_SUCCESS
56/------------------------------------------------
-----------/ / This function computes the
average power in a / / specified window of a
double array. / double power_w(double x, int
length, int n) / Declare and initialize
variables. / int k double xsquare0
/ Compute sum of values squared in the array
x. / for (kn kgtn-length1 k--)
xsquare xkxk / Return
the average squared value. / return
xsquare/length /-----------------------------
------------------------------/
575. TESTING
- Enter number of points for short-window
- 2
- Enter number of points for long-window
- 5
- Possible event at 0.050000 seconds
- Possible event at 0.060000 seconds
586.5 Dynamic Memory Allocation
- If we do not know the exact size of the array
? Specify a maximum size in array definition
OR
? Allocate memory when a program is executed
59- Dynamic memory allocation is specified using
these functions - void malloc ( size_t m )
- memory allocation
- reserve a group of contiguous memory locations
- void calloc (size_t n, size_t size )
-
- cleared allocation
- in addition to malloc, initialize the memory
locations to a binary zero
60- sizeof operator
- get a size of a specific data type in byte
- computes an unsigned integer (size_t type) value
- Ex)
- sizeof ( int )
- sizeof ( float )
- sizeof ( double )
- num_pts 200
- void malloc ( num_pts sizeof ( int ) )
- num_pts 200
- void calloc ( num_pts, sizeof (int ) )
-
61void pointer
void malloc ( size_t m ) void calloc (
size_t n, size_t size )
- Both functions return a value to a pointer
- If the memory is available,
- ? return the address of the memory.
- If the allocation cannot be made,
- ? return NULL value.
62- / declare variables. /
- int npts 500
- double x
-
- / Dynamically allocate memory /
- x (double )malloc(ntps sizeof (double) )
/ declare variables. / int npts
500 double x / Dynamically allocate memory
/ x (double )calloc(ntps, sizeof (double) )
63- To be sure that the memory was allocated,
- if ( x NULL )
-
- printf(Memory requested not available. \n )
- return EXIT_FAILURE
64- To release allocated memory
- void free (void ptr)
- you should free dynamically allocated memory when
it is no longer needed. - ex)
- / release memory allocated to array x /
- free (x)
65- To change the size of memory requested by malloc
or calloc - void realloc ( void ptr, size_t size)
66- With the use of dynamic memory allocations and
releases - A program can be designed with a minimal amount
of memory reserved. - On system that running multiple programs, it
allows more programs to run simultaneously.
67Determine the maximum amount of contiguous memory
68Results
696.6 A Quicksort Algorithm
- Divide-and-Conquer Algorithm
- Select a pivot value from the values
- Separate the values into two groups
- the values in one group are smaller than the
pivot value - the values in the other group are greater than or
equal to the pivot value - Repeat this procedure recursively for the groups
70x
Separate
lt x
x
x
sort recursively by quicksort
sort recursively by quicksort
71(No Transcript)
72- void quicksort ( int x, int n)
-
- int break_pt
- int separate( int x, int n)
- void switch2(int a, int b)
-
- if (ngt1)
-
- break_pt separate( x, n )
- quicksort ( x, break_pt )
- quicksort ( x break_pt1, n-break_pt -1 )
-
73How Separate works
?
x
unknown
split point
?
x
lt x
x
split point
unknown
x
x
lt x
ltx
Interchange
split point
x
lt x
x
split point
74int separate(int y , int m) int k, pivot,
split 0 int switch2 ( int a, int b
) pivot y0 for ( k1 k lt m
k) if ( yk lt pivot ) split
switch2( yk, ysplit
) switch2( y0, ysplit ) return
split
75The keys
20
3
28
12
1
29
11
14
25
The first execution of Split
20
3
28
12
swap in place
20
3
12
28
1
20
3
12
1
28
29
11
20
3
12
1
11
29
28
14
7620
3
12
1
11
14
28
29
25
14
3
12
1
11
20
28
29
25