将std::vector分配给堆上特定内存位置的正确语法
Proper syntax to assign std::vector to a specific memory location on the heap?
我正试图在这两个SO帖子中实现关于将容器分配到堆上特定内存位置的方向:
- 将c++对象,特别是stl容器移动到特定的内存位置
- 在预分配内存中创建对象
然而,我一定是做了一些愚蠢的事情(我是c++的新手),因为我甚至不能让我的代码编译(使用c++ 11,你可以看到下面):
std::vector<int> ints1 = {10, 20, 30};
ints1.reserve(20);
auto adr = &(*ints1.end());
auto *ints2 = new(adr)(std::vector<int>(20);
我得到最后一行expected , or ; before )
的错误,我认为这意味着我的语法是错误的,但是如何?
还有,一旦我修正了这个语法,确保这些对象确实是连续的最好方法是什么?我想我应该确保
&(*ints1.begin()) - &(*ints2.begin) is roughly on the order of sizeof(std::vector<int> v(20));
有更好的方法吗?
(注。我确实意识到我应该分配我将要使用的内存来避免未定义行为)
看起来你在;之前遗漏了a)。应该像这样
auto *ints2 = new(adr)(std::vector<int>(20));
相关文章:
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 内存所有位置 wxWidgets
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 如何在不等待检索的情况下获取C++中的内存位置?
- 内存中类位置的成员是否取决于类成员在类定义中的位置?
- 向量的内存位置不连续
- 如何根据C++在同一内存位置重新初始化 C# 中的对象(还是自动完成)?
- 常量引用的内存位置
- C++强制变量到一个固定的内存位置
- 为什么未初始化的内存位置的值给出 -842150451 的值?
- 为什么存储在内存位置的值会发生变化?
- 在特定内存位置构造 c++ 对象
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 如何识别内存泄漏的位置
- 内存位置出现Microsoft C++异常:std::out_of_range
- 函数,返回变量c++占用的内存位置的大小
- 在尝试使用CUDA分配内存时,我遇到了访问冲突写入位置错误
- delete如何知道对象在内存中的起始位置
- 无法在 Opencv 中显示图像导致内存位置
- 如何修复<程序名称>中的"<内存位置>未处理的异常。Visual Studio 2017 中的访问冲突写入位置<内存位置>"