Title: CS154 Data Structure in C
1CS154 Data Structure in C
- Chapter 2
- Dynamic variables Pointers
- Tutor Angie Hui
2Objective
- Dynamic variables
- Pointers
- Allocating and freeing dynamic variables
- Static variables
- Static variables Vs Dynamic variables
3Static variable
- Definition
- Size is fixed during programming
- running
- E.g. array
4Static variable - Array
- Disadvantage
- int num1000
- - May overestimate the length of
- the array, which cause memory
- waste
- - May underestimate the length of
- the array, which result in
- insufficient memory to hold all the
- required data
5Dynamic variable
- Definition
- Size can vary during programming
- running
- E.g. pointer, linked list
6Dynamic variable - pointer
- Declare a pointer
- e.g1. char c
- e.g2. int no
- Define a pointer type using typedef
- e.g. typedef int intPtr
- intPtr ptr
-
7Dynamic variable - pointer
- E.g1
- int a
- int b 5
- a b //a points to b
- a a 6
- printf(bd, b)
-
8Dynamic variable - pointer
Explanation
- Effect of the below instructions
- int a
- int b 5,
Effect of the below instructions a b
//a points to b a a 6
9Dynamic variable - pointer
- E.g2 int ptr, b5, c10
-
- //assign the address of b to ptr
- ptr b
-
- / change the content in the memory
- location pointed by ptr, to 8 /
- ptr 8
10Dynamic variable - pointer
- E.g3
- int x10, ptrx
- printf(xd\n, x)
- printf(ptrd\n, ptr)
- printf(ptrp\n, ptr)
Result x10 ptr10 ptrFF02 ( Assume the
address of x is FF02 )
11Dynamic variable - pointer
- printf(ptrd\n, ptr)
- - ptr means the content of the
- memory location pointed by ptr
- - So this statement will print out the
- content of x since ptr is pointing
- to x.
12Dynamic variable - pointer
- printf(ptrp\n, ptr)
- - ptr means the address of the
- location pointed by ptr
- - So this statement will print out the
- address of x since ptr is pointing to
- x!
- - We use p if we want to print out
- an address
13Dynamic variable - pointer
- printf(ptrp\n, x)
- - This statement will print out the
- address of x
- - The above statement should print out
- the same value as the following one if
- ptr is pointing to x
- printf(ptrp\n, ptr)
14Direct Access Vs Indirect Access
- int x int b x
- Direct access
- e.g. x 12 //directly assign 12 to x
- Indirect access
- e.g. b 3 //Indirectly assign 3 to x
15Dynamic variable - pointer
- Exercise 1
- (Assume the addr. of a is FE01)
- Line 1 int a15, b6
- Line 2 int ptr
- Line 3 ptr a
- Line 4 ptr ptr 3
- Line 5 b ptr 10
16Dynamic variable - pointer
a b ptr ptr
Line 1 (i) (ii)
Line 3 (iii)
Line 4 (iv) (v) (vi) (vii)
Line 5 (viii) (ix)
17Answer
- (i) 15
- (ii) 6
- (iii) FE01
- (iv) 18
- (v) 6
- (vi) FE01
- (vii) 18
- (viii) 28
- (ix) 18
18Dynamic variable - pointer
- Exercise 2
- long x10, ptrx
- ptrptr7
- printf(xd\n, x)
- printf(ptrd, ptr)
Result x70 ptr70
19Dynamic variable - pointer
- Exercise 3
- long x10, ptrx
- ptr
- printf(xd\n, x)
- printf(ptrd, ptr)
- printf(xd, x)
Result x11 ptr11 x12
20Using pointer notation to refer to the item in an
array
- int no 11, 12, 13, 14, 15
- no address of no0 (i.e. no0)
- no1 address of no1(i.e. no1)
- no3 address of no3(i.e. no3)
- (no2) 13
- (no1) 12
- (no4) 15 and so on
21Array - Exercise
- int no 11, 12, 13, 14, 15
- int i
- for(i0 ilt5 i)
- printf(d , (noi))
Result 11 12 13 14 15
22Array Exercise (Cont)
- for(i0 ilt5 i)
- printf(d , (noi))
- is equivalent
to - for(i0 ilt5 i)
- printf(d , noi)
23Dynamic memory allocation
- Programmer can vary the size of dynamic variables
during program running - In C, programmer can use the following 2
functions to allocate memory space to any dynamic
variable - (i) malloc or
- (ii) calloc (Out of syllabus)
24Dynamic memory allocation
- E.g. To allocate 3 char spaces to ch
- char ch
- malloc (memory allocation)
- e.g. ch (char)malloc( 3sizeof(char) )
- calloc (contiguous allocation)
- e.g. ch (char)calloc( 3, sizeof(char) )
25Dynamic memory allocation
- If the computer system is unable to allocate the
necessary memory, NULL is returned - Otherwise, the starting address of the memory
block will be returned
26malloc Vs calloc
- calloc
- - memory allocated is automatically
- initialized to 0
- malloc
- - memory allocated is not initialized.
- It starts with garbage values
27malloc Exercise 1
- Ex1 How to allocate 5 long int spaces
- to lptr, using malloc
-
- long lptr
-
Answer lptr (long)malloc(5sizeof(long))
28malloc Exercise 2
- struct student
- char name20
- char studID6
-
- struct student ptr
-
29malloc Exercise 2 (Cont)
- Q1 How to allocate 10 student struct to ptr?
- Q2 How to assign Mary Wong as the
- name of the 1st student?
- Q3 How to assign FT1111 as the student
- ID of the 1st student
Answer Q1. ptr(struct student)malloc(10sizeof(s
truct student)) Q2. strcpy(ptr0?name, Mary
Wong) Q3. strcpy(ptr0?studID, FT1111)
30Release of unused memory
- Its better to free all the unused memory so as
to use the memory effectively!! - In C, programmer can use the function, free to
free all the unnecessary memory!
31Release of memory
- free
- - char ch
- ch (char)malloc( 3sizeof(char) )
- .
- free(ch)
32Heap
- It is a part of the memory which is reserved for
dynamic variables.
33NULL pointer
- If a pointer equals to NULL, that means it points
to nothing - Initially, we can set the pointer to NULL to
indicate the pointer is pointing to nothing at
the beginning - e.g. int no NULL
- To check whether the dynamic memory allocation is
successful or not ? - if((ch(char) malloc(3sizeof(char)))
NULL) - printf(Not enough memory allocated to
ch!!)
34Compare two pointers
- Case 1. Compare the reference
- - i.e. check whether the 2
- pointers point to the
same - location
-
- Case 2. Compare the contents
35Case 1 Compare the reference
- e.g1.
- int first, second, a10
- first a second a
- if(first second)
- printf(they point to the same location)
- else
- printf(they point to different
location)
Result they point to the same
location.
36Case 1 Compare the reference
- e.g2.
- int first, second, a10
- first a second first
- if(first second)
- printf(they point to the same location)
- else
- printf(they point to different
location)
Result they point to the same
location.
37Case 1 Compare the reference
- e.g3.
- int first, second, a10, b10
- first a second b
- if(first second)
- printf(they point to the same location)
- else
- printf(they point to different
location)
Result they point to the different
location.
38Case 2 Compare the contents
- e.g1.
- int first, second, a10, b10
- first a second b
- if( first second )
- printf(they have the same content)
- else
- printf(they have the different content)
Result they have the same content.
39Changing address of a pointer
- Method 1
- float f(float)malloc(sizeof(float))
- Method 2
- int no NULL
- Method 3
- char a, b
- a b
40Changing address of a pointer
- Method 4
- char a, b, ptr1, ptr2
- ptr1 a
- ptr2 ptr1
41Static Vs Dynamic variable
Static variable Dynamic variable
Size is fixed during program running Size can vary during program running
Cannot be created or destroyed during the program execution Can be created or destroyed during the program execution
42