The Cherno C++ Course(个人抄录与注解笔记)(ch16. Pointer in C++ )

The Cherno C++ Course(个人抄录与注解笔记)(ch16. Pointer in C++ ),第1张

The Cherno C++ Course(个人抄录与注解笔记)(ch16. Pointer in C++ ) The better C++ Course for primers by the Cherno(个人抄录与注解笔记)

第十六章 开始了解指针


本节涉及的指针均为raw pointer(原始指针)。至于smart pointer(智能指针)和其他指针的高阶用法,可以观看Cherno所写的其他视频。

文章目录
  • The better C++ Course for primers by the Cherno(个人抄录与注解笔记)
  • 前言
  • 一. What are the Pointer?
    • (1.1)Definition
    • (1.2)指针的比喻
  • 二、空指针
  • 总结


前言
  • Computers deal with memory. Memory is everything to a computer. Memory(内存)是计算机编程中的根本。
  • When you write an application and you launch it that entire application gets loaded into memory , all of the instructions that tell the computer what to do in the code that you’ve written. All of that gets loaded into memory, and that’s how the CPU can actually access your program and start executing its instructions. 当你打开应用程序时,该程序会被加载到内存中,然后你的CPU才会访问你的程序,并执行相应的代码。
  • When you create a variavle, when you load in data from disk, everything gets stored in memory. There is nothing you can do, if you do not have a memory and pointers are extremely important for managing and manipulating that memory. 如果没有内存(Memory),那么你什么都做不到。内存对于计算机而言,就好比是水和鱼,空气和人类的关系。而指针(Pointer)可以帮助我们管理和 *** 纵内存。
一. What are the Pointer? (1.1)Definition
  • Qustion: So what are pointers? 那么什么是指针呢?
  • Answer: A pointer is an integer, a number which stores a memory address , that is all that is. 指针是一个整数,一个数字。指针存储一个内存地址,这就是指针的全部内容了。
(1.2)指针的比喻
  • Imagine: Put your memory inside your computer is just like one big blob. It’s like one big line that all it is. Picture one is straight if your entire city that you live in, just had a single straight and there was a start , and there was an end, and then you had a bunch of houses, there were no houses across the load. 试想以下,如果你生活在一个条只有起点和终点的街道上,然后街道上有一整排房子(Memory)。这就是内存(Memory)在计算机当中的样子。
  • 从抽象的角度而言,内存的存储是一维的,线性的。对于上述图片而言,这条街(计算机总的内存)上的每一幢房子(计算机当中的一块内存)都有一个门牌号(指针)和门牌号对应的地址。(比如:我家的门牌号是123456,那么123456这6位数字就是指针。)
  • 如果上述图片中,每一个房子的大小均为1byte。那么,我们需要使用某种方法,来获取每一个房子的地址值。
  • Cherno在这里的例子是:假设有人在网上订购了某种商品,并想要送货上门,那么该商品需要被正确地送到购买者手里。当然如果商品质量不合格的话,购买者也可以使用某种方法,把不合格的商品退回去。无论如何,我们需要一种可以从内存(上述图片中的房子)中,读取和写入的方法。于是,指针就出现了。指针就是那个地址,它告诉我们,一小内存块在总体内存中的位置是多少。
  • 简言之,我们编写代码的过程,其实就是从总体内存中,写入或读取一片内存。而内存是计算机中的根本,计算机中的一切都依赖着内存。而指针可以帮助我们,快速找到对应的内存块。
  • 当然,如果你不使用指针写C++程序是完全可以的。但是,实际上指针是非常有用,且高效的工具。笔者在这里的比喻是,对于C++而言,指针就像数学、物理当中的微积分一样重要。
  • Cherno反复强调指针重点是: A pointer is an integer, a number which stores a memory address , that is all that is. 指针是一个整数,一个数字。指针存储一个内存地址,这就是指针的全部内容了。别管什么类型,类型和这些没有任何关系。类型只是我们虚构的,好让我们编写代码更加轻松。无论是int型指针,entity类指针,这些都没关系。A pointer is an integer, a number which stores a memory address , that is all that is.

二、空指针

以下是一个简易的源文件Main.cpp:

#include 

#define LOG(x) std::cout << x << std::endl;

int main() {
	void* ptr = 0;
	std::cin.get();
}

  • 原文: 对于void* ptr=0;Now, I’ll call it PTR or Pointer for short and I’ll set it equal to 0. So what is 0, we’ve given this pointer and memory address of a 0 what does that mean. Well, 0 is not actually a valid memory address. Memory address do not go all the way down to 0. Because 0 is invalid and what that means is that , this pointer is not valid being not valid is a perfectly acceptable state for a pointer. But all I’m saying here is that 0 is not a valid memory address. And we can’t read from or write to a memory address of 0. Or if we try and do that our program will crash.
  • 注解: valid为形容词,此处指被计算机系统认为有效的。所以空指针(void* ptr = 0;)(此处,我们声明了一个叫做ptr的空指针)对于计算机内存而言是,无效的内存。进而,我们不能对空指针进行读写 *** 作,否则会导致项目的崩溃。
  • 以下是void* ptr=0;在VS当中的内存图。当然空指针你也可以写成void* ptr=NULL;或者是void* ptr=nullptr;。在VS当中,如果你声明了一个空指针,那么它的内存地址是0,并且其周围会出现??,提醒你这是个空指针。
  • 而对于一般整形变量,如int value=123;,在编译之后,其内存地址是一个不为0的正数,此处为0x000000BD2FEFF6A4。并且可以知道内存地址(0x000000BD2FEFF6A4)对应的内存值为0x0000007b,即123。那些赋值,且不是空指针的变量,VS使用0xcc来提醒,你是否对变量赋值了。

总结
  • 以上是笔者所抄录的一些重要部分,也是笔者第二次抄录Cherno的课程。但本节Cherno视频中所讲述的内容,却远远比这些文字来得精彩。
  • 如果您有幸阅读到这里,你可以使用搜索引擎去搜索Cherno的C++课程。对于初学者而言,Cherno的课程,就像理查德.费曼所讲述的物理学讲义视频一样,可以使你,在学习C++这类编程语言时,不会像The C++ Primer这类专业书籍,让你头大。
  • A pointer is an integer, a number which stores a memory address , that is all that is. 指针是一个整数,一个数字。指针存储一个内存地址,这就是指针的全部内容了。别管什么类型,类型和这些没有任何关系。类型只是我们虚构的,好让我们编写代码更加轻松。无论是int型指针,entity类指针,这些都没关系。A pointer is an integer, a number which stores a memory address , that is all that is.
    您在闲暇时,不妨再回头看看Cherno这段关于指针的表述,或许这可以更好地帮助您,理解什么是指针。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5699298.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存