内存泄露

来源:互联网 发布:java snmp编程 编辑:程序博客网 时间:2024/04/26 17:47
#include <windows.h>
#include <time.h>
#include <math.h>
#include <conio.h>
#include <iostream>
using namespace std; 
void GetMemory(char **p, int num)
{
    *p = (char *)malloc(num);
}
void main(void)
{  
    char *str = NULL;
    while(1)
    {
        GetMemory(&str, 100000);
        strcpy(str, "hello");
        printf(str);
        free(str);//如果此处不释放内存,则每次调用函数都产生内存块
        //从而使内存泄露,最后导致内存溢出
    }
}