How to initialize an array whose size is initially unknown?

自闭症网瘾萝莉.ら 提交于 2019-12-07 07:49:33

问题


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? Because the initial size of the Array is determined by user input

Thanks!


回答1:


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 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. If you want it to behave more like your const array, you can assign it to a const reference.

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



回答2:


Size of dynamically created array on the stack must be known at compile time.

You can either use new:

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

or better to use std::vector instead (no need to do memory management manually):

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



回答3:


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.

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++. std::vector is such a class. 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.



来源:https://stackoverflow.com/questions/22432755/how-to-initialize-an-array-whose-size-is-initially-unknown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!