1 Dynamic Variables 2 Table of Contents Dynamic Variables Pointer Declaration Pointer References Pointer Manipulation Addressing: Direct & Indirect Record Arrays of Pointer Expressions Dynamic Srage Dynamic Memory Problems Reference Variables Static Variables Size is fixed throughout execution Size is known at compile time Space/ is allocated at compilation Dynamic Variables Created during execution "dynamic allocation" No space allocated at compilation time Size may vary Structures are created and destroyed during execution. Knowledge of structure size not needed Memory is not wasted by non-used allocated space. Srage is for addresses. Example of Assume: Houses represent data Addresses represent the locations of the houses. Notice: To get a house you must have an address. No houses can exist without addresses. An address can exist without a house (vacant lot / NULL )
Pointer Declaration 3 Pointer References 4 Pointer Type Simple type of variables for sring the addresses of other locations Pointer Variables Declarations The asterisk * character is used for variable s: int* float iptr; *fptr; iptr is a an integer fptr is a a real Given the : Declares be a variable, but is a simple integer variable. Equivalent : Declare all variables in separate statements. Pointer Type Definitions: int* int recommended form common common int*, ; ; ; typedef int *intptr; intptr ; strong strong type type (preferred) (preferred) Address Operar: & Unary operar that returns the hardware location address of it s operand. Given: Address Assignment: Dereference / Indirection Operar: * unary operar that returns the contents at the address contained in the variable. Pointer Output: Results: int* ; int* ; int numa, numb; numa = 1; numb = 2; = &numa; = &numb; cout << << * << endl; cout << << * << endl; 0xF4240 1 0x3B9ACA00 2
Pointer References 5 Pointer Manipulation NULL Pointer Pointer constant, address 0 Named constant in the <stddef.h> include header Represents the empty points nowhere, unique /address value Symbolic/graphic representations: Illegal: *NULL Pointer Diagrams Given (text/code representation) Graphic representation #include <stddef.h> void main() { int* = NULL; int* = NULL; int numa, numb; numa = 1; numb = 2; } Pointer Assignments Independent from initial code above. #1 = &numa; = &numb; 1 numa numb 2 numa 1 numb 2 #2 * = * - 1; = ; numa 1 numb 0 #3 = *; * = ;
Addressing: Direct & Indirect Direct Addressing normal variable access non- variables represent one-level of addressing non- variables are addresses locations containing data values. compilers sre variable information in a symbol table : symbol type address x int 0xF4240 iptr (int) 0xF4241 compilers replace non- variables with their addresses & fetch/sre operations during code generation. Indirect Addressing accessing a location s contents thru a variables represent two-levels of addressing variables are addresses locations containing addresses. compilers replace variables with their addresses & double fetch/sre operations during code generation. indirect indirect addressing addressing variable. variable. x = 28 ; iptr = &x ; MEMORY address contents 0xF4239??? 0xF4240 28 0xF4241 0xF4240 0xF4241??? 7 Record structures: Given: Member Access Field Access Examples: Errors: Arrow Operar Short-hand notation: const int f3size = 20; struct rectype { int field1; float field2; char field3[f3size]; }; typedef rectype *recptr; rectype recptr r1ptr = &rec1; cout << (*r1ptr).field1 << (*r1ptr).field2 << (*r1ptr).field3 ; cout << *r1ptr.field1 << *r1ptr.field2 << *r1ptr.field3 ; cout << r1ptr->field1 << r1ptr->field2 << r1ptr->field3 ; rec1(1, 3.1415, pi ); r1ptr; 8 parenthesis parenthesis due due operar operar precedence; precedence; without without compiler compiler attempts attempts fields. fields. -> -> is is an an ANSI ANSI C C member member selection selection operar. operar. Equivalent Equivalent : : (*).member (*).member
Arrays of 9 Pointer Expressions 10 Declarations: Given: Member Access Field Access Examples: Arrow Operar Short-hand notation: const int size = 20; struct rectype { int field1; float field2; char field3[size]; }; typedef rectype *recptr; rectype rec1(1, 3.1415, pi ); recptr rayptrs[size]; rayptrs[size-1] = &rec1; cout << (*rayptrs[size-1]).field1 << (*rayptrs[size-1]).field2 << (*rayptrs[size-1]).field3 ; cout << rayptrs[size-1]->field1 << rayptrs[size-1]->field2 << rayptrs[size-1]->field3 ; Arrays == Non-indexed Array variables are considered s in C Array names as s contain the address of the zero element (termed the base address of the array). Given: equivalent assignments Pointer Indexing allocation) allocation) All s can be indexed, (logically meaningful only if the references an array). Logical Expressions NULL tests: Equivalence Tests: const int size = 20; char name[size]; char *person ; person = name ; person = &name[0] ; person[0] = ; person[size-2] =. ; if (!person) //true if (person == NULL) if (person == name) //true if s reference //the same address Does Does not not create create a a copy, copy, (no (no preferred check types types must must be be identical identical
Dynamic Srage Heap (Free Sre, Free Memory) Area of reserved by the compiler for allocating & deallocating a program during execution. Operations: Allocation char* name; int* iptr ; // C++ C name = new char ; name = (char *) malloc(sizeof(char)); iptr = new int [20] ; iptr = (char *) malloc(20 * sizeof(char)); //initialization name = new char ( A ) ; dynamic array allocation Deallocation C++ function C new type allocation malloc(# bytes) delete deallocation free NULL is returned if the heap is empty. typecasts // C++ C delete name ; free(name); delete [] iptr ; are undefined after //delete [20] iptr ; deallocation. 11 Dynamic Memory Problems Given: Garbage Previously allocated that is inaccessible thru any program s or structures. * Aliases typedef int *intptr; intptr, ; = new int (); = NULL; Two or more s referencing the same location. = new int (); = ; before during after Dangling References that reference locations previously deallocated.? = new int (); = ; delete ; leaks leaks?? 12?
Reference Variables 13 Reference Variable Declarations The and & character is used for reference variable s: int& float iptr; &fptr1, &fptr2; Reference Reference variables variables are are aliases aliases for for variables. variables. Pointer Differences Reference variables do NOT use the address and operars (& *). Compiler s reference variables transparently. Reference variables are constant addresses, assignment can only occur as initialization or as parameter passing, reassignment is NOT allowed. Examples: Purpose char char& //char* chref = B ; //achar = B ; //*chptr = B ; achar = A ; chref = achar; chptr = &achar; Frees programmers from explicitly dereferencing accessing, (in the same way non variables do). Cleans up the syntax for standard C arguments and parameters.