简体   繁体   English

如何通过分配器为int a [10] [10]分配内存

[英]how to allocate memory for int a[10][10] by allocator

I know how to create an 1d array (like a[10] ) through an allocator. 我知道如何通过分配器创建一个1d数组(如a[10] )。 For instance, here is an abstract from cppreference : 例如,这是cppreference的摘要:

#include <memory>
#include <iostream>
#include <string>
int main()
{
    std::allocator<int> a1; // default allocator for ints
    int* a = a1.allocate(10); // space for 10 ints

    a[9] = 7;

    std::cout << a[9] << '\n';
   // the remainder was omitted .....
    return 0;
}

I do not, however, know how to create a 2D array, like int a[10][10] . 但是,我不知道如何创建2D数组,例如int a[10][10] Can someone help me with that, please? 有人可以帮我吗?

int[10][10] is an array type of 10 elements. int[10][10]是10个元素的数组类型。 The element type is int[10] . 元素类型为int[10] So the equivalent of that allocation would be: 因此,该分配的等效项为:

std::allocator<int[10]> a2;
int (*a)[10] = a2.allocate(10);

You could simplify the code with a type alias, eg: 您可以使用类型别名简化代码,例如:

using A = int[10];
std::allocator<A> a2;
A *a = a2.allocate(10);

Note that the cppreference example incorrectly went on to write a[9] = 7; 注意,cppreference示例错误地继续写a[9] = 7; . The allocate function allocates storage but does not create objects in the storage. allocate功能分配存储,但不在存储中创建对象。 (The standard explicitly says this, C++14 table 28). (标准对此明确表示,C ++ 14表28)。 And it is undefined behaviour to use the assignment operator with a left-hand operand that does not designate an object. 将赋值运算符与未指定对象的左侧操作数一起使用是未定义的行为。 You would need to subsequently use placement-new to create objects, before using the assignment operator. 在使用赋值运算符之前,您将需要随后使用newplacement创建对象。 The example has now been fixed to use construct instead of allocate . 现在,该示例已固定为使用construct而不是allocate

You can instead allocate 100 and access using y*10 + x. 您可以改为分配100并使用y * 10 + x进行访问。 This is how the compiler generates the index for a[10][10] 这就是编译器如何为a [10] [10]生成索引

int* a = allocate(100);
a[5*10 + 2] = 9; //a[5][2]

If the size of the array is fixed then you can simply write 如果数组的大小是固定的,那么您可以简单地写

int a[10][10]; 

and allocation will be on the stack for local variables or in the data or bss segment for global variables (depending on whether you initialize the array members or not). 对于本地变量,分配将在堆栈上;对于全局变量,分配将在data或bss段中(取决于是否初始化数组成员)。

If you want to allocate the size of the array dynamically, I would suggest to use std::vector instead of an array. 如果要动态分配数组的大小,建议使用std :: vector而不是数组。 Using the std::allocator you would write 使用std :: allocator可以编写

std::vector<std::vector<int>> a(10, std::vector<int>(10)); 

std::vector also can take a second template parameter to define an allocator different from std::allocator . std::vector也可以采用第二个模板参数来定义不同于std::allocator Note that the memory is not allocated continuously. 请注意,内存不是连续分配的。

A possibility for dynamic sized 2D arrays with continuous array sizes is described here . 在此描述具有连续数组大小的动态大小2D数组的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM