Reputation: 37
As it's probably obvious from my code I'm really new to C language. I'm working on a program which is called from a larger Python program via subprocess.PIPE / cin. My intent was to assign an array of a size directed from Python. Now I realized that I can assign an integer to 1202th block of that array independently of a number I pass to my program. What exactly happens here? Is such array safe to use or is it advisable to use some other feature (I was thinking of 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;
}
Upvotes: 3
Views: 158
Reputation: 8885
int group[ group_num ];
group[ 1202 ] = 233;
This is not safe to do if group_num <= 1202. You are accessing memory that is not allocated to your array which can potentially cause segfault or even worse, randomly corrupt other data of your program.
So if you use int[], you should check that you access indexes in the valid range (i.e less than size). std::vector behaves exactly the same in this aspect if you use the bracket access (e.g. group[0]
) but you can also use .at()
which 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 perhaps more programmer-friendly.
Upvotes: 1
Reputation: 156
is that C or C++ I assume C++ the lightest transform would be...
--int group[ group_num ];
++int group* =new int[ group_num ];
however you should try catch this allocation as group_num could contain anything PS. don't forget to delete
Upvotes: 0
Reputation: 42984
I'd just use C++ STL's std::vector
instead of raw C arrays.
#include <vector>
and then use std::vector
's proper constructor to create a vector of specified size, e.g.
vector<int> group(group_num);
Then you can access (read and modify) the vector item at a given index using operator[]
, just like with raw C arrays.
(If you want bounds checking on the vector index you may consider using std::vector::at()
method, which throws an exception if the vector index is out of bounds.)
Upvotes: 1
Reputation: 63481
I would advise using a vector
, as this is in fact C++ not C. The two languages are distinct and not to be confused.
You really shouldn't assume that the user will enter a group_num
that is 1203 or larger. In fact, you don't check for any input errors so if the user enters "A" you will have serious issues.
Upvotes: 1
Reputation:
Is such array safe to use or is it advisable to use some other feature (I was thinking of vector).
If you use C++, then yes, std::vector
is the data structure you should use.
If you want to understand C-style arrays, though, then you need to remember: T array[N];
declares array
to have N
elements, indexed from 0
to N - 1
. It's undefined behavior to access its element at index N
, since it's out of bounds.
Upvotes: 1
Reputation: 53067
That's not standard C++, it's using an extension from C called "variable length arrays".
In short, don't bother with it. Just use std::vector
if you want a dynamic-size array.
What exactly happens here?
As with every array, if the index is within the bounds of the array it works fine. If not, you'll get undefined behavior. This includes std::vector
's operator[]
.
Upvotes: 1
Reputation: 15278
It is undefined behavior, it can cause your program to do anything. Language guarantees nothing. Do not access array outside of its bounds.
Upvotes: 4