13.C语言指针的易错点
1.指针声明和初始化
1.1未初始化(野指针)
int *p; // 未初始化,指向随机地址
*p = 5; // 未定义行为,可能导致崩溃
修正:初始化指针为NULL
或有效地址。
1.2错误类型匹配
int a = 10;
float *p = &a; // 类型不匹配,编译器警告
2.内存管理
内存泄漏
int *p = (int*)malloc(sizeof(int));
// 未调用 free(p)
-
注意:动态内存必须显式释放。
2.1内存重复释放
free(p);
free(p); // 未定义行为(double free)
2.2悬垂指针(Dangling Pointer)
int *p = (int*)malloc(sizeof(int));
free(p);
*p = 10; // p 成为悬垂指针
3.指针运算
3.1 访问越界
int arr[3] = {1, 2, 3};
int *p = arr;
p += 5; // 越界访问,结果不可预测
3.2 指针算术错误
int *p = ...;
p++; // 实际移动 sizeof(int) 字节,而非 1 字节
4.数组与指针
4.1数组名不可重新赋值
int arr[5];
arr = NULL; // 错误!数组名是常量指针
4.2数组作为函数参数退化为指针
void func(int arr[]) {
// 实际等价于 int *arr
}
5.字符串操作
5.1 未终止的字符串
char str[3] = {'a', 'b', 'c'}; // 缺少 '\0'
printf("%s", str); // 可能输出乱码
5.2 缓冲区溢出
char dest[5];
strcpy(dest, "HelloWorld"); // 溢出
6.函数与指针
6.1 返回局部变量指针
int* func() {
int a = 5;
return &a; // a 在函数结束后被销毁
}
6.2 函数指针语法
int (*func_ptr)(int, int); // 正确声明
int *func_ptr(int, int); // 错误:声明了一个返回 int* 的函数
7.多级指针
7.1 错误解引用层级
int a = 10;
int **pp = &a; // 错误:需要 int* 的地址
8. 类型转换与对齐
8.1 void指针转换
void *p = malloc(sizeof(int));
int *q = (int*)p; // 必须显式转换
8.2 对齐问题
char *pc = ...;
int *pi = (int*)pc; // 可能导致未对齐访问(平台相关)
9. const 修饰符
9.1 常量指针 vs 指针常量
const int *p1; // 指向常量的指针(值不可改)
int *const p2; // 常量指针(地址不可改)
10. 空指针问题
10.1未检查 malloc 返回值
int *p = malloc(100 * sizeof(int));
if (p == NULL) { /* 处理错误 */ }
10.2 解引用空指针
int *p = NULL;
*p = 10; // 崩溃
11. 结构体与指针
11.1错误访问成员
struct Point { int x; };
struct Point *p = malloc(sizeof(struct Point));
p.x = 10; // 错误:应使用 p->x
12.指针越界
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
//指针越界
void test01() {
char str[3] = "abc";//指针固定传入abc
printf("str:%s ",str);
}
int main() {
printf("main....start \n");
test01();
printf("main....end \n");
return EXIT_SUCCESS;
}
从上面的指针越界来看,数组定义的长度是3,传入的是abc,abc的长度也是3,但是还有结束符\0所以这个这个属于绝对的越界行为。
13.指针叠加会不断改变指针方向
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void test02() {
char* p = malloc(sizeof(char) * 64);
for (int i = 0; i < 10;i++) {
*p = i + 97;
printf("%c", *p);
p++;
}
free(p);
}
int main() {
printf("main....start \n");
test02();
printf("main....end \n");
return EXIT_SUCCESS;
}
从上面来看p是一直++的,最后释放,存在问题,并不是从初始位释放。所以导致错误
解决方案,我们可以通过一个临时变量来操作他。
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void Ftest02() {
char * p = malloc(sizeof(char) * 64);
if (p != NULL) {
char* pp = p;
for (int i = 0; i < 10;i++) {
*pp = i + 97;
printf("%c", *pp);
pp++;
}
free(p);
}
}
int main() {
printf("main....start \n");
Ftest02();
printf("main....end \n");
return EXIT_SUCCESS;
}
14.返回局部变量地址
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
char* get_str()
{
char str[] = "dabangzhu"; //栈区数据
printf("[get_str]str = %s\n", str);
return str;
}
int main() {
printf("main....start \n");
printf("main str: %s\n",get_str());
printf("main....end \n");
return EXIT_SUCCESS;
}
运行结果:
15.同一块内存释放多次
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void test01()
{
char* p = malloc(sizeof(char)*1024);
if (p!=NULL) {
free(p);
}
//重复释放报错
if (p != NULL) {
free(p);
}
}
int main() {
printf("main....start \n");
test01();
printf("main....end \n");
return EXIT_SUCCESS;
}
free()函数的功能只是告诉系统 p 指向的内存可以回收了,就是说,p 指向的内存使用权交还给系统 ,但是,p的值还是原来的值(野指针),p还是指向原来的内存