cs 251 fall 2019 cs 240 principles of programming
play

CS 251 Fall 2019 CS 240 Principles of Programming Languages - PowerPoint PPT Presentation

CS 251 Fall 2019 CS 240 Principles of Programming Languages Foundations of Computer Systems Ben Wood Programming with Memory the memory model pointers and arrays in C https://cs.wellesley.edu/~cs240/ Programming with Memory 2 Program,


  1. λ CS 251 Fall 2019 CS 240 Principles of Programming Languages Foundations of Computer Systems Ben Wood Programming with Memory the memory model pointers and arrays in C https://cs.wellesley.edu/~cs240/ Programming with Memory 2

  2. Program, Application Software Programming Language Compiler/Interpreter Operating System Instruction Set Architecture Microarchitecture Hardware Digital Logic Devices (transistors, etc.) Solid-State Physics Programming with Memory 3

  3. Instruction Set Architecture (HW/SW Interface ) processor memory Instructions Instruction Encoded Names, Encodings • Logic Instructions Effects • Arguments, Results • Registers Data Local storage Names, Size • • How many Large storage Addresses, Locations • Computer Programming with Memory 4

  4. Byte-addressable memory = mutable byte array Location / cell = element 0xFF…F Identified by unique numerical address • Holds one byte • range of possible addresses address space Address = index Unsigned number • • • • Represented by one word • Computable and storable as a value • Operations: Load: read contents at given address • 0x00…0 Store: write contents at given address • Programming with Memory 5

  5. Multi-byte values in memory Store across con5guous byte loca5ons. 64-bit Words Bytes Address 0x1F 0x1E Alignment 0x1D (Why?) 0x1C 0x1B 0x1A 0x19 0x18 0x17 0x16 0x15 0x14 0x13 ✘ 0x12 0x11 0x10 0x0F 0x0E 0x0D 0x0C 0x0B 0x0A 0x09 0x08 0x07 Bit order within byte always same. 0x06 0x05 0x04 Byte ordering within larger value? 0x03 0x02 0x01 0x00 Programming with Memory 6

  6. Endianness In what order are the individual bytes of a multi-byte value stored in memory? least significant byte most significant byte 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 2A B6 00 0B Little Endian: least significant byte first Address Contents low order byte at low address • 03 2A high order byte at high address • 02 B6 used by x86 , … • 01 00 00 0B Big Endian: most significant byte first Address Contents high order byte at low address • 03 0B low order byte at high address • 02 00 • used by networks, SPARC, … 01 B6 00 2A Programming with Memory 7

  7. Data, addresses, and pointers address = index of a location in memory pointer = a reference to a location in memory, represented as an address stored as data 0x24 0x20 00 00 00 F0 The number 240 is stored at address 0x20 . 0x1C 240 10 = F0 16 = 0x00 00 00 F0 0x18 A pointer stored at address 0x08 0x14 points to the contents at address 0x20 . 0x10 00 00 00 0C A pointer to a pointer 0x0C is stored at address 0x00 . 0x08 00 00 00 20 The number 12 is stored at address 0x10 . 0x04 0x00 00 00 00 08 Is it a pointer? How do we know if values are pointers or not? +3 +2 +1 +0 How do we manage use of memory? memory drawn as 32-bit values, little endian order Programming with Memory 12

  8. C: Variables are locations Compiler maps variable name à location. Declarations do not initialize! int x; // x @ 0x20 int y; // y @ 0x0C 0x24 x 0x20 x = 0; // store 0 @ 0x20 0x1C 0x18 // store 0x3CD02700 @ 0x0C 0x14 y = 0x3CD02700; 0x10 y 0x0C // 1. load the contents @ 0x0C 0x08 // 2. add 3 0x04 // 3. store sum @ 0x20 0x00 +3 +2 +1 +0 x = y + 3; Programming with Memory 13

  9. C: Pointer operations and types address = index of a location in memory pointer = a reference to a location in memory, represented as an address stored as data Expressions using addresses and pointers: & ___ address of the memory location representing ___ a.k.a. "reference to ___" * ___ contents at the memory address given by ___ a.k.a. "dereference ___" Pointer types: ___ * address of a memory location holding a ___ a.k.a. "a reference to a ___" Programming with Memory 17

  10. & = address of C: Pointer example * = contents at Declare a variable, p int* p; that will hold the address of a memory location holding an int int x = 5; Declare two variables, x and y, that hold ints, int y = 2; and store 5 and 2 in them, respectively. Take the address of the memory location representing x p = &x; ... and store it in the memory location representing p . Now, “ p points to x .” the contents of memory at the address Add 1 to given by the contents of the y = 1 + *p; memory location representing p … and store it in the memory location representing y. Programming with Memory 21

  11. & = address of C: Pointer example * = contents at C assignment: location What is the type of *p? Left-hand-side = right-hand-side; What is the type of &x? value What is *(&y) ? int* p; // p @ 0x04 int x = 5; // x @ 0x14, store 5 @ 0x14 y int y = 2; // y @ 0x24, store 2 @ 0x24 0x24 p = &x; // store 0x14 @ 0x04 0x20 0x1C 0x18 // 1. load the contents @ 0x04 (=0x14) x 0x14 // 2. load the contents @ 0x14 (=0x5) 0x10 // 3. add 1 0x0C // 4. store sum as contents @ 0x24 0x08 y = 1 + *p; p 0x04 0x00 // 1. load the contents @ 0x04 (=0x14) +3 +2 +1 +0 // 2. store 0xF0 as contents @ 0x14 *p = 240; Programming with Memory 25

  12. C: Pointer type syntax Spaces between base type, *, and variable name mostly do not matter. The following are equivalent : I prefer this int* ptr; I see: "The variable ptr holds an address of an int in memory." int * ptr; more common C style int *ptr; Looks like: "Dereferencing the variable ptr will yield an int ." Or "The memory location where the variable ptr points holds an int ." Caveat: do not declare multiple variables unless using the last form. int* a, b; means int *a, b; means int* a; int b; Programming with Memory 26

  13. Arrays are adjacent memory locations C: Arrays storing the same type of data. a is a name for the array’s base address, Declaration: int a[6]; can be used as an immutable pointer. element type number of name elements 0x24 0x20 0x1C 0x18 0x14 0x10 0x0C 0x08 0x04 0x00 +3 +2 +1 +0 Programming with Memory 27

  14. Arrays are adjacent memory locations C: Arrays storing the same type of data. a is a name for the array’s base address, Declaration: int a[6]; can be used as an immutable pointer. Address of a[i] is base address a Indexing: a[0] = 0xf0; plus i times element size in bytes. a[5] = a[0]; No bounds a[6] = 0xBAD; check: a[-1] = 0xBAD; Pointers: 0x24 int* p; equivalent { a[5] p = a; 0x20 p = &a[0]; 0x1C *p = 0xA; 0x18 … 0x14 { p[1] = 0xB; 0x10 equivalent a[0] *(p + 1) = 0xB; 0x0C p = p + 2; 0x08 p 0x04 array indexing = address arithmetic 0x00 Both are scaled by the size of the type. +3 +2 +1 +0 *p = a[1] + 1; Programming with Memory 39

  15. C: Array allocation Basic Principle T A [ N ]; Array of length N with elements of type T and name A Contiguous block of N*sizeof(T) bytes of memory Use sizeof to determine char string[12]; proper size in C. x x + 12 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 double a[3]; x x + 8 x + 16 x + 24 IA32 char* p[3]; (or char *p[3]; ) x x + 4 x + 8 x + 12 x86-64 x x + 8 x + 16 x + 24 Programming with Memory 40

  16. ex C: Array access Basic Principle T A [ N ]; Array of length N with elements of type T and name A Identifier A has type T* 0 2 4 8 1 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 Expression Type Value val[4] int 1 val int * val+1 int * &val[2] int * val[5] int *(val+1) int val + i int * Programming with Memory 41

  17. ex C: Null-terminated strings C strings: arrays of ASCII characters ending with null character . Why? 0x57 0x65 0x6C 0x6C 0x65 0x73 0x6C 0x65 0x79 0x20 0x43 0x53 0x00 'W' 'e' 'l' 'l' 'e' 's' 'l' 'e' 'y' ' ' 'C' 'S' '\0' Does Endianness matter for strings? int string_length(char str[]) { } Programming with Memory 43

  18. ex C: * and and [] C programmers often use * where you might expect []: e.g., char*: pointer to a char • pointer to the first char in a string of unknown length • int strcmp(char* a, char* b); int string_length(char* str) { // Try with pointer arithmetic, but no array indexing. } Programming with Memory 44

  19. C: 0 vs. '\0' vs. NULL 0 ' \0 ' Name: zero Name: null character Type: Type: int char Size: 4 bytes Size: 1 byte Value: Value: 0x00000000 0x00 Usage: The integer zero. Usage: Terminator for C strings. NULL Name: null pointer / null reference / null address Type: void* Size: 1 word (= 8 bytes on a 64-bit architecture) Value: 0x00000000000000 Usage: The absence of a pointer where one is expected. Address 0 is inaccessible, so *NULL is invalid; it crashes. Is it important/necessary to encode the null character or the null pointer as 0x0? What happens if a programmer mixes up these "zeroey" values? Programming with Memory 45

  20. Memory address-space layout Addr Perm Contents Managed by Initialized 2 N -1 Stack RW Procedure context Compiler Run time Programmer, Dynamic Heap RW malloc/free, Run time data structures new/GC Global variables/ Compiler/ Statics RW Startup static data structures Assembler/Linker Compiler/ Literals R String literals Startup Assembler/Linker Compiler/ Text X Instructions Startup Assembler/Linker 0 Programming with Memory 50

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend