Defining an array with no initial size? - c++

I have a question about declaring arrays as of an unknown size I was under the impression that syntax like this is legal:
char [] word
But when I go to compile it isn't correct. I'm doing a school project on postfix notation and I do exactly known the size of the equation and I always thought that the above syntax is legal and I swear I have used it? Do you have to use the new keyword to allocate memory for an array of unkown size?

C++ array's size must be determined at the compile time, which means it has to be a constant number.
If size is unknown at the compile time, use std::vector.

For arrays of variable size, use std::vector instead.
Refer:
http://www.cplusplus.com/reference/vector/vector/

Related

How come std::array be stored on the stack?

I learned about the stack and the heap recently, and something doesn't make sense.
I read that std::array is stored on the stack even though the count and size of the elements of the size is unknown.
the same way you can't read input of unknown length to the stack from the user.
what am I missing here?
To create an std::array you have to specify in its template (or let the compiler deduce) both the count and the type of the elements stored. That makes the size known at compile time, hence it can be instantiated on the stack.
std::array is nothing but a thin wrapper around C-style arrays. The size is fixed, it cannot grow nor shrink.

Setting an array's size based on user's input in c++ but const not working

I need to set an array's size based on a user's input to cin in c++, and I can't figure out why it won't compile.
int input;
cin >> input;
const int N = input;
int array[N];
Shouldn't this work? I must be missing something.
Shouldn't this work?
It shouldn't.
I must be missing something.
You're missing the fact that the size of an array variable must be a compile time constant. A value provided by the user at runtime cannot possibly be known at compile time.
In order to create an array with a dynamic size, you need to create a dynamic array. Simplest way to do that is to use std::vector.
It is not possible to statically automatically allocate an array with variable size. The size of such an array must be compile-time constant: that is, its size is encoded in your executable, and so must be known to the compiler from reading your source code alone.
The two most common approaches for dynamic arrays are std::vectors, which are a neat little class that handles a lot of this for you, and creating arrays with the new keyword, which is junkier but has its place.
I'd recommend watching The Cherno's video on dynamic arrays here: https://www.youtube.com/watch?v=PocJ5jXv8No. It is a great primer on this topic.

C++ Declaring a 2d array with a size function

I'm trying to declare a 2d array by using the size() method for STL map:
const int size = edge_map.size();//get row and column size
int a[size][size];//nxn matrix
I keep getting a compiler error that the size must be a constant value even though I am declaring it as a constant. Is there any work around for this without having to use a dynamic 2d array?
Static memory allocation for arrays can accept variables as long as the value of the variable can be determined at compile-time. The reason for this requirement is because the compiler must know how much memory to allocate for the array on the stack. If edge_map is what it sounds like (some kind of container which can change sizes throughout its existence), you are not going to be able to do it this way.
If this is not the case, though, and edge_map.size() has a return value which can be determined at compile-time, marking that function as constexpr should allow this code to work.
const means to not change its original (initial) value.
But size must be known at compile time, as the compiler/linker allocates memory for non-local variables (declared out of any function).

Create an N dimensional array, where N is determined at runtime (C++)

I'm encoding N-Dim image cubes into a different image format. I don't know the dimensions of the image until runtime and the library I'm using to read from the original image needs an N-dim array destination buffer as a parameter.
How can I declare such an array in C++? Thanks :)
The short answer is that you cannot declare such an array in C++. The dimensions of an array are part of the type (with a miscellaneous exception that sometimes the value of one of the dimensions can be unknown, for an extern array declaration). The number of dimensions is always part of the type, and the type must be known at compile time.
What you might be able to do instead is to use a "flat" array of appropriate size. For example, if you need a 3x3...x3 array then you can compute 3^n at runtime, dynamically allocate that many int (probably using a vector<int> for convenience), and you have memory with the same layout as an int[3][3]...[3]. You can refer to this memory via a void*.
I'm not certain that it's strictly legal in C++ to alias a flat array as a multi-dimensional array. But firstly the function you're calling might not actually alias it that way anyway given that it also doesn't know the dimension at compile-time. Secondly it will work in practice (if it doesn't, the function you're calling is either broken or else has some cunning way to deal with this that you should find out about and copy).
You can't use array in this case. Array is only for those data whose size and dimension are known at compile time. Try use an array of std::vector instead

Creating a static array with a variable size?

I want to create an array which is not on the heap, but on the stack. The size of my array will be based on the number of lines in a file.
I found this SO post:
Why aren't variable-length arrays part of the C++ standard?
Is there still no way to create an array on the stack where the size is determined by something else, other than a const?
No, you can't create an array on the stack unless its size is known at compile time. Not just const, but compile-time constant.