-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;
}
8
  • 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. Commented Mar 23, 2022 at 19:50
  • 2
    It is not static storage at all, but statically-sized automatic storage. Commented Mar 23, 2022 at 19:53
  • 4
    Undefined behavior doesn't automatically mean a crash. Commented Mar 23, 2022 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. Commented Mar 23, 2022 at 19:57
  • 2
    undefined behavior is undefined. Commented Mar 23, 2022 at 20:00

2 Answers 2

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? :-)

Sign up to request clarification or add additional context in comments.

1 Comment

The C and C++ primes is I should not have to pay for what you are not using. I don'
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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.