How to initialize an array whose size is initially unknown?
Asked Answered
A

3

15

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!

Ashliashlie answered 16/3, 2014 at 3:54 Comment(2)
According to this answer to a similar question, you can't do what you're trying to do. Why not use std::vector instead?Farseeing
You can either use vector or dynamic array, though vector is preferred. Refer to this response https://mcmap.net/q/487771/-creating-array-of-objects-on-the-stack-and-heapOakie
G
13

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
}
Ganny answered 16/3, 2014 at 4:4 Comment(0)
T
10

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);
Trachytic answered 16/3, 2014 at 4:10 Comment(1)
herohuyongtao, do you mean "Size of dynamically created array on the HEAP stack"? I am not fluent in c++, so I easily might be wrong.Stealage
J
0

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.

Jonell answered 16/3, 2014 at 4:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.