0

I know that arrays need to be defined with a size that cannot be modified, but in this code, the counter i exceeds the size of the array (because of the 2 in "i<sz+2" in the for loop) but the code don't give any errors, why? another question: is it correct to use std::cin to assign the value to sz or should I assign the value of sz when I declare the variable?

#include <iostream>

int main() {
    int sz;
    std::cin >> sz;
    int v[sz];
    std::cout << "i" << " " << "v[i]" << std::endl;
    for(int i=0;i<sz+2;i++){
        std::cin >> v[i];
        std::cout << i << " " << v[i] << std::endl;
    }
    return 0;
}
Fabio
  • 61
  • 5
  • Your array isn't valid to begin with. See [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Some programmer dude Mar 24 '22 at 15:39
  • Besides that, C++ doesn't have any bounds checking for arrays. It's your responsibility as the programmer to make sure your code doesn't go out of bounds. – Some programmer dude Mar 24 '22 at 15:39
  • On a totally unrelated note, why `std::cout << "i" << " " << "v[i]" << std::endl;` instead of the more natural `std::cout << "i v[i]" << std::endl;`? – Some programmer dude Mar 24 '22 at 15:40
  • I was focused on the size of the array, you are right... – Fabio Mar 24 '22 at 15:43
  • But, if as you said "C++ doesn't have any bounds checking for arrays" and the code works fine, why is my code wrong when it exceeds the dimension? – Fabio Mar 24 '22 at 15:45
  • 2
    A C++ programmer should be aware of Undefined Behavior - what it is, what it looks like, why it exists. Your question and comments seem to be revolving around that concept. – Drew Dormann Mar 24 '22 at 15:47
  • 2
    @fabio Undefined behavior includes the possibility of "works fine" (as well as any other possibility). – Daniel Langr Mar 24 '22 at 15:48
  • @Fabio **Undefined behavior** means anything can happen *including but not limited to* the program giving your expected output. But never rely on the output of a program that has undefined behavior. – Jason Mar 24 '22 at 15:52
  • For a great explanation of undefined behaviour, read Eric Lippert's answer to [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Fabio says Reinstate Monica Mar 24 '22 at 15:54
  • 1
    The sad part is that std::vector (with insert) is made for this and there are very few situations where it should not be used and still variable-length arrays are being taught. – stefaanv Mar 24 '22 at 16:29
  • 1
    Does this answer your question? [Why don't I get a segmentation fault when I write beyond the end of an array?](https://stackoverflow.com/questions/12410016/why-dont-i-get-a-segmentation-fault-when-i-write-beyond-the-end-of-an-array) – BoP Mar 24 '22 at 16:42

1 Answers1

2

the counter i exceeds the size of the array (because of the 2 in "i<sz+2" in the for loop) but the code don't give any errors, why?

Going out of bound of the array(as you're doing) is undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.


Additionally, in Standard C++ the size of an array must be a compile time constant. So when you wrote:

int sz;
std::cin >> sz;
int v[sz]; //NOT STANDARD C++

The statement int v[sz]; is not standard C++ because sz is not a constant expression.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

Jason
  • 36,170
  • 5
  • 26
  • 60