指针真的会乱指
笔者在一次数据结构的实验时,构造了以下代码
StuInfo<T> *p = new StuInfo<Data>;
p=head;
if (account <= 0) return 0;
for (int k = 0; k < account; k++)
{
if (p == 0) return 0;
if (count == 0) { head = info; count++; return -1; }
if (p->next == 0) { p->next = info; count++; return -2; }
p = p->next;
}
info->next = p->next;
p->next = info;
return 1;
这段代码是采用模板形式的类中的函数实例,在编译过程中是通过的,但是运行到
info->next = p->next;
时会出现
0xC0000005: 读取位置 0xCDCDCDCD 时发生访问冲突
的程序中断提示,经多方查证,得知:
* 0xcdcdcdcd : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
就是使用了定义了但未初始化的内存块,从而导致指针在debug时赋予了一个0xcdcdcdcd,也就是一个报错时经常见到的字符串**‘烫烫烫烫……’**。
解决方式:
作者是以一个结构体作为类模板的实例化的,既如下代码所示:
struct Data {
char name[STU_INFO_SIZE]; //学生姓名
char id[STU_INFO_SIZE]; //学生学号
cha