69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAX_ARRAY_SIZE 170000
struct MyBase
{
void * pUserData;
char pArray[56];
};
void DoTest()
{
printf("sizeof(MyBase):%d\n", sizeof(struct MyBase));
struct MyBase * pContacts[MAX_ARRAY_SIZE];
memset(pContacts, 0, sizeof(pContacts));
/* 循环分配内存 */
int i;
for (i = 0; i < MAX_ARRAY_SIZE; ++i)
{
pContacts[i] = malloc(sizeof(struct MyBase));
}
/* 此时用top命令查看,可以查看进程的内存占用情况 */
printf("DoTest:sleeping...\n");
sleep(5);
/* 循环释放内存 */
for (i = 0; i < MAX_ARRAY_SIZE; ++i)
{
if (pContacts[i] != NULL)
{
free(pContacts[i]);
pContacts[i]= NULL;
}
}
}
int main()
{
while(1)
{
printf("calling DoTest()...\n");
DoTest();
printf("end of calling DoTest()...\n");
/* DoTest里已经把内存都释放了,但用top命令查看,为什么程序所占的内存依然没有减少? */
sleep(5);
}
return 0;
}
char pArray[56];
改成char pArray[57];