3

Why the array is not overflowed (e.g. error alert) when the array is declared globally, in other why I'm able to fill it with unlimited amount of elements (through for) even it's limited by size in declaration and it does alert when I declare the array locally inside the main ?

char name[9];


int main(){


    int i;


    for( int i=0; i<18; ++i){
    cin>>name[i];


    }

    cout<<"Inside the array: ";
    for(i=0; i<20; i++)
    cout<<name[i];

    return 0;

}

5 Answers 5

6

C++ does not check bounds errors for arrays of any kind. Reading or writing outside of the array bounds causes what is known as "undefined behaviour", which means anything could happen. In your case, it seems that what happens is that it appears to work, but the program will still be in an invalid state.

If you want bounds checking, use a std::vector and its at() member function:

vector <int> a( 3 );   // vector of 3 ints
int n = a.at( 0 );     // ok
n = a.at( 42 );        // throws an exception
Sign up to request clarification or add additional context in comments.

9 Comments

Why it alerts then when I declare it locally?
@persistent, why what when you declare it locally?
@persistent you declare them locally if you need local array variable. what Neil says is valid for all arrays in c/c++. Consider using std::vector
Whether local or global it is undefined behavior. You probably notice the local array sooner, because you start over writing other stack variables.
@persistent That's the wonder of undefined behaviour - the explanation is probably to do with the internal memory layout of your program, but that is really not important.
|
2

C++ does not have array bounds checking so the language never check to see if you have exceeded the end of your array but as others have mentioned bad things can be expected to happen.

Global variables exists in the static segment which is totally separate from your stack. It also does not contain important information like return addresses. When you exceed an array's boundaries you are effectively corrupting memory. It just so happens that corrupting the stack is more likely to cause more visible bad things than corrupting the data segment. All of this depends on the way your operating system organizes a process's memory.

Comments

1

its undefined behavior. Anything can happen.

Comments

0

You cannot assume too much about the memory layout of variables. It may run on your computer with these parameters, but totally fail when you increase your access bounds, or run that code on another machine. So if you seriously want to write code, don't let this become a habit.

Comments

0

I'd go one step further and state that C/C++ do not have arrays. What they have is array-like syntactic sugar that is immediately translated to pointer arithmetic, which cannot be checked, as pointers can be used to access potentially all of memory. Any checking that the compiler may manage to perform based on static sizes and constant bounds on an index is a happy accident, but you cannot rely on it.

Here's an oddity that stunned me when I first saw it:

int a[10], i;
i = 5;
a[i] = 42;  // Looks normal.
5[a] = 37;  // But what's this???
std::cout << "Array element = " << a[i] << std::endl;

But the odd-looking line is perfectly legal C++. This example emphasizes that arrays in C/C++ are a fiction.

Neil Butterworth already commented on the benefits of using std::vector and the at() access method for it, and I cannot second his recommendation strongly enough. (Unfortunately, the designers of STL blew a golden opportunity to make checked access the [] operators, with the at() methods the unchecked operators. This has probably cost the C++ programming community millions of hours and millions of dollars, and will continue to do so.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.