简体   繁体   English

如何初始化一个大小最初未知的数组?

[英]How to initialize an array whose size is initially unknown?

Say I have this: 说我有这个:

int x;
int x = (State Determined By Program);
const char * pArray[(const int)x]; // ??

How would I initialize pArray before using it? 在使用之前如何初始化pArray? Because the initial size of the Array is determined by user input 因为Array的初始大小由用户输入决定

Thanks! 谢谢!

Size of dynamically created array on the stack must be known at compile time. 必须在编译时知道堆栈上动态创建的数组的大小。

You can either use new : 你可以使用new

const char* pArray = new char[x];
...
delete[] pArray;

or better to use std::vector instead (no need to do memory management manually): 或者更好地使用std::vector (不需要手动进行内存管理):

vector<char> pArray;
...
pArray.resize(x);

You cannot initialize an array at compile-time if you are determining the size at run-time. 如果要在运行时确定大小,则无法在编译时初始化数组。

But depending on what you are trying to do, a non-const pointer to const data may provide you with what you're going for. 但是根据你想要做的事情,一个指向const数据的非const指针可能会为你提供你想要的东西。

const char * pArray = new const char[determine_size()];

A more complete example: 一个更完整的例子:

int determine_size()
{
    return 5;
}

const char * const allocate_a( int size )
{
    char * data = new char[size];
    for( int i=0; i<size; ++i )
        data[i] = 'a';
    return data;
}

int main()
{
    const char * const pArray = allocate_a(determine_size());
    //const char * const pArray = new char[determine_size()];
    pArray[0] = 'b'; // compile error: read-only variable is not assignable 
    pArray    =  0 ; // compile error: read-only variable is not assignable 

    delete[] pArray;
    return 0;
}

I do agree with others that a std::vector is probably more what you're looking for. 我同意其他人认为std :: vector可能更像你正在寻找的东西。 If you want it to behave more like your const array, you can assign it to a const reference. 如果您希望它的行为更像const数组,则可以将其指定给const引用。

#include <vector>

int main()
{
    std::vector<char> data;
    data.resize(5);

    const std::vector<char> & pArray = data;

    pArray[0] = 'b'; // compile error: read-only variable is not assignable
}

The example you provided attempts to build the array on the stack. 您提供的示例尝试在堆栈上构建阵列。

const char pArray[x];

However, you cannot dynamically create objects on the stack. 但是,您无法在堆栈上动态创建对象。 These types of items must be known at compile time. 必须在编译时知道这些类型的项目。 If this is a variable based on user input then you must create the array in heap memory with the new keyword. 如果这是基于用户输入的变量,则必须使用new关键字在堆内存中创建数组。

const char* pArray = new char[x];

However, not all items need to be created on the heap. 但是,并非所有项都需要在堆上创建。 Heap allocation is normally a lot slower then stack allocation. 堆分配通常比堆栈分配慢很多。 If you want to keep your array on the stack you could always use block based initialization. 如果要将数组保留在堆栈中,可以始终使用基于块的初始化。

#define MAX_ITEMS 100
const char pArray[MAX_ITEMS]

It should be noted that the second option is wasteful. 应该指出的是,第二种选择是浪费的。 Because you can not dynamically resize this array you must allocate a large enough chunk to hold the maximum number of items your program could create. 由于无法动态调整此数组的大小,因此必须分配足够大的块以容纳程序可以创建的最大项目数。

Finally, you can always use data structures provide by C++. 最后,您始终可以使用C ++提供的数据结构。 std::vector is such a class. std :: vector就是这样一个类。 It provides you a good level of abstraction and item are stored in contingent memory like an array. 它为您提供了良好的抽象级别,项目像数组一样存储在偶然的内存中。 As noted by one of the other answers you should use the resize option once you know the final size of your vector. 正如其他一个答案所述,一旦知道了矢量的最终大小,就应该使用调整大小选项。

std::vector<char> pArray;
pArray.resize(X);

The reason for this is every time you add an element to a vector, if it no longer has enough room to grow, it has to relocate all items so they can exist next to one another. 原因是每次向向量添加元素时,如果它不再有足够的空间来增长,它必须重新定位所有项目,以便它们可以彼此相邻存在。 Using the resize method helps prevent vector from having to grow as you add items. 使用resize方法有助于防止向量在添加项目时增长。

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

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