tags:

views:

174

answers:

5

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;

}
+6  A: 

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
anon
Why it alerts then when I declare it locally?
@persistent, why *what* when you declare it locally?
JSBangs
@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`
Andrey
Whether local or global it is undefined behavior. You probably notice the local array sooner, because you start over writing other stack variables.
Andrew Stein
@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.
anon
@Andrey I'm interested why the compiler generates alert for the locally declared array whereas in contrast when the same array is declared globally?
@persistent: assuming you're getting a compiler warning, then it entirely depends on how much analysis the compiler does. You shouldn't rely on it, since many bounds errors are difficult or impossible to detect at compile time. The compiler has do draw the line somewhere; in your case, it looks like it will check local arrays, but not global ones. If you really want to know why the line was drawn there, you'd have to ask the compiler writer.
Mike Seymour
@Mike I don't think the compiler will be doing any checking. He's getting a segmentation error in one case, but not in the other, because of the process memory layout.
anon
@Neil: probably; without knowing whether it's a compiler warning or run-time error, we're both just speculating.
Mike Seymour
+1  A: 

its undefined behavior. Anything can happen.

James
Beware the nasal demonshttp://www.catb.org/jargon/html/N/nasal-demons.html
EvilTeach
A: 

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.

Relax, I'm just practicing and learning :)
+2  A: 

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.

doron
A: 

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.)