Buffer Overrun Question

Good day everyone,

Could anyone explain to me why a buffer overrun does not happen here?
The char word was defined to use only 4 characters but later on you can add it characters larger than that. Just adding it on "SIZE+X".

Thank you. Your help is appreciated.


#include<iostream>
#include<iomanip>

using namespace std;

int main ()
{
const int SIZE=5;
char word[SIZE];

cout<< "Enter a word larger than 4 characters to cause an Buffer Overrun:";

cin.getline(word,SIZE+3); //Avoids Buffer Overrun
cout<< "You entered " <<word <<endl;

return 0;
}
You actually are having a buffer overrun -- only, your program doesn't do anything that it makes a difference for.

Remember, the word[] array is stored on the local stack. And, your compiler has a lot of leeway deciding how to use memory, so you might not even see a problem in a small contrived example.

The problem is that you do not know when careless buffer overruns will bite you based upon a whole bunch of compile and partitioning issues working from the way your compiler built it all the way to how your OS manages memory.

Hope this helps.
Thanks Duoas.

It helps. I understand your point.
Topic archived. No new replies allowed.