指针使用常见错误.docVIP

  • 12
  • 0
  • 约1.62千字
  • 约 3页
  • 2017-08-03 发布于河南
  • 举报
指针使用常见错误: 指针变量未初始化 void main() { int *p;// int *p = NULL; *p = 5; printf(^%d\n, *p); return; } 对指针进行动态内存分配后,要检查是否分配成功 int *p = NULL; p1 = (int *)malloc( N * sizeof(int)); if(NULL == p) return -1; *p = 10; 或者: int *p1 = NULL; if( !(p1 = (int *)malloc( sizeof(int)) ) ) return -1; 3 指针所指内容使用完free掉后,需把指针也置空 = 野指针 对应地,指针置空前要先free = 内存泄露 #include stdio.h #include stdlib.h int main() { //定义p1,并分配动态内存及赋初值 int *p1 = NULL; if( !(p1 = (int *)malloc( sizeof(int)) ) ) return -1; *p1 = 5; //free free(p1); // p1 = NULL; //定义p2,并动态分配内存及赋初值 int *p2 = NULL; if( !(p2 = (int *)mall

文档评论(0)

1亿VIP精品文档

相关文档