摘要:
在遍历链表时正确释放元素的方法是:使用临时变量存储下一个元素的地址。这样就可以安全的地在任何时候释放当前元素,不必担心在取下一个元素的地址时还要引用它。代码如下:(注意for执行的顺序)struct node *p, *start, *tmp;for(p=start; p; p=tmp){ tmp = p->next; delete(p); p = NULL;} 前提是链表最后的 next被赋值为NULL了,要不然即使 p最后没有数据了还是会执行下去的。 阅读全文
摘要:
1 #include <stdio.h> 2 void main() 3 { 4 int i=4; 5 int*p=&i; 6 //int *p=(int *)4; 7 printf("%d\n",*p); 8 printf("%p\n",p); 9 *p=12;10 printf("%d\n",*p);11 printf("%p\n",p);12 getchar();13 return;14 }当为 int *p=(int *)4的时候会报错。因为 4在内存中没有相应地址映射,p指向的是非法地 阅读全文