开发者社区> 问答> 正文

c 语言内存泄露 (while循环中的malloc)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
typedef struct{
    char a[20];
        char b[100];
        char c[150];
}test;
 
int main()
{
 
      test *p2 = (test *)malloc(sizeof(test)*100);
      if(p2 = NULL)
      {
         printf("malloc p2 error");
         exit(1);
      }
  int a;
  while(1)
  {
      test *p2 = (test *)malloc(sizeof(test)*100);
      if(p2 = NULL)
      {
         printf("malloc p2 error");
         exit(1);
      }
      sleep(0.5);
       
      if(p2 != NULL)  
      {
         free(p2);
         p2 = NULL;
   }
       
 
  }
 
  return 0;

展开
收起
a123456678 2016-06-14 11:16:19 2567 0
1 条回答
写回答
取消 提交回答
  • #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
     
    typedef struct{
        char a[20];
            char b[100];
            char c[150];
    }test;
     
    int main()
    {
     
      while(1)
      {
          test *p2 = (test *)malloc(sizeof(test)*100);
          if(p2 = NULL)
          {
             printf("malloc p2 error");
             exit(1);
          }
          sleep(0.5);
           
          if(p2 != NULL)  
          {
             free(p2);
             p2 = NULL;
       }
           
     
      }
     
      return 0;
    }
    
    2019-07-17 19:37:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
内存取证与IaaS云平台恶意行 为的安全监控 立即下载
云服务器ECS内存增强型实例re6全新发布 立即下载
低代码开发师(初级)实战教程 立即下载