Title: Arrays and Pointers
1????? ??? ??
- Chapter 9
- Arrays and Pointers
2Contents
- One-Dimensional Arrays
- Initialization
- Subscripting
- An Example Counting Each Letter Separately
- Two-Dimensional Arrays
- Multidimensional Arrays
- Review of Pointers
- The Relationship between Arrays and Pointers
- Pointer Arithmetic and Element Size
- Combining the and Operators
3Contents
- Passing Arrays to Functions
- Passing Pointers to Functions
- Bubble Sort
- Dynamic Memory Allocation
- calloc() and malloc()
4One-Dimensional Arrays
- Array ?? ??? ??? ???? ?? ?? type? ?? ?? ??? ?
?? ?? data type. - Syntax
- ??? ??? ??? ??? ?? ??.
- ?? ??? ??? ?? 0 ?? ????.
- ?? ??? ??? grade0, grade1, , grade49? ??.
Ex int grade0, grade1, grade2,, grade49
?? ?? ????? ??? ??? ?? ?
element-type array_namesize Ex int grade50
size of Array
data type
variable Name
5One-Dimensional Arrays
Ex int digit10 0 Ex define MAX 100 double studentMAX Ex float exp13 int a10a2 8 exp10 exp11 exp23 100 expa2 100
10?? int? ??? 0?? ?????.
100? MAX? ??
double student100 ? ??.
a2? 8? ?? exp11? ?? exp10? ??
exp5? 100? ?? exp8? 100? ??
6One-Dimensional Arrays
Ex / Fill and Print an Array / include ltstdio.hgt define N 5 int main(void) int aN /allocate space for a0 to a4 / int i, sum 0 for ( i 0 i lt N i) / fill the array / ai 7 i i for ( i 0 i lt N i) / print the array / printf(ad d ,i, ai ) for ( i 0 i lt N i) / sum the elements / sum ai printf(\nsum d\n, sum) / print the sum / return 0
a0 7 a1 8 a2 11 a3 16 a4
23 sum 65
7One-Dimensional Arrays
????? ???? ?? address number
1000 1004 1008 1012 1016
7 8 11 16 23
0 1 2 3 4
? ??? ? array element? ???? ?? ??
? ???? array? index
8Initialization
- ???
- ??? array? ???? ???? ?.
- ???? ?? ??? ??? ?? ?
Ex float x7 -1.1, 0.2, 33.0, 4.4, 5.05, 0.0, 7.7
x0 -1.1, x1 0.2,, x6 7.7 ? ?????.
Ex int a100 0
a0 0, a1 0, a99 0?? ?? ???? ?? 0??
??? ??.
9Initialization
- ??? ??? ???? ?? ?
- ?? ????? ?? ?
Ex int a 2, 3, 5, 7 int a4 2, 3, 5, 7
???? ?? ??? ??? ??. ?? ? ??? ??.
Ex char s abc char s a, b, c, \0
?? ? array? ?? ?? ???.
.
10Subscripting
int i, asize
??? aexpr? ??? ? ??. ???? expr? a?
??(Subscript) ?? ??(index)?? ???.
Ex int a5 1, 2, 3, 4, 5 printf(d, a5 )
a0a4 ?? ???? a5? ???? ?? ??? ?? ????. ???
??? ??!
.
11cnt_abc Program
Ex / Count each uppercase letter separately / include ltstdio.hgt include ltctype.hgt int main(void) int c, i, letter26 for ( i 0 i lt 26 i) letteri 0 while(( c getchar() ) ! EOF) ctoupper(c) if(isalph(c))letterc A for ( i 0 i lt 26 i) if ( i 6 0 ) printf(\n) printf(4c3d, A i, letteri) / end of for / printf(\n\n) return 0
C Programming?? ???? ??? ??? ?? ????.
A 1 B 0 C 1 D 0 E 0 F 0 G 2
H 0 I 1 J 0 K 0 L 0 M 2 N 1
O 1 P 1 Q 0 R 2 S 0 T 0 U 0
V 0 W 0 X 0 Y 0 Z 0
12Dissection of the cnt_abc Program
int main(void) while (( c getchar( ) ) ! EOF) ctoupper(c) if( isalph(c) ) letterc A for ( i 0 i lt 26 i) if ( i 6 0 ) printf(\n) printf(4c3d, A i, letteri) / end of for / printf(\n\n) return 0
??? ????? getchar()? ?????. ???? ???? ???.
??? ??? ???? ??? ??? ????.
13Example of Array
- Reverse a series of numbers
Ex include ltstdio.hgt define N 10 main() int aN, i printf(Enter d numbers , N) for( i 0 i lt N i ) scanf(d, ai ) printf(in reverse order ) for ( i N 1 i gt 0 i-- ) printf( d, ai ) printf(\n) return 0
??? ??? ?? ??? ???? ??
10?? ??? ??? ?? ???? ??
Enter 10 numbers 1 2 3 4 5 6 7 8 9 10 In
reverse order 10 9 8 7 6 5 4 3 2 1
14Example of Array
- Check a Number for Repeated Digits
Ex / ???? ??? ??? ???? ???? ???? / include ltstdio.hgt define TRUE 10 define FALSE 0 typedef int Bool main() Bool digit_seen10 0 int digit long int n printf(Enter a number ) scanf(ld, n ) / continue /
int type? Bool?? ???? typedef? ??
?? ???? ???? ??? digit_seen ? boolean??? ??
???? ?? Bool?? ???? ???? ??.
15Example of Array
- Check a Number for Repeated Digits
while ( n gt 0 ) digit n 10 if (digit_seendigit) break digit_seendigit TRUE n / 10 if ( n gt 0 ) printf(Repeated digit\n\n) else printf(No repeated digit\n\n) return 0
digit_seen? index? 09?? ??. ?? ? array? ???? ??
0(false)??. n? ??? ?? ??? ??? ?? ??? digit_seen?
index? ??, ?? array? ?? 1(TRUE)? ????. True?
???? ?? ?? index??? ?? ????? ?? ??? ???? ?? ???.
?? while??? digit_seendigit ? true(1)? ?? ??
??, break??? ?? n/10? ???? ?? loop? ?? ??? ??.
?? n ? ?? 0?? ?? ??? ??? ??? ???? ????.
Enter a number 28212 ? Repeated digit
16Two-Dimensional Arrays
- 2?? ?? Syntax
- int b27? ????
data_type variable_name number number
Array dimensions Array dimensions
Declarations of arrays Remarks
int a100 a one-demensional array
int b27 a two-demensional array
int c532 a three-demensional array
col row 0 1 2 3 4 5 6
0 00 01 02 03 04 05 06
1 10 11 12 13 14 15 16
17Two-Dimensional Arrays
- ?????? elements
- ??? ??? ????? ? ??? ?? ?????? ??? ??? ???? ????
????? ??? ??. ??? ??? ??? ???? ??? ???? ???. - row0 row1
row2
Ex int a35
col1 col2 col3 col4 col5
row 1 a00 a01 a02 a03 a04
row 2 a10 a11 a12 a13 a14
row 3 a20 a21 a22 a23 a24
a00 a01 a04 a10 . a14 a20 a24
18Two-Dimensional Arrays
Ex include ltstdio.hgt define M 3 / number of rows / define N 4 / number of columns / int main(void) int aMN, i, j, sum 0 for ( i 0 i lt M i ) for ( j 0 j lt N j ) aij i j for ( i 0 i lt M i ) for ( j 0 j lt N j ) printf(add d , i, j, aij ) printf(\n) for ( i 0 i lt M i ) for ( j 0 j lt N j ) sum aij printf(\nsum d\n\n, sum) return 0
a00 0 a01 1 a02 2 a03
3 a10 1 a11 2 a12 3
a13 4 a20 2 a21 3 a22 4
a23 5 sum 30
19Two-Dimensional Arrays
- 2?? ?? element? access?? ?? ?? ??
- a i ? a? i?? ?
- a i j ? ??? i?? ?? j?? ?? ??
- ?? ?? a? a0? ??.
Expressions equivalent to a i j
( a i j )
( ( a i ) ) j
( ( ( a i ) ) j )
( a00 5 i j )
20Two-Dimensional Arrays
Ex int a23, p p a00 ? p a0 / a0 a00? ???? address / p 1 ? a01 ? a0 1 p 2 ? a02 ? a0 2 p 3 ? a10 ? a0 3 ? a1 0 p 4 ? a11 ? a0 4 ? a1 1 p 5 ? a12 ? a0 5 ? a1 2
21Two-Dimensional Arrays
int a23, p a00 , i , j Ex for( i 0 i lt 2 i) for( j0 j lt 3 j) aij 0 Ex for( i 0 i lt 2 i) for( p ai p lt ai3 p) p 0 Ex for( p a00 p lt a12 p) p 0
0
1
2
00 0
01 0
02 0
0
10 0
11 0
12 0
1
??? element? address lt a23 ?? ? ?? ??
22Multidimensional Arrays
- 3?? ?? Syntax
- ??a? ??? ??? ??
i j
data_type variable_name number number number
Ex int sum( int a 9 2 ) int i, j, k, aum 0 for ( i 0 i lt 7 i ) for ( j 0 j lt 9 j ) for ( k 0 k lt 2 k ) sum a i j k return sum
0
1
k
23Multidimensional Arrays
i
Ex int a 2 2 3 1,1,0, 2,0,0 , 3,0,0, 4,4,0
k
k
j
j
k
k
Ex int a 2 3 1, 1, 2 , 3, 4, 4
Ex int a 2 2 3 0
?? ???? 0?? ??? ??.
24 Review of Pointers
- pointer operator
- address of operator
- gets the address of the variable
-
- indirect operator
- gets the value at the address
Pointer?? p? ?? ?? rate? ??? ??
p rate
p? point?? ?, ? rate? 10? assign
p 10
25 Review of Pointers
Ex int x, p x 10 p x
Pointer?? p? x ??? assign, ? ? x? ?? 10
??
int x, p x x 10
p?point?? ?? ?? print ? x? ?? ?? x ??? point??
?? ?? ?
Ex printf(d, p)
??
printf(d, x) printf(d, x)
26 Review of Pointers
Ex int x, y, p1, p2 x -10 y 100 p1 x p2 y printf(dd\n, p1, x) p1 17 p2 p1 printf(dd\n, x, y) printf(uu\n, x, p1)
x
y
-10
100
x
y
p1
p2
x
y
p
p
-10 -10 17 17 12450521245052(address?)
27The Relationship between Arrays and Pointers
- Pointer
- ?? ?? ??? memory ??? ??? ?? data item
- memory address? ??? ?? ??
Ex define N 100 int aN, p
?? ???? memory byte number? ?? 300,304,308,,696
? ?????? ??, a0, a1,,a99 ? ?? ??? 300 ? ??.
?? ??
p a p a0
p? 300?? ??? ??. pointer ?? ??? ????.
p a 1 p a1
?? ??
p? 304?? ??? ??. ? ??? ??.
28The Relationship between Arrays and Pointers
- ??? ?? ??? code 1
- ??? ?? ??? code 2
Ex sum 0 for ( p a p lt aN p ) sum p
p a0
p lt aN-1
p? a0, p i? ai? ????
?? ??
Ex sum 0 for ( i 0 i lt N i ) sum ( a i )
(a i) ? ai? ????, (p i)? pi? ????.
29The Relationship between Arrays and Pointers
- ??? ?? ??? code 3
- ?? ?? ?
Ex p a sum 0 for( i 0 i lt N i ) sum p i
Ex a? ?? ????? ??? ?? ?, a p ( wrong ) a ( wrong ) a 2 ( wrong )
??? ?? ??? ?? X ?? ??? ?? ??? ??
O ????? ?? X ????? ?? X
a? ??? ?? ?? ??.
30The Relationship between Arrays and Pointers
sum 11
p
Ex define N 10 main() int aN11,34,82,7,64,98,47, 18,79,20 int sum, p sum 0 for ( p a0 p lt aN p ) sum p
11 34 82 7 64 98 47 18 79 20
0 1 2 3 4 5 6 7 8 9
a
sum 45
p
a
11 34 82 7 64 98 47 18 79 20
0 1 2 3 4 5 6 7 8 9
sum 127
p
a
11 34 82 7 64 98 47 18 79 20
0 1 2 3 4 5 6 7 8 9
for( p a p lt a N p ) ? ??? ????.
31Pointer Arithmetic and Element Size
- ??? ??
- ?? p? ????? p 1? ? type? ?? ??? ????? access? ?
??? ??? ????. - ???? ?? ?? ??? ????.
- ???? ?? ?? ??? ????.
- ? ?? ???? ?? ??? ????.
p
Ex int a10, p p a0
0 1 2 3 4 5 6 7 8 9
a
p
Ex p 5
5
0 1 2 3 4 5 6 7 8 9
a
32Pointer Arithmetic and Element Size
- Adding an Integer to a Pointer
p
0 1 2 3 4 5 6 7 8 9
a
Ex p a2
p
q
3
0 1 2 3 4 5 6 7 8 9
a
q p 3
p
q
6
0 1 2 3 4 5 6 7 8 9
a
p 6
p p 6
33Pointer Arithmetic and Element Size
- Subtracting an Integer from a Pointer
p
Ex p a8
0 1 2 3 4 5 6 7 8 9
a
p
q
-3
0 1 2 3 4 5 6 7 8 9
a
q p - 3
p
q
-6
0 1 2 3 4 5 6 7 8 9
a
p - 6
p p - 6
34Pointer Arithmetic and Element Size
Ex p a5 q a1 i p q / i is 4 / i q p / i is -4 /
i 5-1
i 1-5
q
p
0 1 2 3 4 5 6 7 8 9
a
35Pointer Arithmetic and Element Size
- Comparing Pointers
- ????? (relational operators) lt, lt, gt,gt ?? ??.
- ?????(equality operators) , ! ?? ??.
Ex p a5 q a1 p lt q / result is 0 / p gt q / result is 1 /
5 lt 1 ??
5 gt 1 ?
36Pointer Arithmetic and Element Size
p
q
Ex int a 5,15,25,43,12,1,7,89,32,11 int p a1, q a5 1. (p 3) ? 2. (q - 2) ? 3. q - p ? 4. if ( p gt q ) ? 5. if ( p gt q )?
12
43
5-1 ? 4
1. 12 2. 43 3. 4 4. false 5. true
1 gt 5 false
15 gt 1 true
37Pointer Arithmetic and Element Size
p
q
Ex include ltstdio.hgt int main(void) double a2, p, q p a0 / points at base of array / q p 1 / equivalent to q a1 / printf(d\n, q p ) printf(d\n, (int) q (int) p ) printf(d\n, sizeof(double) ) return 0
a0
a1
1 0 ? 1
8 ??? ??
8 ???
1 8 8
38Combining the and Operators
- Combining the and Operators
??? p?? ?? , ??? (p1)? ???? ?? value
p ? (p)
p ? (p) ? (p)
p? ???? ?? value? increment ?? ??
point?? ?? ??? ? p? ?? increment
p? ??? ?, ??? p ? ?????.
Expression Meaning
p or (p) ?? p? ?? ??? ??, p? ?????.
(p) ?? p? ?? ??? ??, p? ?????.
p or (p) p? ?? ????? p?? ????.
p or (p) P? ???? ?? ?? ???? ? ??
39Combining the and Operators
a a
b b
3
5
Ex int a 3, b 5 , p p a printf(d d d\n, a, b, (p))
a
3 5 3
p p
a a
b b
10
5
Ex int a 10, b 5 , p p b printf(d d d\n, b, p, (p))
b
p p
5 5 6
40Combining the and Operators
a0 1000 20
a1 1004 7
a2 1008 -9
Ex int a320, 7, -9, p, i p a1
a1 ? ??
? i p printf(d d, i, p) / i p p p ? 1008?? /
7 -9
? i p printf (d d, i, p) / p i p p ? 1008?? /
-9 -9
??
? i p printf (d d, i, p) / (p) i 8 p? 1004?? /
8 8
? i (p) printf (d d, i, p) / i p (p) p? 1004?? /
7 8
41Combining the and Operators
top_ptr
Ex / ????? ?? / void push( int i ) if ( is_full() ) stack_overflow() else top_ptr i int pop(void) if ( is_empty() ) stack_underflow() else return --top_ptr
stack? ? ? ???? ??? ? push? ? ??
top_ptr ? top_ptr ??
top_ptr ? ???? ?? i ? ?? top_ptr ? ??
stack? ? ???? ??? ? pop? ? ??
--top_ptr ? top_ptr ??
42Passing Arrays to Functions
- ????? ??
- ??? ??? ??? ?, ??? ??? call by value? ??.
- ?? ??? ??? call by value? ???? ?? call by
reference? ????.
Ex int sum( int a, int n) int i, s 0 for ( i 0 i lt n i) s a i return s
int a ? int a? ??.
Various ways that sum() might be called Various ways that sum() might be called
Invocation What gets computes and returned
sum(v, 100) v0 v1 v99
sum(v, 88) v0 v1 v87
sum(v7, k 7 ) v7 v8 vk -1
sum(v 7, 2 k ) v7 v8 v2 k 6
43Passing Arrays to Functions
Ex include ltstdio.hgt int sigma (int n ) int r for ( r 0 n gt 0 n-- ) r n return r void main(void) int a printf(Input number ) scanf(d, a) printf(Sigma 1 to d is d.\n, a, sigma(a) )
7? ??????? 7654321 ? 28
44Passing Arrays to Functions
Ex include ltstdio.hgt void swap(int p, int q) main() int i 3, j 5 swap(i, j) printf(d d\n, i, j) return 0 void swap(int p, int q ) int tmp tmp p p q q tmp
i? j? ?? ??? p?q? ???? swap()?? ???? i?j? ?? ??
??????, ???? ?? p?q? ?? ??? ???. ???? ????
swap?? ???? swap? ?? ??? ????.
3 5
45Passing Pointers to Functions
- Call by Reference (??? ?? ??)
Ex include ltstdio.hgt void swap(int , int ) main() int i 3, j 5 swap(i, j) printf(d d\n, i, j) return 0 void swap(int p, int q ) int tmp tmp p p q q tmp
swap()??? i? j? ?? ?? p? q? ????.
swap()?? ???? ??? ???? ?? ?? ??? ??? ?? ??? ???
??? ??? ??.
5 3
46Passing Pointers to Functions
- ???? argument? ?? ?? ??
- ? ??? ??? ??
Ex void decompose (float, int , float ) / prototype / main() int i, float f decompose(3.14159, i, f) void decompose (float x, int int_part, float frac_part) int_part (int)x frac_part x - int_part
x 3.14159
int_part
? i
frac_part
? f
47Passing Pointers to Functions
void decompose (float x, int int_part, float frac_part) int_part (int)x frac_part x - int_part
float? ?? x? ?? int??? casting. int_part?
point?? ??? 3? ??
x 3.14159
int_part
3 i
frac_part
? f
48Passing Pointers to Functions
void decompose (float x, int int_part, float frac_part) int_part (int)x frac_part x - int_part
x?? ????? ? ??? ?? frac_part? point?? ??? ??
x 3.14159
int_part
3 i
frac_part
.14159 f
49Passing Pointers to Functions
- ???? argument? ?? ?? ??? ???
Ex void decompose (float, int , float ) / prototype / main() int i, float f decompose(3.14159, i, f)
???? ???
i?? f?? address? ???? int_part, frac_part? ????
?? ? ? ?? memory location? ??? write. ?
Unpredictable error
50Problems
pointer? ?? ?? ?? ?? ????? ? ??? ???? ?? ?? ??? ???? ??? ????. ????? ?? ??? ??? ? ????? ??? ??? ????? ?? ????? ??? ????.
p3
p2
a1
a1
p1
p2
main( ) int a5 5, 4, 3, 2, 1, p1 , p2 , p3 p1 a 1 p2 p1 p3 p2 / ? / printf(d d d d\n, a2, p1, p2, p3) (p2) p3 p1 / ? / printf(d d d d\n, a2, p1, p2, p3) (a 2) p1 p2 p3 / ? / printf(d d d d\n, a2, p1, p2, p3)
5
4
3
2
1
a0
a1
a2
a3
a4
3 4 4 3000 3 5 5 3000 15 5 5 3000
51Problems
?? ??? ?? ??? ?? ??? ????. ?? ?? ?? ??? ??? ?? ?? ??? ????.
??? ???? ?? ??????(value) ??????(value) ??????(value)
??? ???? ?? ??? ??? ???
a1 1000
p1 2000
p2 3000
p3 4000
52?? ??
pointer? ??
p2
p1
p2 p1 p3 p2 / ? / (p2) p3 p2 / ? / (a 2) p1 p2 p3 / ? /
p3
5 4 3 2 1
0 1 2 3 4
p2
p1
p3
5 5 3 2 1
0 1 2 3 4
p2
p1
p3
5 5 15 2 1
0 1 2 3 4
53?? ??
???
??? ???? ?? ??????(value) ??????(value) ??????(value)
??? ???? ?? ??? ??? ???
a1 1000 4 5 5
p1 2000 1000 1000 1000
p2 3000 1000 1000 1000
p3 4000 3000 3000 3000
54Bubble Sort
- Bubble Sort
- ??? ?? ???? ???? ???? ??? ?? ??? ?????? ????? ??
??? ????. - ?? ?? (??? pass)
- S O R T I N G ? S? O? ???? ????.
- O S R T I N G ? S? R? ???? ????.
- O R S T I N G ? S? T? ???? ????.
- O R S T I N G ? T? I? ???? ????.
- O R S I T N G ? T? N? ???? ????.
- O R S I N T G ? T? G? ???? ????.
- O R S I N G T ? ??? T? ?? ?? ??.
3 2 1 5 4
2 3 1 5 4
2 1 3 5 4
2 1 3 5 4
2 1 3 4 5
55Bubble Sort
include ltstdio.hgt define N 10 main() int a102, 4, 5, 3, 6, 1, 7, 8, 12, 54 int i, j, tmp for (i 0 i lt N i) printf("d ", ai)
? ??? ?? ????
56Bubble Sort
for ( i 0 i lt N-1 i ) for(j 0 j lt N-i-1 j) if( aj gt aj1 ) tmp aj aj aj1 aj1 tmp
57Bubble Sort
- int a 7, 3, 66, 3, -5, 22, -77, 2 ?? ??.
- bubble( a, 8 ) ? ??? ???? ??? ?? ???? ????.
Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass Elements of array a after each pass
Unordered data 7 3 66 3 -5 22 -77 2
First pass -77 7 3 66 3 -5 22 2
Second pass -77 -5 7 3 66 3 2 22
Third pass -77 -5 2 7 3 66 3 22
Fourth pass -77 -5 2 3 7 3 66 22
Fifth pass -77 -5 2 3 3 7 22 66
Sixth pass -77 -5 2 3 3 7 22 66
Seventh pass -77 -5 2 3 3 7 22 66
58Dynamic Memory Allocation
- ?? ??? ??? ???
- ????? ???? ??? ??? ??? ?? ????? ??? ?? ??? ????
???? ?? ????. - ??? ?? ??? ??? 3 x 3 ? ??? ???? ?? ? ? ??.
- ?? ??? ??? ?? ???? ??? ??? ??? ???? ??.
- ??? ????? ?? ?? ?? ??? ??? ????.
Ex main() double a33 det determinant(a) .. double determinant ( double a 3 ) ...
59Dynamic Memory Allocation
- ?? ??? ??
- program? ?? ? new storage? ???? process
- stdlib.h? ?? calloc()? malloc()? ??.
- calloc()? ??? ?? (contiguous allocation)? ??.
- malloc()? ??? ?? (memory allocation)? ??.
- Compiled C program - 4???? ??
- stack , return address of function, parameters,
local variables ?? - global variables ??
- program code ??
- heap(unallocated? free memory)
60Dynamic Memory Allocation
- calloc()
- ? ??? object_size ?????, ? ??? n?? ??? ?? ????
??? ?? ??? ????. - ? ??? 0?? ??? ??.
- ??? ???? ??? ????? ??? ????.
- ??? ???? ??? NULL? ????.
- calloc() Syntax
calloc ( n, object_size )
size of each object
number of objects
calloc()? size_t type? ??? argument? ??
Ex void calloc ( size_t, size_t )
61Dynamic Memory Allocation
- malloc()
- object_size? ???? ??? ??? ????
- ??? ???? ???.
- ??? ???? ??? ??? ??? ????.
- ??? ???? ??? NULL? ????.
- malloc() Syntax
malloc ( object_size )
size of each object
Ex void malloc ( size_t size )
malloc()? size_t type? ??? argument? ??
62Dynamic Memory Allocation
- free()
- ptr? ????? ??? ??? ????.
- ptr? NULL?? ? ??? ??? ??, ptr? NULL? ????
calloc(), malloc(), realloc()? ?? ??? ????? ??
free()? ?? ???? ???? ??. - free()? prototype? stdlib.h? ??.
- free() Syntax
void free(void ptr)
Ex p malloc() q malloc() free(p) p q
free? ???? ?? p? point?? ?? memory block? ????
???.
63Dynamic Memory Allocation
- Dangling Pointer
- free()? ?? ??? memory block? deallocate ? ?? ???
pointer? point?? memory block? ???? ??. - garbage
- calloc(), malloc()? ??? ??? block? free()? ?? ??
???? ????? ?? memory block? ?? ????? ??. ??? ? ?
?? ? memory block? garbage? ?????? ??.
Ex char p malloc(4) free(p) strcpy ( p, abc ) /WRONG /
free? ?? ??? pointer? ???? ?? error? ????.
64Dynamic Memory Allocation
Ex include ltstdio.hgt include ltstdlib.hgt int main(void) int a, i, n, sum 0 printf(\ns, An array will be created dynamically.\n\n Input an array size n followed by n integers ) scanf(d , n ) a calloc(n, sizeof(int) ) / get space for n ints / for ( i 0 i lt n i ) scanf(d, a i ) / ( Continue ) /
calloc(), malloc(), free() ??? ???? ?? library ??
a malloc(n sizeof(int) ) ? ??.
65Dynamic Memory Allocation
/ ( Continued. ) / for ( i 0 i lt n i ) sum a i free(a) /free the space / printf(\ns7d\ns7d\n\n, Number of elements , n, Sum of the elements , sum ) return 0
calloc()? ???? memory block? deallocation ??.
66???????