在C/C++中是否有一个函数可以摆脱非动态数据,类似于动态分配内存的函数free();
。在C++中释放非动态内存
我尝试使用功能free(i);
但是编译器报错:
invalid conversion from 'int' to 'void*' [-fpermissive]
在下面的代码我用free(&i);
,编译器没有报告错误,但也并没有释放内存。
#include<iostream>
#include<stdlib.h>
int main()
{
int i, n;
cin >> n;
for(i = 0; i < n; i++);
cout << i << endl;
free(&i);
cout << i << endl;
return 0;
}
对于输入15
输出是:
15
15
Process returned 0 (0x0) execution time : 11.068 s
Press any key to continue.
而且我从编译器得到了一个警告:
warning: attempt to free a non-heap object 'i' [-Wfree-nonheap-object]