std::allocator的construct和destroy成员函数上构造元素的类型参数化:
template<class T>
class allocator
{
public:
typedef T value_type;
typedef T* pointer;
template<class U, class... Args>
void construct(U *p, Args&&... args);
template<class U>
void destroy(U *p);
...
};
Run Code Online (Sandbox Code Playgroud)
这是什么理由?为什么他们不采取value_type*或pointer?似乎allocator<T>应该只知道如何构造或销毁类型的对象T.
在他的演讲"效率与算法,性能与数据结构"中,Chandler Carruth谈到了在C++中需要更好的分配器模型.当前的分配器模型侵入了类型系统,因此很难在许多项目中工作.另一方面,Bloomberg分配器模型不会入侵类型系统,而是基于虚函数调用,这使得编译器无法"看到"分配并对其进行优化.在他的演讲中,他谈到编译器重复删除内存分配(1:06:47).
我花了一些时间来找到一些内存分配优化的例子,但我发现这个代码示例,在clang下编译,优化掉所有的内存分配,只返回1000000而不分配任何东西.
template<typename T>
T* create() { return new T(); }
int main() {
auto result = 0;
for (auto i = 0; i < 1000000; ++i) {
result += (create<int>() != nullptr);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
以下论文http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html也说分配可以在编译器中融合,似乎表明一些编译器已经做了那种排序东西的.
由于我对高效内存分配的策略非常感兴趣,我真的很想理解为什么Chandler Carruth反对Bloomberg模型中的虚拟调用.上面的例子清楚地表明,clang可以在看到分配时优化.
我想动态分配一个C结构:
typedef struct {
short *offset;
char *values;
} swc;
Run Code Online (Sandbox Code Playgroud)
'offset'和'values'都应该是数组,但它们的大小在运行时才会被识别.
如何为我的struct和struct的数组动态分配内存?
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <utility>
#include <algorithm>
void * GetMemory(size_t n) {
void *ptr = malloc(n);
printf("getMem n %d ptr 0x%x\n", n, reinterpret_cast<unsigned int> (ptr));
return ptr;
}
void FreeMemory(void *p) {
free(p);
}
void* operator new (size_t n) {
void *p = GetMemory(n);
return p;
}
void* operator new [] (size_t n) {
void *p = GetMemory(n);
return p;
}
void operator delete (void *p) {
FreeMemory(p);
} …Run Code Online (Sandbox Code Playgroud) 如果您编写的应用程序是:
现代整个程序优化编译器可以优化所有堆栈分配(例如GCC,MSVC,ICC)吗?在这种情况下,它似乎应该能够静态地分配所有可能的堆栈空间."整个程序"我的意思是编译器可以访问/ all /源代码(在运行时没有可能的dlopen'ing事物等).
我的目标是一台具有四个内存通道和一个 pcie 卡的机器,我希望尽可能快地将数据传输到它。考虑到该卡理论上可以单程传输大约8GB/s(7.88GB/s),这将导致大约15.8GB/s的吞吐量。看看这个我知道我的最大内存传输速率是 12.8GB/s。即使不考虑实际安装的内存,很明显 PCIe 吞吐量超过了一个内存通道的能力。
为了避免这种情况,我想确保我流到设备的内存块最终在与我收到的频道不同的频道上。
magic_allocate_page_on_channel(); get_user_pages(...)我会在短时间内(理论上)再次换出页面,并且可能会被交换到不同的框架get_user_pages)如何在C中分配和声明结构的3D数组?你首先分配数组还是声明它?我觉得你必须先分配它,这样你就可以声明它在堆上了,但那么你如何分配尚未制作的东西呢?另外,您应该一次性还是逐个元素地分配它?我也正确地将结构放入数组中?我对如何做的猜测是:
header.h
struct myStruct{
int a;
int b;
};
typedef struct myStruct myStruct_t;
Run Code Online (Sandbox Code Playgroud)
main.c中
#include "header.h"
#include <stdio.h>
#include <stdlib.h>
int main(void){
int length=2;
int height=3;
int width =4;
myStruct_t *elements;
struct myStruct arr = (*myStruct_t) calloc(length*height*width, sizeof(myStruct);
//zero based array
arr[length-1][height-1][width-1];
int x=0;
while(x<length){
int y=0;
while(y<height){
int z=0;
while(z<depth){
arr[x][y][z].a=rand();
arr[x][y][z].b=rand();
z++;
}
y++;
}
x++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) C malloc实现通常不分配所请求的精确内存量,而是使用固定大小的内存运行,例如,使用两个幂的大小,因此1025字节的分配实际上需要一个2048字节的段,丢失1023个字节作为slop.
HotSpot是否对Java数组使用类似的分配机制?如果是这样,分配Java数组的正确方法是什么,这样就没有什么了不起?(例如,数组长度应该是2的幂,还是2的幂,减去一些固定的开销量?)
在C++,每次new []使用或delete []使用时,每个人分配或释放内存的次数是多少?我的问题更具体地说是在具有各自构造函数和析构函数的类上使用它们.
如,采取以下课程:
#include <iostream>
class Cell
{
public:
Cell() : _value(2)
{
std::cout << "Cell being made!\n";
}
~Cell()
{
std::cout << "Cell being freed!\n";
}
const int& getVal() const
{
return _value;
}
private:
int _value;
};
Run Code Online (Sandbox Code Playgroud)
现在,假设需要一个类类型的数组,并new[]使用,如下所示
Cell* cells = new Cell[5];
Run Code Online (Sandbox Code Playgroud)
当在可执行文件或程序中运行时,我也看到以下打印到stdout:
Cell being made!
Cell being made!
Cell being made!
Cell being made!
Cell being made!
Run Code Online (Sandbox Code Playgroud)
随后当指针delete[]被调用时cells,我看到:
Cell being freed!
Cell being …Run Code Online (Sandbox Code Playgroud) 给定一个v带有v.size() == 3and的容器v.capacity() == 5,我的理解是v.shrink_to_fit() 可以完成对 的调用,如果是,它会导致v.capacity()变为 3。
然而,这是以重新分配为代价的。
为什么?是否可以释放未使用的内存而不为剩余的内存重新分配一块内存?
可能这个问题的根源在于更原始的命令(如new/delete和malloc/ )如何free工作。
allocation ×10
c++ ×7
memory ×5
c ×3
arrays ×2
allocator ×1
c++11 ×1
constructor ×1
insert ×1
java ×1
jvm-hotspot ×1
linux ×1
map ×1
optimization ×1
stack ×1
struct ×1
typedef ×1