1 / 42

CS154 Data Structure in C

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

tuyen
Download Presentation

CS154 Data Structure in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS154 Data Structure in C Chapter 2 Dynamic variables: Pointers Tutor: Angie Hui

  2. Objective • Dynamic variables • Pointers • Allocating and freeing dynamic variables • Static variables • Static variables Vs Dynamic variables

  3. Static variable • Definition: • Size is fixed during programming running • E.g. array

  4. 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

  5. Dynamic variable • Definition: • Size can vary during programming running • E.g. pointer, linked list

  6. 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;

  7. 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

  8. 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;

  9. 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;

  10. 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 )

  11. 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.

  12. 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

  13. 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);

  14. 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

  15. 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;

  16. Dynamic variable - pointer

  17. Answer (i) 15 (ii) 6 (iii) FE01 (iv) 18 (v) 6 (vi) FE01 (vii) 18 (viii) 28 (ix) 18

  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);

  19. 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);

  20. 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

  21. 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

  22. 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]);

  23. 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)

  24. 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) );

  25. 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

  26. malloc Vs calloc • calloc - memory allocated is automatically initialized to 0 • malloc - memory allocated is not initialized. It starts with garbage values

  27. malloc – Exercise 1 • Ex1 How to allocate 5 long int spaces to lptr, using malloc long *lptr; Answer lptr = (long*)malloc(5*sizeof(long));

  28. malloc – Exercise 2 • struct student { char name[20]; char studID[6]; }; struct student *ptr;

  29. 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”);

  30. 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!

  31. Release of memory • free - char *ch; ch = (char*)malloc( 3*sizeof(char) ); ………. free(ch);

  32. Heap • It is a part of the memory which is reserved for dynamic variables.

  33. 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!!”);

  34. 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

  35. 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.

  36. 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.

  37. 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.

  38. 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.

  39. 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;

  40. Changing address of a pointer • Method 4: char a, b, *ptr1, *ptr2; ptr1 = &a; ptr2 = ptr1;

  41. Static Vs Dynamic variable

  42. ~ END ~

More Related