I don’t understand why the compiler does not generate errors during execution in Visual Studio 2010 with C ++ builder 6. I refer to the array element that exceeds its size.

const int dl=2; int bb[dl]; cout<<"razmer "<<sizeof(bb)<<"\n"; for (int i=0;i<dl*5;i++) { bb[i]=i*2; } for (int i=0;i<dl*5;i++) { cout<<bb[i]<<" nomer "<<i<<"\n"; } 
  • one
    Because C ++ compilers by default don't care whether you go beyond the scope or not. Perhaps certain flags like -Wall will generate warn. - ivkremer

4 answers 4

Now, it no longer does anything criminal, except that you are accessing a section of memory that does not belong to you. You can write to it and read it, but there is no guarantee that having written something there, you think the same thing.

  • 3
    Or do not fall. - avp 8:40 pm

By default, C / C ++ compilers do not care whether you go beyond the scope or not.

If you add flags:

 -Wall -O2 

That will display warnings. This is true of g ++, I'm not sure about Visual, but I think

 /Wall /O2 должно сработать. 

    I would like to add to the above, the following quotation from the book: "Any verification of the correctness of access by means of C ++ significantly slows down the execution of the program. Therefore, such actions are left for the programmers to consider."

      It is better to use the dynamic array std::vector it has two ways of accessing the elements:

      • using square brackets [i]
      • using mas.at(i); - duck here at - generates an out_of_range exception if you have applied beyond the limits of the array.