12 KiB
第四章 专辑-指针
指针
- 计算机在存储数据时必须跟踪的三种基本属性:
- 信息存储在何处
- 信息的值是多少
- 存储的信息是什么类型
- 指针是一个变量,存储的是值的地址,而不是值本身。使用常规变量时,值是指定的量,地址为派生量。
int test = 99;
int* p_test = &test;
int* p1,p2; //这里声明了int指针p1和int变量p2.
-
声明和初始化指针:声明指针必须指定指针指向的数据类型。对每个指针变量名,都需要使用一个指针符来声明。
-
指针的危险
int* example; *example = 999999; //发生不可预测的错误- 在使用解除*之前,必须将指针初始化给一个确定的、适当的地址。
-
指针和数字:不能简单的将整数赋值给指针。
new和delete
-
使用new来分配内存:
int* example = new int;- 在程序运行过程中分配对应的内存空间,存储在自由存储区中。计算机由于没有足够的内存而无法满足new请求时,new通常返回空指针。
-
使用delete来释放内存:
int* example = new int; delete example; //2 int* ps = &test; delete ps; //not allow-
要配套使用new和delete,否则会发生内存泄漏,导致被分配的内存再也无法使用。
-
不要尝试释放已经释放的内存块。不能使用delete释放声明变量获得获得的内存。
-
只能使用delete释放new分配的内存,对空指针使用delete是安全的。
int* ps = new int; int* pq = ps; delete pq; //此时pq释放掉了ps内存- 使用delete关键将它用于new分配的内存。
-
-
使用new来创建动态数组:
int* example = new int[10]; delete[] example; //2 int* example = new int[10]; example[0] = 1; cout << example[0] << endl; delete[] example; //3 int* example = new int[10]; example[0] = 1; example[1] = 2; cout << *(example+1) << endl; //指向第二个元素 delete[] example;- 将数组元素类型和元素数目告诉new即可,new运算符返回第一个元素地址。
- 释放时候的方括号告诉程序,释放整个数组。
- 不能使用sizeof运算符来确定动态分配的数组包含的字节数。
-
使用new和delete要遵守的规则:
- 不要使用delete释放不是new分配的内存。
- 不要使用delete释放同一个内存两次。
- 使用new[]为数组分配内存,则应当使用delete[]来释放。
- 对空指针使用delete是安全的。
指针、数组和指针算数
int a[100];
int* p_a = a;
p_a = a = &a[0];
//2
double example[3] = {1.1,2.2,3.3};
short statcks[3] = {3,2,1};
double (*p_example)[3] = &example;
//1.1
cout << **p_example << endl;
//0x920bbffa00
cout << *p_example <<endl;
//0x920bbffa00
cout << p_example << endl;
//0x920bbffa00
cout << example << endl;
//3
double a1[4] = {1.1,2.2,3.3,4.4};
//创建一个指向4个double字节的指针
double (* p_a1)[4] = &a1;
//创建10个结构指针
double* gg[10];
double **qq = gg;
- 将指针变量+1,增加的量等于它指向的类型的字节数
- C++将数组名解释为数组第一个元素的地址
- 对数组使用sizeof得到的是数组的长度,对指针得到的是指针的长度。在这种情况下,C++不会将数组名解释为地址。
数组的静态联编和动态联编
- 所需的内存空间一个是在编译时已经分配好的,一个是在程序运行时才进行分配的。
指针和字符串
const char* bird = "test"; //可以使用bird访问字符串,但不能修改它
const char* str = "test2"; //这里的const指的是*str所指向的指
cout << str << (int*) str << endl; // str = "test2",*str=t,(int*) = 地址
//字符串赋值给new指针的正确方法
char test[20] = "hello world!";
char* p_test = new char[strlen(test) + 1];
strcpy(p_test,test);
cout << p_test << endl;
delete[] p_test;
//C++11中必须加入const
const char* p = "hello world!";
- 在cout和多数C++表达式中,char数组名、char指针以及用引号括起来的字符串常量都被解释为字符串第一个字符的地址。
使用new创建动态结构(适用于类)
创建结构
example* ps = new example;
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
struct test{
char name[20];
double price;
};
test* p_example = new test;
//test example = {"XIAOAN",1999};
//p_example = &example;
cin.getline(p_example->name,20);
p_example->price = 99;
cout << (*p_example).name << " and " << (*p_example).price << endl;
//等价于
cout << p_example->name << " and " << p_example->price << endl;
delete p_example;
return 0;
}
- 与结构不同的是,创建这种结构,不知道其名称,只知道地址。
- 访问成员:(->)
- 结构标识符是结构名,使用句点运算符。标识符是指向结构的指针,则使用箭头运算符。
一个使用new和delete的实例
//根据输入来分配内存
#include <iostream>
#include <cstring>
char* getname(void);
int main()
{
char* name;
name = getname();
std::cout << name << " &=" << (int*)name << std::endl;
delete name;
return 0;
}
char* getname()
{
using std::cout;
using std::endl;
using std::cin;
using std::strlen;
char temp[80];
cout << "Enter last name:";
cin.get(temp,80);
char* example = new char[strlen(temp)+1];
strcpy(example,temp);
return example;
}
自动存储、静态存储和动态存储
- 自动存储(存在于栈中):所属函数调用时产生,函数结束时候释放。局部变量,作用域包含它的代码块。如果在某个代码块内定义了一个变量,则该变量仅在程序执行该代码块中的代码时存在。
- 静态存储:静态存储是整个程序执行期间都存在的存储方式,在函数外面声明,使用关键字static。static double example = 22.22;
- 动态存储:new/delete
- 线程存储
函数和数组
int sum_arr(int arr[],int n);
int sum_arr(int* arr,int n);
//arr[i] == *(arr+i)
//&arr[i] == arr+i
int sum_arr(const int arr[],int n);
-
arr是一个指针,这样传递值,节省了内存。
-
函数如何使用指针处理数组,数组名解释为数组的第一个元素的地址。
-
使用数组区间的函数:第一个参数是数组的第一个元素,第二个参数是test+size;
#include <iostream> int sum_arr(int* test,int* end); int main() { using namespace std; int test[10] = {1,2,3,4,5,6,7,8,9,10}; cout << sizeof(test)/sizeof(test[0]); cout << sum_arr(test,test+sizeof(test)/sizeof(test[0])); return 0; } int sum_arr(int* test,int* end) { int* pt; int count = 0; for (pt = test; pt != end; ++pt) { count = count + *pt; } return count; }
指针和const
int age = 25;
const int* p_age = &age; //*p_age的值不能被修改
//当变量值本身就为const时
const float test = 1.1;
float* p_test = &test; //不允许
const float* p_test = &test; //允许
//指针指向指针
int test = 22;
int* p_test = &test;
const int* p2_test = p_test; //*p_test不允许修改
-
如果非要用非指向const的指针去指向const变量,强制类型转换可以突破这种限制。
-
const指向的变量只能使用const指向的指针。非const指向的变量可以是const指针也可以是非const指针。
-
在函数调用中,如果函数参数是数组变量,这个数组变量是const,但是函数没有声明const,这样的调用是不允许的。
-
将指针参数声明为常量数据,尽可能使用const可以避免无意中修改数据而导致的错误。使用const使得函数可以处理const和非const实参,否则只能接受非const参数。
-
可以修改指针的值,但是不能修改指针指向的值
float test = 1.1; float test2 = 2.2; const float* p_test = &test; p_test = &test2; -
可以修改指针指向的值,但是不能修改指针的值
int test = 99; int const* p_test = &test; //在这里之后,p_test的值不能被修改,但是*p_test的值可以修改 -
既不能修改指针指向的值,也不能修改指针值。
const int const* p_test = &test; -
在不修改数组的函数,通常使用const声明函数
void show_array(const int arr[],int n);
二维数组
//void example(int test[][3],int size);
//void example(int(* test)[3],int size);
//代码实例:
#include <iostream>
void Read_twoarr(int test[][3],int size);
void Read_twoarr2(int(* test)[3],int size);
int main()
{
int test[2][3] = {
{1,2,3},
{3,2,1}
};
Read_twoarr(test,2);
Read_twoarr2(test,2);
return 0;
}
void Read_twoarr(int test[][3],int size)
{
for (int i = 0; i < size; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << test[i][j] << std::endl;
}
}
}
void Read_twoarr2(int(* test)[3],int size)
{
for (int i = 0; i < size; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << test[i][j] << std::endl;
}
}
}
函数与字符串
-
要将字符串作为参数传递给函数:
- char数组
- 用引号引起来的字符串常量
- 被设置为字符串的地址的char指针
- 字符串以'\0'结尾,因此传递时无需传递字符串长度。
-
★处理字符串中的字符的标准方法:
while(*str) { statements; str++; }
函数指针
- 不能在函数中定义函数,但是可以在函数中调用其他函数
- 函数的地址存储器机器语言的代码的内存开始地址。
- 获取函数地址:函数名即为函数地址:例如:example()是一个函数,example即是该函数地址。
process(example); //指的是可以在process函数中调用example()函数
process(example()) //指的是调用process前,先调用example()函数将其结果返回给process供其调用。
-
声明一个函数指针:
double example(int ); //2 double(* p_example)(int ); //赋值 p_example = example; //3 double* p_example2(int );-
(* p_example)是函数,p_example为函数指针
-
double *p_example(int )意味着p_example2是一个返回指针类型的函数,而代码2意味着p_example 是一个指向函数的指针。
-
实际应用:想要将一个计算时间的函数和自己编写估计时间的算法结合起来,将估算算算法地址传递给它,其函数原型如下:
void estimate(int line,double (*pf)(int)); // 调用方法 estimate(50,pam);- 第二个参数是一个函数指针,它指向它的函数接受一个int参数,且返回一个double值。
-
使用函数指针来调用函数:
//(* p_example)和函数名等价 //C++也允许像使用函数名那样使用p_example p_example = example; x = p_example(4); y = (*p_example)(4); //二者等价
-
-
深入函数指针:
const double* f1(const double ar[],int n); const double* f2(const double [], int); const double* f3(const double*,int) //三者等价 //声明函数指针: const double* (*pf_example)(const double*,int) = f1; //使用过C++11自动类型推断 auto p2 = f2; cout << (*pf_example)(av,3) << *(*pf_example)(av,3); cout << p2(av,3) << *p2(av,3);-
包含多个函数指针的数组
const double* (*pa[3])(const double*,int) = {f1,f2,f3}; auto pb =pa; const double *px = pa[0](av,3); const double *py = (*pb)[1](ab,3); //获取指向double的值: double x = *pa[0](av,3); double y = *(*pb)[1](ab,3); //要想创建指向数组的指针 auto pc = &pa; //---- const double* (*(*pd)[3]))(const double *,int) = &pa;
-