初始野指针
C语言中没有任何手段可以判断一个指针是否为野指针!
野指针的由来
1.局部指针变量没有被初始化
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct Student
{
char* name;
int number;
};
int main()
{
struct Student s;
//char a[] = {0};
//s.name = a;
strcpy(s.name, "Delphi Tang"); // OOPS!
s.number = 99;
return 0;
}
2.使用已经被释放过后的指针
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void func(char* p)
{
printf("%s\n", p);
free(p);
}
int main()
{
char* s = (char*)malloc(5);
strcpy(s, "Delphi Tang");
func(s);
printf("%s\n", s); // OOPS!
return 0;
}