C++ basics-4.7 pointers and free storage space

  • Declare and initialize pointers
int a = 10;
int *b = &a; // 将pt的值设置为&b

int *p1, p1; // 一个int类型指针p1, 和一个int类型变量p2,每个指针变量名都需要一个*

When using regular variables, the value is the specified quantity, and the address is the derived quantity. Treat addresses as specified quantities in pointer strategies, and treat values ​​as derived quantities

  • The danger of pointers
// 在C++中创建指针时,计算机将分配用来存储地址的指针1
// ,但不会分配用来存储指针所指向的数据的内存。为数据提供空间是一个独立的步骤
long *fellow;
*fellow = 23323;
// fellow是指针,但是它指向的地址是不确定的,可能在代码段可能是其他。
// 此时将造成隐匿难以发现的bug

Be sure to initialize the pointer to a certain and appropriate address before applying the dereference operator to the pointer.

  • Pointers and numbers
    Although computers usually treat addresses as integers, pointers are not integers. Integers are numbers that can be added, subtracted, multiplied, and divided. The position described by the pointer, multiplying the address is meaningless. From the operations performed on them, they are different from each other, therefore. Can't simply assign an integer to a pointer
int *pt;
pt = 0xB8000000; // c99标准发布之前,C语言允许这样做。
// 但C++在类型一致方面要求更严格。编译器将产生类型不匹配的错误消息

// 要将数字值作为地址使用,应当使用强制类型转换将数字转换为适当的地址类型
int *pt;
pt = (int *)0xB8000000;

pt is the address of the int value does not mean that the type of pt itself is int. In some platforms, the int type is a 2-byte value, and the address is a 4-byte value

  • Use new to allocate memory (new delete & malloc free)
    variables are actually named memory allocated at compile time, and pointers just provide an alias for memory that can be directly accessed by name. Its real use is to allocate unnamed memory to store values ​​at runtime. In this case, memory can only be accessed through pointers
/* typeName * pointer_name = new typeName; */
/* 在两个地方指定数据类型:用来指定需要什么样的内存和用来声明合适的指针 */

int *pt = new int;
*pt = 100;

delete pt;
/* 1. 释放ps指向的内存,但不会删除指针ps本身。可以将pt重新指向另一个新分配的内存 */

/* 2. 要配对使用new和delete;否则将发生内存泄露(memory leak)。
被分配的内存无法使用了,如果泄露严重,程序将由于不断寻找更多内存终止 */

/* 3. 不可重复释放内存块, */
int *ps = new int;
delete ps;
delete ps; // 重复释放

/* 4. 不能使用delete释放声明变量所获得的内存 */
int jugs = 5;
int *p1 = &jugs; 
delete p1; 	// not allowed, memory not allocated by new

/* 只能用delete释放使用new分配的内存, 然而对空指针使用delete是安全的 */

The memory block allocated by new is usually different from the memory allocated by regular variable declarations. The values ​​of variables and regular pointers are stored in stack memory, while new allocates memory from heap memory or free store

  • Create new dynamic array
    for large data (arrays, strings and structures) should be used to create a new array; new is the use of dynamic binding (dynamic binding) way to create an array, it is created in the operating phase and determine the length. Created by declaring arrays, is the use of static binding (static binding) way to create an array, is added to the program at compile time
int *arr =  new int [10];

delete [] arr;
  • When using new and delete, follow the rules
    1. Do not use delete to release memory that is not allocated by new
    2. Do not use delete to release the same memory block twice
    3. If you use new[] bit array to allocate memory, you should use delete[] to release
    4. If you use new[] to allocate memory for an entity, you should use delete to release
    5. It is safe to apply delete to a null pointer

Guess you like

Origin blog.csdn.net/gripex/article/details/105523275