430 likes | 792 Views
CS154 Data Structure in C. Chapter 2 Dynamic variables: Pointers Tutor: Angie Hui. Objective. Dynamic variables Pointers Allocating and freeing dynamic variables Static variables Static variables Vs Dynamic variables. Static variable. Definition: Size is fixed during programming
E N D
CS154 Data Structure in C Chapter 2 Dynamic variables: Pointers Tutor: Angie Hui
Objective • Dynamic variables • Pointers • Allocating and freeing dynamic variables • Static variables • Static variables Vs Dynamic variables
Static variable • Definition: • Size is fixed during programming running • E.g. array
Static variable - Array • Disadvantage: int num[1000]; - 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
Dynamic variable • Definition: • Size can vary during programming running • E.g. pointer, linked list
Dynamic 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;
E.g1 int* a; int b = 5; a = &b; //a points to b *a = *a + 6; printf(“b=%d”, b); Result b=11 Dynamic variable - pointer
Dynamic 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;
Dynamic variable - pointer • E.g2 int *ptr, b=5, c=10; //assign the address of b to ptr ptr = &b; /* change the content in the memory location pointed by ptr, to 8 */ *ptr = 8;
Dynamic variable - pointer E.g3 int x=10, *ptr=&x; printf(“x=%d\n”, x); printf(“*ptr=%d\n”, *ptr); printf(“ptr=%p\n”, ptr); Result x=10 *ptr=10 ptr=FF02 ( Assume the address of x is FF02 )
Dynamic variable - pointer • printf(“*ptr=%d\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.
Dynamic variable - pointer • printf(“ptr=%p\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
Dynamic variable - pointer • printf(“ptr=%p\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(“ptr=%p\n”, ptr);
Direct 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
Dynamic variable - pointer Exercise 1: (Assume the addr. of a is FE01) Line 1: int a=15, b=6; Line 2: int *ptr; Line 3: ptr = &a; Line 4: *ptr = *ptr + 3; Line 5: b = *ptr + 10;
Answer (i) 15 (ii) 6 (iii) FE01 (iv) 18 (v) 6 (vi) FE01 (vii) 18 (viii) 28 (ix) 18
Dynamic variable - pointer Result x=70 *ptr=70 Exercise 2 long x=10, *ptr=&x; *ptr=*ptr*7; printf(“x=%d\n”, x); printf(“*ptr=%d”, *ptr);
Dynamic variable - pointer Result x=11 *ptr=11 x=12 Exercise 3 long x=10, *ptr=&x; *ptr++; printf(“x=%d\n”, x); printf(“*ptr=%d”, *ptr++); printf(“x=%d”, x);
Using pointer notation to refer to the item in an array int no[] = {11, 12, 13, 14, 15}; no : address of no[0] (i.e. &no[0]) no+1: address of no[1](i.e. &no[1]) no+3: address of no[3](i.e. &no[3]) *(no+2) = 13 *(no+1) = 12 *(no+4) = 15 … and so on
Array - Exercise int no[] = {11, 12, 13, 14, 15}; int i; for(i=0; i<5; i++) printf(“%d “, *(no+i)); Result 11 12 13 14 15
Array – Exercise (Con’t) for(i=0; i<5; i++) printf(“%d “, *(no+i)); is equivalent to for(i=0; i<5; i++) printf(“%d “, no[i]);
Dynamic 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)
Dynamic memory allocation E.g. To allocate 3 char spaces to ch char *ch; • malloc (memory allocation) e.g. ch = (char*)malloc( 3*sizeof(char) ); • calloc (contiguous allocation) e.g. ch = (char*)calloc( 3, sizeof(char) );
Dynamic 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
malloc Vs calloc • calloc - memory allocated is automatically initialized to 0 • malloc - memory allocated is not initialized. It starts with garbage values
malloc – Exercise 1 • Ex1 How to allocate 5 long int spaces to lptr, using malloc long *lptr; Answer lptr = (long*)malloc(5*sizeof(long));
malloc – Exercise 2 • struct student { char name[20]; char studID[6]; }; struct student *ptr;
malloc – Exercise 2 (Con’t) 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(10*sizeof(struct student)); Q2. strcpy(ptr[0]name, “Mary Wong”); Q3. strcpy(ptr[0]studID, “FT1111”);
Release of unused memory • It’s 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!
Release of memory • free - char *ch; ch = (char*)malloc( 3*sizeof(char) ); ………. free(ch);
Heap • It is a part of the memory which is reserved for dynamic variables.
NULL pointer • If a pointer equals toNULL, 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(3*sizeof(char))) == NULL) printf(“Not enough memory allocated to ch!!”);
Compare two pointers Case 1. Compare the reference - i.e.: check whether the 2 pointers point to the same location Case 2. Compare the contents
Case 1: Compare the reference e.g1. int *first, *second, a=10; 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.
Case 1: Compare the reference e.g2. int *first, *second, a=10; 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.
Case 1: Compare the reference e.g3. int *first, *second, a=10, b=10; 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.
Case 2: Compare the contents e.g1. int *first, *second, a=10, b=10; 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.
Changing address of a pointer • Method 1: float *f=(float*)malloc(sizeof(float)); • Method 2: int *no = NULL; • Method 3: char *a, b; a = &b;
Changing address of a pointer • Method 4: char a, b, *ptr1, *ptr2; ptr1 = &a; ptr2 = ptr1;