www.digitalmars.com         C & C++   DMDScript  

c++ - free() problem

reply "Freedon" <huleidong foxmail.com> writes:
There is a problem with the free() function,it doesn't work on my computer.
test source code:

#include <stdio.h>
#include <malloc.h>

int main(){
char *p;
getch();
p=(char*)malloc(1024*1024*1024);
printf("size=%u\n",_msize(p));
for (size_t n=0;n<1024*1024*1024;n++)
p[n]='C';
puts("1G,Ok!");
getch();
free(p);
puts("After free!");
//process still use 1G memory
getch();
}


I'm sorry about my poor English,I'm a student in middle school of China. 
Aug 02 2011
parent Bertel Brander <bertel post4.tele.dk> writes:
Den 03-08-2011 04:02, Freedon skrev:
 There is a problem with the free() function,it doesn't work on my computer.
 test source code:

 #include<stdio.h>
 #include<malloc.h>

 int main(){
 char *p;
 getch();
 p=(char*)malloc(1024*1024*1024);
 printf("size=%u\n",_msize(p));
 for (size_t n=0;n<1024*1024*1024;n++)
 p[n]='C';
 puts("1G,Ok!");
 getch();
 free(p);
 puts("After free!");
 //process still use 1G memory
 getch();
 }


 I'm sorry about my poor English,I'm a student in middle school of China.
The free function does most likely work, but once memory has been allocated to your process, the memory is not normally given back to the OS. Try to run the code above in a loop, the memory usage will most probably not increase after the first iteration.
Aug 03 2011