- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
C语言指针
展开查看详情
1 .CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http:// inst.eecs.berkeley.edu /~cs61c
2 .Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers 2
3 .Processor Control Datapath Components of a Computer 3 PC Registers Arithmetic & Logic Unit (ALU) Memory Input Output Bytes Enable? Read/Write Address Write Data ReadData Processor-Memory Interface I/O-Memory Interfaces Program Data CS 61c Lecture 3: Pointers
4 .Computer Memory CS 61c Lecture 3: Pointers 4 int a; a = -85; printf (“%d”, a); Do not confuse memory address and value. Nor a street address with the person living there. Type Name Addr Value … 108 107 106 105 104 103 102 101 100 …
5 .Pointers C speak for “memory addresses” Notation int *x; // variable x is an address to an int int y = 9; // y is an int x = &y; // assign address of y to x // “address operator” int z = *x; // assign what x is pointing to to z // “dereference operator” *x = -7; // assign -7 to what x is pointing to What are the values of x, y, z? CS 61c Lecture 3: Pointers 5 Type Name Addr Value … 108 107 106 105 104 103 102 101 100 …
6 .Pointer Type Pointers have types, like other variables “type of object” the pointer is “pointing to” Examples: int *pi; // pointer to int double * pd ; // pointer to double char *pc; // pointer to char CS 61c Lecture 3: Pointers 6
7 .Generic Pointer (void *) Generic pointer Points to any object ( int , double, …) Does not “know” type of object it references (e.g. compiler does not know) Example: void *vp; // vp holds an address to // object of ”arbitrary” type Applications Generic functions e.g. to allocate memory malloc , free accept and return pointers of any type see next lecture CS 61c Lecture 3: Pointers 7
8 .Pointer to struct CS 61c Lecture 3: Pointers 8
9 .Your Turn! Answer a b RED 3 -7 GREEN 4 5 ORANGE -4 5 YELLOW -2 5 CS 61c 9 Type Name Addr Value … 108 107 106 105 104 103 102 101 100 …
10 .What’s wrong with this Code? CS 61c Lecture 3: Pointers 10 Output: a = 1853161526, p = 0x7fff5be57c08, * p = 0
11 .Pointers as Function Arguments C passes arguments by value i.e. it passes a copy value does not change outside function To pass by reference use a pointer CS 61c Lecture 3: Pointers 11 Type Name Addr Value … 108 107 106 105 104 103 102 101 100 …
12 .Parameter Passing in Java “primitive types” ( int , char, double) by value (i.e. passes a copy) O bjects by reference (i.e. passes a pointer) Java uses pointers internally But hides them from the programmer Mapping of variables to addresses is not defined in Java language No address operator (&) Gives JVM flexibility to move stuff around CS 61c Lecture 3: Pointers 12
13 .Your Turn! Answer a b c RED 5 3 1 GREEN 1 5 3 ORANGE 3 3 1 YELLOW 3 5 1 CS 61c Type Name Addr Value … 105 104 103 102 101 100 …
14 .Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers 14
15 .C Arrays CS 61c Lecture 3: Pointers 15 Declaration: // allocate space // unknown content int a[5]; // allocate & initialize int b = { 3, 2, 1 }; Element access: b[1]; a[2] = 7; Index of first element: 0 Type Name Addr Value … 108 107 106 105 104 103 102 101 100 …
16 .Beware: no array bound checking! Output : a[0 ] = 1 a[1 ] = 2 a[2 ] = 3 a[3 ] = - 1870523725 CS 61c Lecture 3: Pointers 16 Often the result is much worse : erratic behavior segmentation fault , etc. C does not know array length ! Pass as argument into functions
17 .Use Constants, Not Literals Assign size to constant Bad pattern int i , ar [10]; for( i = 0; i < 10; i ++){ ... } Better pattern const int ARRAY_SIZE = 10; int i , a[ARRAY_SIZE]; for( i = 0; i < ARRAY_SIZE; i ++){ ... } “Single source of truth” Avoiding maintaining two copies of the number 10 And the chance of changing only one DRY: “Don’t Repeat Yourself” CS 61c Lecture 3: Pointers 17
18 .Pointing to Different Size Objects Modern machines are “byte-addressable” Hardware’s memory composed of 8-bit storage cells, each has a unique address Type declaration tells compiler how many bytes to fetch on each access through pointer E.g., 32-bit integer stored in 4 consecutive 8-bit bytes 18 42 43 44 45 46 47 48 49 50 51 5 2 53 54 55 56 57 58 59 i nt *x 32-bit integer stored in four bytes short *y 16-bit short stored in two bytes char *z 8-bit character stored in one byte Byte address CS 61c Lecture 3: Pointers
19 .sizeof () operator sizeof (type) R eturns number of bytes in object Number of bits in a byte is not standardized All modern computers: 8 bits per byte Some “old” computers use other values, e.g. 6 bits per ”byte” By definition, in C sizeof (char)==1 For all other types result is hardware and compiler dependent Do not assume - Use sizeof ! 19 CS 61c Lecture 3: Pointers Output : double : 8 array : 20 s : 4
20 .Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers 20
21 .So what did Dr. Moore Predict? Transistor* cost as a function of components per chip M inimum S hifts to right: A s time passes, cost decreases provided we get more F ortunately we always had good ideas to use more: C omputers M emory S martphones Internet of Things? Why a minimum? If too small, some don’t work! CS 61c Lecture 3: Pointers 21 * Transistors: basic elements making up computers ( see later)
22 .Dr. Moore’s Vision (in 1965) Something useful that is getting always better and less expensive is good for Society Business CS 61c Lecture 3: Pointers 22
23 .Why do people say Moore’s Law is over? CS 61c Lecture 3: Pointers 23
24 .Fabs (where chips are made) $5-10B CS 61c Lecture 3: Pointers 24 Final Four: Intel TSMC Samsung Global Foundries (was IBM) 130nm 90nm 65nm 40nm 28nm <=22nm
25 .Break! 8/30/17 25 Fall 2017 - Lecture #3
26 .Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers 26
27 .Pointer Arithmetic - char CS 61c 27 Type Name Byte Addr * Value … 108 107 106 105 104 103 102 101 100 … * pc = b c = 0x7fff50f54b3e pc = 0x7fff50f54b3f pc-c = 1 *Computer only uses byte addresses. Tables with blue headers are simplifications.
28 .Pointer Arithmetic - int CS 61c Lecture 3: Pointers 28 Type Name Byte Addr Value … 108 107 106 105 104 103 102 101 100 … *pi = 20 i = 0x7fff50f54b40 pi = 0x7fff50f54b44 pi- i = 1
29 .Array Name / Pointer Duality Array variable is a “pointer” to the first (0 th ) element Can use pointers to access array elements char * pstr and char astr [] are nearly identical declarations Differ in subtle ways: astr ++ is illegal Consequences: astr is an array variable, but works like a pointer astr [0] is the same as * astr astr [2] is the same as *(astr+2) C an use pointer arithmetic to access array elements CS 61c Lecture 3: Pointers 29