280 likes | 485 Views
Lecture Contents. Arrays and Vectors : Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation. Functions and pointers. Array and Pointers. Pointer to pointer. Pointer to function. Common pitfalls.
E N D
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation. Functions and pointers. Array and Pointers. Pointer to pointer. Pointer to function. Common pitfalls. Some other C-complex data types. Examples.
Pointers and References Pointers and References: Address is an important property of variables. Pointer variable is a variable for containing address of the other variables. int *countPtr, count;
Pointers and References Pointers and References: A reference is an alias of other variables. It also contains the address of the aliased variables. e.g: int count; int& countRef=count; Difference between pointers and references: Pointers are dynamic and changeable. References are static and used just as a variable alias. Pointers have operations references do not. Pointers could be NULL, whereas References must contain variable addresses at all time.
Pointer Declaration Declarations and initialization: int count int *countPtr = &count; int& countAlias=count; int *undecided=NULL; Note: Reference must be initialized when being declared. Pointer could be initialized with NULL (pointing to nowhere). To get an address for initializing pointers, operator & is needed.
Pointer Operators Pointer Operators: &: to get the address of a variable. double q,*p=&q; Compared with reference: double q, &p=q; * = To get the value of the variables being pointed by pointers. *ptr=5; *q=8.95; cout<<*ptr; Compared with reference: q=5; cout<<q; cout<<p print out the address containing in p; Cout<<p print out the value of variable aliased by p if p is a reference.
Pointer Operators Pointer Operators: &: to get the address of a variable. double q,*p=&q; Compared with reference: double q, &p=q; * = To get the value of the variables being pointed by pointers. *ptr=5; *q=8.95; cout<<*ptr; Compared with reference: q=5; cout<<q; cout<<p print out the address containing in p; Cout<<p print out the value of variable aliased by p if p is a reference.
Pointer Expression Pointer expression and arithmetic (impossible with references): Since pointers contain values as addresses of variables (i.e. integer number), it is possible to use pointer value in arithmetic expressions. But some operands would be meaningless to use and strongly recommended not to use. Pointer expressions are very controversial as they has been claimed as the cause of many profound bugs --> pointer expressions is impossible in JAVA (where it uses only reference).
Pointer Expression Pointer expression and arithmetic (impossible with references): Since pointers contain values as addresses of variables (i.e. integer number), it is possible to use pointer value in arithmetic expressions. But some operands would be meaningless to use and strongly recommended not to use. Pointer expressions are very controversial as they has been claimed as the cause of many profound bugs --> pointer expressions is impossible in JAVA (where it uses only reference).
Pointer Expression Usual operators with pointers: ++, --, -, +=,-= The arithmetic operators is only meaningful if the pointers are now pointing to an array (series of elements). Ptr++ = point to the next element in an array. Ptr--=point to the previous element in the a. Ptr+2= point to the next next elements. Ptr1-Ptr2 = How many elements are there between the two pointers.
Constant Pointers Constant Pointers: const int *Ptr; //pointer to constant int *const Ptr; //constant pointer A constant pointer contain a constant address (of other variable) it is not possible to change the content of a constant pointer (i.e. to point it to other address). 4 different cases: Non-constant Pointer to non-constant data. Non-constant Pointer to constant data. Constant Pointer to non-constant data. Constant Pointer to constant data.
Memory Allocation Dynamic Memory Allocation: Variable could be created in the running time -> dynamic variable vs static variables created at compiling time. Memory need to be allocated in the run time for dynamic variable. Pointers are used to refer to dynamic variable through their addresses. Dynamic variables, dynamic data structures make program more powerful.
Memory Allocation Dynamic Memory Allocation: In C language: functions malloc() andcalloc() are used to allocate memory dynamically; free() is used to free the memory. In C++: new and delete Syntax: int *q, *intPtr= new int; double *P=new double(3.14); cin>>size; q=new int[size]; …
Memory Allocation Dynamic Memory Allocation: In C++: new and delete: If successful new return the address of the allocated memory (dynamic variable). If not successful (such as out of memory) then its return NULL. To check if a variable was correctly created examine the pointer. …… p= new int; if(p) //successful "or if (p!=NULL)" …. else //fail to create the dynamic variable …..
Memory Allocation Dynamic Memory Allocation: delete is used to free the memory for dynamic variable when it is no longer needed. delete p; Note: Always free dynamic variable and release memory when the variable is no longer needed, as the could be reallocated to other dynamic variables. After a variable is free the pointer is pointing nowhere (the variable cease to exist); an attempt to use the pointer after delete will cause bug (hang-up). Attempt to free NULL pointer will result in bug (hang-up). Attempt to free constant pointer will cause error.
Function and Pointer Functions and pointers: Passing argument with pointers:
Function and Pointer Functions and pointers: Returning a pointer: int *Max_Address() { int *ptr; ….. ptr=new int; ……. return ptr; }
Function and Pointer Functions and pointers: Returning a reference: int &Max_Address(int &m,int &n) { return (m>n ? m:n; }
Array and Pointer Array and Pointers: Pointer could be used to access array elements using pointer arithmetic. int a[10], *intPtr; ….. intPtr=a; //intPtr pointing to a[0] *(p+5)=4; //a[5]=4 intPtr=a[7]; //intPtr pointing to a[7] intPtr++; //intPtr pointing to a[8]
Array and Pointer Array and Pointers: Dynamic arrays: Pointers could be used to create dynamic arrays of elements. int *intPtr; int size; ….. cin>>size; intPtr=new int[size+10]; …. *(Ptr+3)=5; Ptr[3]=5; delete intPtr;
Array and Pointer Array and Pointers: Array of pointers: int *Ptr[5]; char *Ptr="Hello, World"; char *Ptr[4]={"Spring", "Summer", "Autumn", "Winter" }; Since an array could be represented by a pointer. An array of pointer is equivalent to pointer to pointer: int **Ptr[5]; char **Ptr[4]; … cout<<*Ptr[2];
Pointer to Functions Pointer to functions:
Common Pitfalls Some Common Pitfalls with Pointers: Fail to initialize pointer. Attempt to use uninitialized pointers. Attempt to use pointer to released memory area. Deleting NULL pointers. Confuse between constant pointer and pointer to constant. Misuse of pointer arithmetic. Forget to release memory of dynamic variables when it is no longer needed. (lose address -> wasted memory). Multiple release of same memory area.
Enumeration Type Some other Complex Data Types: 1. Enumeration: is an integral type of finite number of values defined by users. enum <type_name> {<enumeration list>}; enum Semester {Spring, Fall, Summer}; Semester s1,s2; …. s1=Summer; s2=Fall; … if (s1==Fall) cout<<"Fall Semester!";
Structures Some other Complex Data Types: 2. Structure: User-defined complex data type is a combination of other data type. struct <name> { <field declaration>; }; E.g: struct Student { int age; string name; boolean gender; }; struct Student s1, s2;
Structures Some other Complex Data Types: 2. Structure: User-defined complex data type is a combination Using typedef struct _Student { int age; string name; boolean gender; }; #typedef struct _Student Student; Student s1, s2;
Structure and Pointer Some other Complex Data Types: 2. Structure: User-defined complex data type is a combination Accessing the field data - dot operator: s1.age=23; cout<<s2.name; Pointer with structures: Student *s1,*s2; …. s1=new Student; s1->age=23; s1->name="Peter";
Lecture Contents Some Examples: Write a function for copying an array. Re-implement selection sort using an dynamic array. Write a function that receive an array of n pointers to float and return the pointer that pointing to the maximal float.
Further Readings Textbook 1: Chapter 8. Textbook 2: Chapter 3. Workbook 1: Chapter 6.