【整理】C/C++多维数组初始化析构,char **初始化,析构

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】C/C++多维数组初始化析构,char **初始化,析构

项目中实例代码如下:

///取消订阅行情
void MdSpi::UnSubMarket(list<string> *l_instrument) {
	list<string>::iterator itor;
	char **instrumentID = new char *[l_instrument->size()];
	int size = l_instrument->size();
	int i = 0;
	const char *charResult;
	for (itor = l_instrument->begin(), i = 0; itor != l_instrument->end(); itor++, i++) {
		cout << *itor << endl;
		charResult = (*itor).c_str();
		instrumentID[i] = new char[strlen(charResult) + 1];
		strcpy(instrumentID[i], charResult);
	}
	USER_PRINT(this->mdapi);
	this->mdapi->UnSubscribeMarketData(instrumentID, size);

	// 析构字符串数组
	for (i = 0; i < size; i++) {
		delete[]instrumentID[i];
	}
	delete[]instrumentID;

	// 取消订阅列表里清空
	for (itor = l_instrument->begin(); itor != l_instrument->end();) {
		(*itor).clear();
		itor = l_instrument->erase(itor);
	}
}

从中可以抽取出多维数组的初始化析构如下:

初始化:

	int  **pArr=new int*[5];
	for(int k=0;k<5;k++)
		pArr[k] = new int[5];

析构:

	for(i=0;i<5;i++)
		delete []pArr[i];
	delete []pArr;

 

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】C/C++多维数组初始化析构,char **初始化,析构

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: