Pointers and memory addressing. Arrays and pointer arithmetic. Strings.
Searching and sorting algorithms.
Pointers and addresses
• Pointer: memory address of a variable
• Address can be used to access/modify a variable from anywhere
• Extremely useful, especially for data structures
• Well known for obfuscating code
C中的指针很迷人,神秘而又强大。它自身其实很简单,就是内存的地址。这使得人们可以直接修改内存,从而可以在理论上做任何事情。其缺点是复杂性大幅上升。
Physical and virtual memory
• Physical memory: physical resources where data can be storedand accessed by your computer
• cache
• RAM
• hard disk
• removable storage
• Virtual memory: abstraction by OS, addressable spaceaccessible by your code
随内存的价格不断下降,计算机能用的内存将更多地取决于虚拟内存,32位的寻址空间太小,将逐渐被64位取代。
Virtual memory
• How much physical memory do I have?
Answer: 2 MB (cache) + 2 GB (RAM) +100 GB (hard drive) + . . .
• How much virtual memory do I have?
Answer: <4 GB (32-bit OS),typically 2 GB for Windows, 3-4 GB for linux
• Virtual memory maps to different parts of physical memory
• Usable parts of virtual memory: stack and heap
• stack: where declared variables go
• heap: where dynamic memory goes
利用栈的特性可以方便地控制程序的执行顺序,堆则用来分配较大的数据和静态数据。堆中的指针存储在栈中,我想这也是C中使用指针的一个重要原因。
我认为C程序的执行过程可以看作遍历一棵Block树的过程,栈可以方便地实现这个遍历过程,因此将内存分配为栈空间是很自然的。
Addressing variables
• Every variable residing in memory has an address!
• What doesn’t have an address?
• register variables
• constants/literals/preprocessordefines
• expressions (unless result is avariable)
• How to find an address of a variable? The & operator
int n = 4 ;
double pi = 3.14159;
int ∗pn = &n ; / ∗ address of integer n ∗ /
double ∗ ppi = &pi ; / ∗address of double pi ∗ /
• Address of a variable of type t has type t *
C中除register类型外,其它变量都有地址。
Dereferencing pointers
• Accessing/modifying addressed variable:
dereferencing/indirection operator *
/ ∗ prints "pi = 3.14159\n" ∗ /
printf ( "pi = %g\n" ,∗ ppi ) ;
/ ∗ p i now equals 7.14159 ∗ /
∗ppi = ∗ppi + ∗pn;
• Dereferenced pointer like any other variable
• null pointer, i.e. 0 (NULL): pointer that does not referenceanything
*操作符的实现应该有两步,首先确定指针的类型,其次从指针的地址开始从内存中读取相应类型大小的数据段。这里也体现了类型的方便之处,可以确定变量的大小,从而使对内存的操作更为容易。
Variables passing out of scope
• What is wrong with this code?
#include < s t d i o . h>
char ∗ get_message ( ) {
char msg [ ] = "Aren’tpointers fun?" ;
return msg ;
}
int main (void) {
char ∗ string = get_message ( ) ;
puts(string) ;
return 0;
}
• Pointer invalid after variable passes out of scope
传自动变量的地址出来是C程序中常见的错误,如果想返回一个地址,那么可以返回堆中的地址,如malloc分配的,但这样也有一个风险,就是malloc和free不在同一个程序块中,free操作容易出错,所以这种方式也不鼓励。
The sizeof() operator
• For primitive types/variables, size of type in bytes:
int s = sizeof(char); /∗ == 1 ∗/
double f; /∗ sizeof ( f ) == 8 ∗/(64-bit OS)
• For primitive arrays, size of array in bytes:
int arr [8]; /∗ sizeof ( arr ) ==32 ∗/ (64-bit OS)
long arr [5]; /∗ sizeof ( arr ) ==40 ∗/ (64-bit OS)
• Array length:
#define array_length(arr) (sizeof (arr)==0 ? 0 : sizeof(arr)/sizeof((arr)[0]))
sizeof是我在写C代码时常用的一个运算符,因为我对内存的分配总是感到很恐怖,好在sizeof帮助了我。使用sizeof的一个重要好处就是增强了程序的通用性,这对于早期的C至关重要。我想sizeof是通过读取程序内部的类型表,从而获得类型的大小,而不是通过计算获得。看来,不同大小的数组其类型也是不一样的.
Pointer arithmetic
• Suppose int ∗pa = arr ;
• Pointer not an int, but can add or subtract an int from apointer:
pa + i points to arr[i]
• Address value increments by i times size of data type
Suppose arr[0] has address 100. Thenarr[3] has address 112.
指针的代数运算又一次影响了我对+运算符的看法,这里的+运算符的确是重载了。在执行+时,编译器检查两个变量,如果有一个是指针,那么就启动指针的代数运算。
Discussion of quicksort
• Not stable (equal-valued elements can get switched) inpresent form
• Can sort in-place – especially desirable for low-memoryenvironments
• Choice of pivot influences performance; can use random pivot
• Divide and conquer algorithm; easily parallelizeable
• Recursive; in worst case, can cause stack overflow on largearray
记得在IBM面试时他们让我写QuickSort,当时我用了两个数组,当时面试官就指出可以本地排序,那会儿要是复习一下该多好。
分享到:
相关推荐
资料目录.bat Advice to next year student.doc lec1.ppt lec10.ppt lec11.ppt lec12.ppt lec13.ppt lec14.ppt lec15.ppt lec16.ppt lec17.ppt lec18.ppt ...lec5.ppt lec6.ppt lec7.ppt lec8.ppt lec9.ppt
In the previous lecture, we leant about impedance spectroscopy. Electrochemical impedance spectroscopy is the technique where the cell or electrode impedance is platted versus frequency. Thus, the ...
programming in computing 10a lec2
EI374 高级算法-全套 PPT 课件-笔记 lec1-slides.pdf lec1.pdf lec2-slides.pdf lec2.pdf lec3-slides.pdf lec3.pdf lec4-slides.pdf ...lec5.pdf lec6.pdf lec7.pdf lec8.pdf lec9.pdf lec10.pdf lec11.pdf
lec5会计控制.pptx
SES # TOPICS KEY DATES Lec 00 Introduction and Course Overview Lec 01 Bezier Curves and Splines Assignment 0 Lec 02 Curves Properties and ...Lec 24 Graphics Hardware and Computer Games Assignment 5
EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件 CA-lec1.pdf ...lec6-OS.pdf lec7-OS.pdf lec8-OS.pdf lec9-OS.pdf lec10-OS.pdf lec11-OS.pdf lec12-OS.pdf Study-Guide.pdf Summary.pdf
lec4.pptx
6 PNNI: Private Network Node Interface ...................... 151 6.1 Introduction ................................... 151 6.1.1 Introduction to the PNNI Routing Protocol .............. 151 6.1.2...
可通过改程序进行危险源的LEC分值的客观化处理。
算法设计与分析:2-Lec6.pdf
算法设计与分析:2-Lec6 .pdf
study.c_plus_plus.lec.5
麻省理工matlab课件-MIT6_094IAP10_lec04.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。
算法设计与分析:Lec5.pdf
图像处理与分析:lec5 Image Enhencement in Frequency Domain(I).pdf
Week1—4_Note_Lec1—6.pdf
CS385 机器学习-全套 PPT 课件-作业 ...lec6-Neural Network.pdf lec7-Clustering.pdf lec8-Feature Reduction.pdf lec9-EM.pdf lec10-HMM.pdf lec11-GM.pdf 作业1.pdf 作业2.pdf 作业3.pdf 作业4.pdf 作业5.pdf
Lec6-企业合并会计.pptx
demo_Lec.m