Outline CS11600: Introduction to � Computer memory Computer Programming (C++) � Lvalues and rvalues � Arrays and strings Lecture 5 � Pointers � Dynamic memory allocation Svetlozar Nestorov University of Chicago 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 2 Memory Heap and Stack � The Stack: � Hierarchical memory organization: • When a function is called a new frame is pushed on • Cache the stack. • RAM (main memory) • The frame contains parameters, local variables, and other info. • Hard disk (secondary storage) • When a function call returns its frame is popped off • Tape (tertiary storage) the stack. � Our focus is on RAM: � The Heap: • For dynamically allocated memory. • Think of it as a long list of bytes. � Heap and stack are on opposite end of memory and grow towards each other. 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 3 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 4 Lvalues and Rvalues Arrays � Lvalue is writable memory location, i.e. can be � Basic form: assigned a value. Type name[size] = {val1,val2,…}; � Rvalue is data at memory location. � Values are optional; the number of values must � Constants (constant variables and literal be less than size but not more. constants) have only rvalues. � Size must be a constant integer expression. � Variables have rvalues and lvalues. � Examples: • Lvalue is used on the left side of assignments. int scores[20]; • Rvalue is used on the right side of assignments. float gpas[] = {3.4, 3.6, 2.1, 4.0} double prices[5] = {199.99, 201.11, 11.0} 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 5 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 6 1
Multidimensional Arrays Accessing Arrays � Access subscripts mimic array definition: Type name[size1][size2]… = {{val1,val2,…},…}; � Example: name[expr1][expr2]… int grades[3][4] = { � The index expressions may involve variables and {10, 10, 10}, must evaluate to integers. {1, 1} � Array subscripts start from 0! }; � Examples: char hi[2][2][2] = {{{‘h’,’e’}, {‘l’,’l’}}, gpas[0] is 3.4; gpas[4] is undefined {{‘o’}} grades[1][0] is 1; grades[2][2] is undefined }; hi[0][1][0] is ? 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 7 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 8 Strings Pointers � Strings are represented as NULL-terminated one- � A pointer is a memory address: dimensional arrays of char ’s. Type *pname [= value]; � Accessing data pointed to by a pointer is called � Examples: dereferencing : char hello[] = “hello”; is equivalent to *pname char hello[] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; � A pointer definition does not allocate memory for the data to which it points! � A pointer can be initialized with a reference to already defined variable of the appropriate type. � Examples 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 9 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 10 Pointers and Arrays Dynamic Memory Allocation � Pointers and array are related by the � Why do need it? following rule: � Two operators: name[i] is equivalent to *(name + i) • new allocates memory. � Example: • delete de-allocates memory previously allocated with new . char hi = “hello”; � Memory is allocated on the heap. *hi is ‘h’; *(hi+4) is ‘o’; � No garbage collection – delete what you allocated! 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 11 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 12 2
New New and Arrays � Basic form: � Primary use of new is for allocating arrays of variable length and user-defined types. new Type • With initialization � Syntax mimics array declaration: new Type(value) new Type[size1][size2]… � Returns a pointer to an object of Type. � size1 can be a variable expression. � Example: � Returns a pointer to the first element. int *n = new int(5); • But memory is allocated for all elements! char *p; � Examples. p = new char; 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 13 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 14 Delete � De-allocates memory allocated with new delete ptr; delete [] ptr; (for arrays) � Example: int *zips = new int[k]; zips[0] = 60611; /* do something with zips */ delete [] zips; 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 15 3
Recommend
More recommend