标签: allocation

为什么std :: allocator :: construct和std :: allocator :: destroy模板化元素类型?

std::allocatorconstructdestroy成员函数上构造元素的类型参数化:

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.

c++ allocation allocator c++11

8
推荐指数
1
解决办法
1072
查看次数

内存分配由编译器优化

在他的演讲"效率与算法,性能与数据结构"中,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可以在看到分配时优化.

  1. 我希望有一个"真实代码",这个优化是有用的,并且由任何当前的编译器完成
  2. 您是否有任何代码示例,其中不同的分配由anu当前编译器融合?
  3. 你是否理解Chandler Carruth在1:06:47他的讲话中说编译器可以"重复删除"你的分配时的含义?

c++ memory optimization memory-management allocation

8
推荐指数
1
解决办法
1007
查看次数

动态分配C结构?

我想动态分配一个C结构:

typedef struct {
    short *offset;
    char *values;
} swc;
Run Code Online (Sandbox Code Playgroud)

'offset'和'values'都应该是数组,但它们的大小在运行时才会被识别.

如何为我的struct和struct的数组动态分配内存?

c c++ memory allocation

7
推荐指数
5
解决办法
2万
查看次数

插入地图时的内存分配

#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)

c++ memory allocation insert map

7
推荐指数
2
解决办法
2037
查看次数

没有堆栈分配整个程序编译?

如果您编写的应用程序是:

  • 单线程
  • 调用图中没有循环
  • 不使用alloca或VLA

现代整个程序优化编译器可以优化所有堆栈分配(例如GCC,MSVC,ICC)吗?在这种情况下,它似乎应该能够静态地分配所有可能的堆栈空间."整个程序"我的意思是编译器可以访问/ all /源代码(在运行时没有可能的dlopen'ing事物等).

c c++ stack memory-management allocation

7
推荐指数
1
解决办法
450
查看次数

内存通道

问题(背景):

我的目标是一台具有四个内存通道和一个 pcie 卡的机器,我希望尽可能快地将数据传输到它。考虑到该卡理论上可以单程传输大约8GB/s(7.88GB/s),这将导致大约15.8GB/s的吞吐量。看看这个我知道我的最大内存传输速率是 12.8GB/s。即使不考虑实际安装的内存,很明显 PCIe 吞吐量超过了一个内存通道的能力。

为了避免这种情况,我想确保我流到设备的内存块最终在与我收到的频道不同的频道上。

  1. 是否可以在 linux 内核中的特定通道上分配内存,如果可以,要使用的接口是什么?
  2. 无论如何要获得有关物理内存映射到不同通道的信息(哪些地址范围/页面帧在哪个通道上)?
  3. 是否有一个接口来请求将页面移动到/分配到特定框架上?
  4. 如何确保页面在被换出/重新定位之前被固定?(假设我必须这样做,magic_allocate_page_on_channel(); get_user_pages(...)我会在短时间内(理论上)再次换出页面,并且可能会被交换到不同的框架get_user_pages

linux memory memory-management allocation

7
推荐指数
0
解决办法
478
查看次数

如何在C中分配和声明结构数组的3D?

如何在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 arrays struct typedef allocation

7
推荐指数
1
解决办法
1125
查看次数

Java数组在HotSpot中使用内存的准确程度(即多少slop)?

C malloc实现通常不分配所请求的精确内存量,而是使用固定大小的内存运行,例如,使用两个幂的大小,因此1025字节的分配实际上需要一个2048字节的段,丢失1023个字节作为slop.

HotSpot是否对Java数组使用类似的分配机制?如果是这样,分配Java数组的正确方法是什么,这样就没有什么了不起?(例如,数组长度应该是2的幂,还是2的幂,减去一些固定的开销量?)

java arrays memory-management allocation jvm-hotspot

7
推荐指数
1
解决办法
94
查看次数

new []和delete []多少次调用分配和释放内存?

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)

c++ memory constructor allocation

7
推荐指数
1
解决办法
213
查看次数

为什么shrink_to_fit(如果满足请求)会导致重新分配?

给定一个v带有v.size() == 3and的容器v.capacity() == 5,我的理解是v.shrink_to_fit() 可以完成对 的调用,如果是,它会导致v.capacity()变为 3。

然而,这是以重新分配为代价的。

为什么?是否可以释放未使用的内存而不为剩余的内存重新分配一块内存?

可能这个问题的根源在于更原始的命令(如new/deletemalloc/ )如何free工作。

c++ memory-management allocation

7
推荐指数
2
解决办法
109
查看次数