c++ - initialize object array on the heap

Follow our previous discussion of how to initialize the object array in this post - c++ - object array intializer in c++ , We said that the if we use the new operator to allocate objects array on the heap, there is no way to do the initialization except for the default constructors, well, here is some tricks that you can do to make the objects array allocated on the heap to have the non-default constructor called. 

 

the trick is to use the placement new operator.  (you may refer to this post - c++ - a sample class to show common aspects of object initialization/uninit on the definition of Account class, you  may need to add yet another memer methods).

 

here is the code. 

 

 

/**
* file 
*   heap_array_initializer.cpp
* description: 
*   THis shows some way to initialize an array of classes in the heap 
*/

#include "stdafx.h"
#include "acct.h"
#include <iostream>
#include <cstddef>
#include <new>  // include this header to use the placement new operator.
#include <utility> // what willl use use this for? abort()???
#include <vector>

using std::cout;
using std::endl;
using std::cerr;
using std::vector;
using std::pair;

typedef pair<char *, double > value_pair;

/** init_heap_array() 
*
*  declared as a static member function
*  provides for allocation and initialization
*  of heap array of class objects
*  init_values: init value pair for array elements
*  elem_count: number of elements in array if 0, array is size of init_value vector
*/

Account * 
	Account::init_heap_array(vector<value_pair> &init_values, 
	vector<value_pair>::size_type elem_count = 0) 
{

	vector<value_pair>::size_type vec_size = init_values.size();

	if (vec_size == 0 && elem_count == 0) { 
		return 0;
	}

	// size of the array to allocate is either elem_count 
	// or, if elem_count is 0, the size of vector..
	size_t elems = elem_count ? elem_count : vec_size;

	//grab a chunk of raw memory to stoe array
	char * p = new char [sizeof(Account) * elems];

	// individuallky initialize each array elements
	int offset = sizeof(Account);
	for (int ix = 0; ix < elems; ++ix) {
		// offset to the ixth elements,
		// if an initial value pair is provided
		//   pass that pair to the constructor
		// otherwise, invoke the default constructor 
		if (ix < vec_size) { 
			new (p+offset * ix) Account(init_values[ix].first, init_values[ix].second);
		}
		else new (p+offset * ix) Account;
	}

	// ok: elements allocated and initialized
	//    retur pointer to first element
	return (Account *) p;

}
MySQL 提供了多种初始化命令参数,用于在安装和配置过程中创建数据目录、生成系统表以及设置初始权限。以下是 `--initialize`、`--initialize-insecure` 和 `--initialize --console` 三者的区别与适用场景: ### `--initialize` 该参数用于执行默认的初始化过程,包括生成数据目录、系统表以及为 `root` 用户生成一个临时密码。这个临时密码会在控制台输出,用户需要记录下来以便后续登录 MySQL 服务器时使用。这种方式更适用于生产环境或对安全性要求较高的场景,因为它强制使用临时密码进行首次登录,提升了初始访问的安全性[^2]。 例如: ```bash mysqld --initialize ``` ### `--initialize-insecure` 与 `--initialize` 不同,`--initialize-insecure` 参数在初始化时不为 `root` 用户生成临时密码。这意味着用户可以在不提供密码的情况下直接登录 MySQL 服务器,这对于测试环境或快速部署非常有用。然而,这种方式存在一定的安全隐患,因此不推荐在生产环境中使用[^3]。 例如: ```bash mysqld --initialize-insecure ``` ### `--initialize --console` 该参数组合用于将初始化过程的详细输出直接打印到控制台,而不是写入日志文件。这种方式便于用户实时查看初始化过程中的状态信息,适合调试或需要即时反馈的场景。需要注意的是,`--console` 并不会影响初始化的安全性设置,因此可以与 `--initialize` 或 `--initialize-insecure` 一起使用以获得更直观的输出[^2]。 例如: ```bash mysqld --initialize --console ``` ### 总结 - **安全性**:`--initialize` 提供了更高的安全性,因为它生成了临时密码;而 `--initialize-insecure` 不生成密码,适合测试环境。 - **输出方式**:`--console` 用于将初始化信息输出到控制台,方便调试。 - **适用场景**:根据具体需求选择合适的参数组合,例如在生产环境中使用 `--initialize --console` 以确保安全并获得实时反馈,在测试环境中使用 `--initialize-insecure --console` 以简化登录流程[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值