範例
int
main(
int
argc,
char
* argv[])
{
int
*a =
static_cast
<
int
*>(malloc(
sizeof
(
int
)));
delete
a;
int
*b =
new
int
;
free(b);
return
0;
}
解決方案
請利用 free() 來解除配置 malloc() 所配置的記憶體,利用 "delete" 來解除配置 "new" 所配置的記憶體。
int
main(
int
argc,
char
* argv[])
{
int
*a =
static_cast
<
int
*>(malloc(
sizeof
(
int
)));
free(a);
int
*b =
new
int
;
delete
b;
return
0;
}