隐蔽的内存泄露

#include <stdio.h>

#include <stdlib.h>

int main(void){    

  int* p=NULL;  int i;

     p = (int *)malloc(6 * sizeof(int));      

  if (NULL == p) {    //判断是否为空        

    printf("内存分配出错!");        

    exit(1);    

  }  

  for(i=0;i<6;i++)  {   

    p++;  //导致内存泄露   

    *p = i;        

    printf("add=%d,%2d\n",p, *p);  

  }

     if (NULL != p) {    //判断是否为空        

      free(p);    //这句运行时出错   

        p=NULL;    

      }       return 0;

}

来源:http://c.biancheng.net/cpp/html/2752.html

posted @ 2015-10-11 21:26  Spir  阅读(94)  评论(0编辑  收藏  举报