首页 > C++ 函数内动态分配的问题
头像
NaruseShiroha1
编辑于 2020-08-14 15:21
+ 关注

C++ 函数内动态分配的问题

在下面的代码中,函数内手动分配的内存不应该在heap中不会被自动释放吗,为什么最后在主程序中的两个cout为空呢

#include<bits/stdc++.h>
using namespace std;

class test {
	public:
		int val = 1;
}; 

void fun(test* t) {
	t = new test();
	cout << t->val << "  " << t << "\n";
}

void func(char* a)
{
	a = new char('a');
	cout << *a << "  " << a << "\n";
}

int main()
{
	char *a = NULL;
	func(a);
	
	
	test *b = NULL;
	fun(b);
	
	cout << "-----\n";
	cout << *a << "  " << a << "\n";
	cout << b->val << "\n";
	
} 


全部评论

(0) 回帖
加载中...
话题 回帖

相关热帖

近期精华帖

热门推荐