-1

I wrote a code for entering element and displaying the array at the same time. The code works but since char A[4] is static memory why does not it terminate/throw error after entering more than four elements? Code:

#include <iostream>
using namespace std;
void display(char arr[],int n)
{
    for(int i=0; i<n; i++)
    cout<<arr[i]<<" ";
    return;
}

int main()
{
char A[4];
int i=0;
char c;
for(;;)
{
    cout<<"Enter an element (enter p to end): ";
    cin>>c;
    if(c=='p')
        break;
    A[i]=c;
    i++;
    display(A,i);
    system("clear");
}
return 0;
}
  • 6
    `How is static array expanding itself?` its not. `why does not it terminate/throw error after entering more than four elements?` Because the C standard never says it should do that. – tkausl Mar 23 '22 at 19:50
  • 2
    It is not static storage at all, but statically-sized automatic storage. – Ben Voigt Mar 23 '22 at 19:53
  • 4
    Undefined behavior doesn't automatically mean a crash. – Mark Ransom Mar 23 '22 at 19:54
  • 2
    The point of C++ is fast, efficient programs. If the program has to check every array access to ensure the bounds were not violated, this is a not-insignificant expense that must be paid by every correctly written program that does not go out of bounds. You will find `at` functions that DO check bounds in most of the Standard Library containers that support random access. – user4581301 Mar 23 '22 at 19:57
  • 2
    undefined behavior is undefined. – Raildex Mar 23 '22 at 20:00
  • C++ is not a nanny language. It does not protect the program or prevent the program from doing things like reading or writing past the end of an array; rather the language expects the program not to do those things. Doing such is called **undefined behavior** — anything can happen. Does not mean the program will crash, although it could. Does not mean it will email your browser history to your grandmother, although it could. Anything can happen. If you are terribly unlucky, it may even appear to work. – Eljay Mar 23 '22 at 20:08
  • The C and C++ premise is you do not pay for what you are not using. I (the global I) don't need that checks, so why should I pay for this extra check that you (the global you) need to make your life simpler. We do have checked access to containers. You just have to explicitly ask for it so you know that you are paying that extra cost. – Martin York Mar 23 '22 at 20:09
  • Although from a different context, see https://stackoverflow.com/a/6445794 – prapin Mar 23 '22 at 20:50

2 Answers2

4

Writing outside of an array by using an index that is negative or too big is "undefined behavior" and that doesn't mean that the program will halt with an error.

Undefined behavior means that anything can happen and the most dangerous form this can take (and it happens often) is that nothing happens; i.e. the program seems to be "working" anyway.

However maybe that later, possibly one million instructions executed later, a perfectly good and valid section of code will behave in absurd ways.

The C++ language has been designed around the idea that performance is extremely important and that programmers make no mistakes; therefore the runtime doesn't waste time checking if array indexes are correct (what's the point if the programmers never use invalid ones? it's just a waste of time).

If you write outside of an array what normally happens is that you're overwriting other things in bad ways, possibly breaking complex data structures containing pointers or other indexes that later will trigger strange behaviors. This in turn will get more code to do even crazier things and finally, some code will do something that is so bad that even the OS (that doesn't know what the program wants to do) can tell the operation is nonsense (for example because you're trying to write outside the whole address space that was given to the process) and kills your program (segfault).

Inspecting where the segfault is coming from unfortunately will only reveal what was the last victim in which the code is correct but that was using a data structure that was corrupted by others, not the first offender.

Just don't make mistakes, ok? :-)

6502
  • 112,025
  • 15
  • 165
  • 265
1

The code works but since char A[4] is static memory why does not it terminate/throw error after entering more than four elements?

The code has a bug. It will not work correctly until you fix the bug. It really is that simple.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 3
    Or worse, it will appear to "work correctly", when running with a particular set of compile-time options, on a particular compiler, on a particular OS, on Tuesdays when the moon is full -- but then crash or behave in some other unpredictable way when one or more of those circumstances change. It's quite common to have a C++ program that is buggy but the symptoms of the bug are undetectably subtle, until suddenly they aren't. – Jeremy Friesner Mar 23 '22 at 20:04