C / C ++ array - Which assignment did I use?
As you can see from my code, I am really new to the C language. I am working on a program that is called from a larger Python program via subprocess.PIPE / cin. My intention was to assign an array of size directed from Python. Now I figured out that I can assign an integer to the 1202nd block of this array regardless of the number I pass to my program. What exactly is going on here? Is such an array safe to use or is it advisable to use some other function (I was thinking about a vector).
int main()
{
string group_str;
int group_num;
getline (cin, group_str);
stringstream( group_str ) >> group_num;
cout << "Group number" << group_num <<"\n";
int group[ group_num ];
group[ 1202 ] = 233;
for (int i=0; i < 1203 ; i++)
{
cout << group[i] << '\t' << i << endl;
}
return 0;
}
source to share
I just used C ++ STL instead of raw C arrays
and then use the correct constructor to create a vector of the specified size, eg. std::vector
#include <vector>
std::vector
vector<int> group(group_num);
Then you can access (read and change) the element of the vector at the given index using operator[]
, just like with the original C arrays.
(If you wanted bounds checking on a vector pointer you could use a method std::vector::at()
that throws an exception if the vector index goes out of bounds.)
source to share
This is not standard C ++, it uses an extension from C called "variable length arrays".
In short, don't worry about it. Just use std::vector
if you need a dynamically sized array.
What exactly is going on here?
As with any array, if the index is within the array, it works fine. If not, you get undefined behavior. This includes std::vector
operator[]
.
source to share
Is such an array safe to use or is it advisable to use some other function (I was thinking about a vector).
If you are using C ++, then yes, std::vector
there is a data structure that you should be using.
If you want to understand C-style arrays, then you need to remember: T array[N];
declares to array
have N
elements indexed from 0
to N - 1
. This is undefined behavior to access its element by index N
as it is out of bounds.
I would advise using vector
, since this is actually C ++ not C. The two languages ββare different and not confused.
You really shouldn't assume that the user is entering group_num
which is 1203 or greater. In fact, you are not checking for input errors, so if the user enters "A" you are in serious trouble.
source to share
int group[ group_num ];
group[ 1202 ] = 233;
It is unsafe if group_num <= 1202. You are accessing memory that is not allocated to your array, which could potentially cause a segfault or worse, accidentally mess up other data in your program.
So, if you are using int [], you must check that you are accessing indices in a valid range (ie, less than size). std :: vector behaves exactly the same in this aspect if you use parenthesis access (for example group[0]
), but you can also use .at()
that throws an exception in case of invalid access.
In the end it depends on your taste what to use, but the truth is std :: vector is arguably more programmer-friendly.
source to share