一段有关内存分配的C++代码问题
  • 板块学术版
  • 楼主fuyuzhe2007
  • 当前回复6
  • 已保存回复8
  • 发布时间2024/12/13 20:51
  • 上次更新2024/12/13 23:43:05
查看原帖
一段有关内存分配的C++代码问题
414521
fuyuzhe2007楼主2024/12/13 20:51

这段代码运行后会跳出“程序已停止工作”的提示框运行结果

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

class A{
public:
	int* xxx;
	A(int l){
		int* xxx=new int;
		*xxx=5;
	}
	~A(){
		delete xxx;
	}
};

class B{
public:
	A* head;
	B(int c,int r){
		head=(A*)operator new(c*sizeof(A));
		for(int i=0;i<c;i++){
			new(head+i) A(r);
			printf("%d:%d\n",i,*(head[i].xxx));
		}
	}
	~B(){
		delete[] head;
	}
};

int main(){
	B b(3,3);
	return 0;
}
2024/12/13 20:51
加载中...