Creating a static array with a variable size? - c++

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.

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.

Defining an array with no initial size?

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/

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).

Dynamic Array in C++ vs ArrayList in Java

I am new to C++. I am working on a problem from class. We must use Dynamic Arrays to read words from a file.
Here are my simple questions.
Does Dynamic Array imply the use of a pointer? Meaning we point at our new array made.
In Java, ArrayList can be created and size changes dynamically. But to me in C++, does using a dynamic array mean to set the size of the array to a large number and then dynamically allocate that array?
Is there no way to chop off the excess indexes from the dynamic array? So then delete the array at the end is the only choice I have in C++?
Thanks, pointers and dynamic arrays can only be used. Thank you.
Java ArrayList is only comparable to C++ Vector. A normal dynamic array does not change it's size at runtime so if you declare a dynamic int array in c++:
int *arr = new int[10];
The size of that array will remain at 10.
However if you use vector:
std::vector<int> arr(10);
Then when this vector is filled past 10 elements, the vector object automatically allocates more memory for itself.
So to answer your question, dynamic array implies making use of a pointer to keep track of the memory location where the heap-allocated array has been placed. But if you use vector, this is handled for you internally
I suppose dynamic array could refer either. Look at what you have to do and decide. Does it make scene to use an array where the size is fixed? I'm guessing that a vector would be the best here since you probably don't know ahead of time how large the file is.
std::vector<string> words;
Also just for your information the vector class simply stores a pointer to a dynamically allocated array. It creates a new one whenever you need more room, copies over the the data from the old array, and the deletes it. This all happens behind the scenes.
The C++ equivalent of a Java ArrayList is std::vector. You use the capitalized term "Dynamic Array" as if it has a specific meaning rather than the generic adjective "dynamic" applied to the concept "array". A "dynamic array" in C++ spec lingo is a local variable that has a fixed size, set when it is initialized/created, but that may get a different size each time the scope is entered (and the array recreated). Since it's local to a scope, its not too useful for storing stuff read from a file other than temporarily. C++ also has heap-allocated arrays which have their size set when created (as with a dynamic array), but which persist until explicitly deleted, which might be more useful. These are "dynamic" in the general adjective sense, but are not "dynamic arrays" in the spec sense.

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