C++动态内存分配并保存数据索引

C++动态内存分配并保存数据索引

一、基础例子1

为了快速理解C++动态内存分配空间,我直接用下面例子进行解释:

/*
 *	Author: img_Guo
 *	Date: 2020年7月24日15点17分
 *	Description: 对C++动态内存分配并保存数据索引进行探索
 */
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

/*
 *	Author: img_Guo
 *	Date: 2020年7月24日15点17分
 *	Description: 创建5块int类型的动态内存并保存其地址到索引数组
 */
void InitDynamicMemorySpace(vector<int*> * indexVector)
{
	for (int i = 0; i < 5; ++i)
	{
		// 创建int类型动态内存
		int * a = new int();

		// 保存动态内存地址到索引数组
		indexVector->push_back(a);
	}
}

int main(void)
{
	static vector<int*> * indexVector = new vector<int*>();	// 定义并初始化索引数组

	InitDynamicMemorySpace(indexVector);	// 初始化动态内存

	printf("索引数组地址:%p\n", indexVector);	// 输出indexVector的地址

	for (int i = 0; i < 5; ++i)
		printf("\t索引数组保存的地址%p\n", (*indexVector)[i]);	// 输出索引数组中保存的内存地址值
	
	system("pause");
	return 0;
}

/* 程序运行结果
---------------------------------------
索引数组地址: 01659868
		索引数组保存的地址: 0165CA88
		索引数组保存的地址: 0165CBD0
		索引数组保存的地址: 0165CBA0
		索引数组保存的地址: 01651748
		索引数组保存的地址: 01651778
---------------------------------------

 * 程序总结说明:
1. int * a = new int();
	中 new int() 会返回新开辟的动态内存的数组的指针,并且每次都会新开辟一块内存空间并把地址赋给变量a
2. 动态内存适合跨函数调用
*/

二、基础例子2

为了快速理解C++动态内存分配空间,我直接用下面例子进行解释:

/*
 *	Author: img_Guo
 *	Date: 2020年7月24日15点49分
 *	Description: 对C++动态内存分配并保存数据索引进行探索
 */

#include <iostream>
#include <vector>

using namespace std;

/*
 *	Author: img_Guo
 *	Date: 2020年7月24日15点50分
 *	Description: 结构体负责保存Index的索引值和内容地址
 */
struct IndexStruct
{
	int index;	// 保存对应的数组索引值
	vector<int> * contents;	// 保存对应数组索引值的内容地址
};


int main(void)
{
	static vector<IndexStruct*> * IndexStructVector = new vector<IndexStruct*>();	// IndexStruct指针的数组

	vector<int> * temporary;	// 临时的 vector<int> * 类型

	for (int i = 0; i < 5; ++i)
	{
		// 初始化并赋值
		temporary = new vector<int>();
		temporary->push_back(i + 1);
		temporary->push_back(i + 2);
		temporary->push_back(i + 3);
		temporary->push_back(i + 4);

		// 构造IndexStruct类型
		IndexStruct * temporaryIndexStruct = new IndexStruct();
		temporaryIndexStruct->index = i;
		temporaryIndexStruct->contents = temporary;

		IndexStructVector->push_back(temporaryIndexStruct);	// 存入IndexStructVect

		//delete(temporary);	// Free temporary
		//temporary = NULL;
	}

	// 输出校验
	for (int i = 0; i < 5; ++i)
	{
		cout << "IndexStructVector->index: " << (*IndexStructVector)[i]->index << endl;
		for (int j = 0; j < 4; ++j)
		{
			cout << '\t'
				<< "IndexStructVector->contents["
				<< j
				<< "]: "
				<< (*(*IndexStructVector)[i]->contents)[j]
				<< endl;
		}
	}

	system("pause");
	return 0;
}

/* 程序运行结果
--------------------------------------------------------------------
IndexStructVector->index: 0
		IndexStructVector->contents[0]: 1
		IndexStructVector->contents[1]: 2
		IndexStructVector->contents[2]: 3
		IndexStructVector->contents[3]: 4
IndexStructVector->index: 1
		IndexStructVector->contents[0]: 2
		IndexStructVector->contents[1]: 3
		IndexStructVector->contents[2]: 4
		IndexStructVector->contents[3]: 5
IndexStructVector->index: 2
		IndexStructVector->contents[0]: 3
		IndexStructVector->contents[1]: 4
		IndexStructVector->contents[2]: 5
		IndexStructVector->contents[3]: 6
IndexStructVector->index: 3
		IndexStructVector->contents[0]: 4
		IndexStructVector->contents[1]: 5
		IndexStructVector->contents[2]: 6
		IndexStructVector->contents[3]: 7
IndexStructVector->index: 4
		IndexStructVector->contents[0]: 5
		IndexStructVector->contents[1]: 6
		IndexStructVector->contents[2]: 7
		IndexStructVector->contents[3]: 8
--------------------------------------------------------------------
 * 程序总结说明:
	动态内存开辟之后,空间会一直存在(除非手动释放或系统自动释放)。
	因此需要保存指针指向它,否则会产生内存泄漏。
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值