I would like to create a variable length global array so that I can use them in any functions such as main() or anything.
#include <iostream>
int g_t_step[];
int main()
{
g_t_step[1]=1;
std::cout << g_t_step[1];
}
I would like to create a variable length global array so that I can use them in any functions such as main() or anything.
#include <iostream>
int g_t_step[];
int main()
{
g_t_step[1]=1;
std::cout << g_t_step[1];
}
In general, global variables are not the way to go except for logging. It's easy to loose track of which function/method will modify the content of your array and it can become hard to debug as your code base grows. If you give more details about what your are trying to achieve, it will be easier to help you.
By seeing your code sample I'm guessing you're quite new to C++, the code you wrote doesn't compile. The variable g_t_step
as you've defined it here should be initialized as well, for example:
int g_t_step[] = {1, 2, 3};
However, if you want your array to be resizable, you cannot declare it this way because g_t_step
will be of size 3 no matter what you try to do (in my example because there are 3 numbers on the left side). The reason for that is because g_t_step
is allocated on the stack which is defined at compile time.
If you want it to be resizable, it should be allocated on the heap like so:
#include <iostream>
int* g_t_step = nullptr;
int size = 0;
int capacity = 0;
int main() {
g_t_step = new int[10]; // heap allocation
capacity = 10;
g_t_step[0] = 4 // initialize the second element of the array
size = 1;
std::cout << g_t_step[0] << '\n';
return 0;
}
This would be the way to answer your question but it is very minimal, you have to keep track of the current size of your array and the number of elements in it yourself. The easy way is to use STL's std::vector template like so:
#include <iostream>
#include <vector>
// the static part is not necessary if you only have one file
// but is usefull if you define g_t_step in another source file
// which is then linked against the main
static std::vector<int> g_t_step;
int main() {
g_t_step.push_back(1); // initialize the first value...
g_t_step.push_back(2); // second...
g_t_step.push_back(3); // and third...
std::cout
<< g_t_step.size() << ' ' // output 3 because the vector contains 3 numbers
<< g_t_step.capacity() << ' ' // output 4
<< g_t_step[0] << ' ' // output 1
<< g_t_step[1] << ' ' // output 2
<< g_t_step[2] << '\n'; // output 3
}
The class std::vector keeps track of the number of elements (size()) and the size of the array (capacity()) which is always a power of 2 greater than the number of elements (4 in this case).
You could then write something like this:
static std::vector<int> vect;
void add_element(int e) {
vect.push_back(e);
}
int main() {
add_element(4);
add_element(5);
std::cout << vect[0] << ' ' << vect[1] << '\n'; // output: 4 5
}
But again, this is considered bad practice so without more details I wouldn't recommend using that unless it's for a small script.
For more information see: std::vector
You can also look at static variables.
The concept of variable-length array is applicable on C99 but not in C++ standard. In an enhance way, you can take the help of vectors in C++. They have the ability to resize and reallocate the required memory to use them properly.
Assume the following example code and notice the comments:
#include <iostream>
#include <vector>
int main(void) {
// Example 1: Manually resizing the vector
// ---------------------------------------
std::vector<int> myVec;
int n = 100;
myVec.resize(n); // Similar work to that of VLAs
// Example 2: Dynamically managing the size of vector
// --------------------------------------------------
std::vector<int> myVec {10, 20, 30};
std::vector<int> myVec2;
myVec.push_back(10);
myVec.push_back(20);
// After this, the size of the vector will be increased by 2
// since two elements are inserted.
// You can erase() the vector elements and the sizes will be
// automatically reduced.
.
.
return 0;
}
On the other hand, you should not consider declaring the variables globally.
You are not initizing the array size. This will give you error at compile time
As when you write
int a
then a variable "a" gets stored in stack memory with random address with size of int (depend on processor)
but when you write
int a[]
then stack memory needs the size of the array to allocate inside the stack
for eg int a[3]
;
this will aqcuire the size of 3*(size of int) inside the stack memeory
As I dicussed above is static allocation of array which is directly stored inside stack but you can also allocate the array inside heap which is called a dynamic allocation. I think you have to first try the dynamic allocation by yourself