So, you know, we've got a bunch of juicy topics to dive into this week involving pointers and memory.
这周我们有很多有趣的主题,包括指针和内存。
Here's the problem with that technique, and by the way, a number of programming languages use this, including Lisp.
这会指向元素的值,然后对内存进行切分,关于这种技术有个问题,顺路说下。
One of the perhaps easiest ones to put your finger on is what if the computer is out of memory?
一个可能的情况是,计算机的内存不足?
I've got some internal variables I have to store, what kinds of things do I have to keep track of?
计算的时候需要占用多少内存,我需要存储一些内部变量,我需要记录哪些东西呢?
When you call a function, what actually happens is that function gets its own chunk of memory that's identical in size to the data type that it expects.
当你调用一个函数,实际上发生的是,函数得到了自己的内存块,相同的类型,是占有一样大小的内存的。
It turns out that slightly before the stack, slightly before this conceptual chunk of memory, there's other things called environment variables that we may see over time but elsewhere in memory are other things.
结果是稍微在堆之前,稍微在这个内存块概念之前,另外还一个东西叫做环境变量,我们之后可能看到,内存中的其他地方,是其他的东西。
Which may take up some arbitrary amount of memory. In that case, I'm back to this problem.
然后将接下来的每一个内存块设置为,指向数组对应元素值的指针。
Now, if I've asked the user for a string and then a moment later I ask the user for another string, well, they're going to end up in different locations in memory just by nature of get string.
现在,如果我要用户输入一个字符串,一会儿之后我要用户输入另一个字符串,他们最终是在,不同的内存地址中。
Its name is malloc for memory allocation and what malloc does for us is we say, hmm, the user has typed in a three-letter word.
它为我们分配内存地址,它为用户输入的3个字母单词,分配地址。
You actually need to kind of do this and then you can swap these two numbers but it requires 50 percent more memory.
你实际上需要这样做,然后才能交换两个数字,但是它需要多50%的内存。
Two pieces of memory actually touching each other or you touching memory that you don't actually own, in which case the computer doesn't really know what to do and just, bam.
两块内存相互覆盖,或者你覆盖了不存在的内存,那样的话计算机不知道,该怎么做,只有崩溃。
You can touch almost any part of memory in the computer systems that you want with your program, but do you want to?
通过你的程序,你可以触及到计算机系统,内存的任意部分,但是那是你想要的吗?
Well, if that function calls another function, you put another tray on, and so that new tray represents that function's chunk of memory.
好的,如果那个函数调用另一个函数,你再放一个托盘在上面,新的托盘代表,那个函数的内存块。
Or another way of thinking about it, is remember I said when I call that class definition it creates an instance, that's a pointer to some spot in memory that's got some local information around it.
也就是一个指向,内存中某处的指针,这个地方存有一些,这个实例的信息,浅意义的相等也就是说,看看这两个东西是不是。
Alright, so it turns out there're two other useful types that you don't get for free out of the box.
我们还是回到这里,还有两种很有用的数据类型,这两种都不用担心内存不足。
I've been using the blackboard a little ad hoc here, but suppose I neaten things up here now and present this rectangle is let's say R RAM, the memory inside of your computer.
我在这里特别地使用这块黑板,假如我整理这些东西,把它们呈现,这个长方形,假如说就是内存,你电脑的内存。
Why not write the program in a way where you figure out dynamically when the program is run how much memory you need rather than hard coding in two with or within that constant.
为什么不写一个可以动态的方式写那个程序,程序可以动态分配内存,而不是用常量硬编码两个值。
And one of the ways you can make a program crash, intentionally or not, is to essentially use up too much memory or call too many functions and what happens is, bam, one hits the other and bad things happen.
其中一个方式能使你的程序崩溃掉,有意或无意的,它本质上使用了太多的内存,或者调用了太多的函数,所发生的,崩掉,一个冲突了另一个,然后发生了坏事情。
Well, again, just as I said verbally a moment ago, if you're going here, you're literally touching, trying to read or change memory that's beyond the boundaries of a chunk of memory that you're supposed to be touching based on its length.
嗯,再次强调,就像我刚才说的,如果你从这里出发,你将依次接触到,试着读取或者改变内存,如果超出了你可以使用的,内存块长度的界限。
I have no idea what's here at the moment so I'm just going to draw a question mark because that memory might have been used previously for some other purpose, but we know it's currently available to us so we have four bytes of memory.
我不知道在此刻这里是什么,所以我画一个问号,因为那块内存可能在之前,为其它的目的而使用过,但是我们知道它可以为我们所使用,这样我们就有4个字节的内存。
Or another way of saying it is, we're going to use as the basic steps, those operations that run in constant time, so arithmetic operations.
我们用可以在恒定时间内完成的操作,算法,比较,内存读取。
It's just some random chunk of four bytes that happens to be available at this point in time, but I can put something there.
只是一些随机的四字节的内存块,在这里是可以使用的,但是我可以在那里放置东西。
Right now it's empty, actually it's not quite empty, it has a pointer back to the class.
当我调用了这个类的,定义声明的时候,它在内存中的特定点。
Every time I use it, I'm creating a new instance, that's a different thing inside of memory.
我每次调用它,我就新建了一个类,这在内存中是个不同的事情。
All right so it has to live in RAM as opposed to the hard drive because otherwise things would be terribly slow as you know so it's much better if your programs live while they're running in RAM and they end up in what's called the tech segment.
好的,它存在内存中,而不是硬盘中,因为,否则程序会非常的缓慢,程序在内存中运行会好得多,它们在技术段,的地方结束。
Well, because the memory we've been using for local variables, and for functions, storage space is what computer scientists generally call the stack.
好的,因为我们看到的内存是用于局部变量的,用于函数的,存储空间就是计算机科学家,通常说的堆。
Not by some very arcane numeric address inside my computer's memory, but by a symbol, by a name.
不是通过一些在我电脑内存中的神秘的数字地址,而是通过一个符号,一个名字。
That is the address in memory in the heap of the first byte that the user typed in.
那是用户输入的字符串第一个字节,内存中堆的地址。
Well, if you forget where the memory is that you asked for, well, in fact, that is how you make a memory leak.
嗯,如果你忘了你申请的内存在哪里,呃,实际上,这就是造成内存泄露的原因。
Somewhere in memory, back to back to back or all of the strings and with them can we store actual words and phrases.
在内存某处,紧邻的或者所有的字符串,我们用它们可以存储实际的单词,和短语。
应用推荐