Nov 17, 2011 at 2:45pm UTC
hi,
I was experimenting with char Arrays . Found some strange results
Code:-
--------------------
#include <iostream>
#include <cstring>
using namespace std;
int main(){
//Defining and assigning valid char arrays;
char a1[2]={'a'};
char a2[3]={'a','b'};
char a3[4]={'a','b','c'};
char a4[5]={'a','b','c','d'};
char a5[6]={'a','b','c','d','e'};
char a6[7]={'a','b','c','d','e','f'};
char a7[8]={'a','b','c','d','e','f','g'};
char a8[9]={'a','b','c','d','e','f','g','h'};
char a9[10]={'a','b','c','d','e','f','g','h','i'};
char a10[11]={'a','b','c','d','e','f','g','h','i','j'};
//Defining and assigning overflow char arrays;
char aa1[1]={'a'};
char aa2[2]={'a','b'};
char aa3[3]={'a','b','c'};
char aa4[4]={'a','b','c','d'};
char aa5[5]={'a','b','c','d','e'};
char aa6[6]={'a','b','c','d','e','f'};
char aa7[7]={'a','b','c','d','e','f','g'};
char aa8[8]={'a','b','c','d','e','f','g','h'};
char aa9[9]={'a','b','c','d','e','f','g','h','i'};
char aa10[10]={'a','b','c','d','e','f','g','h','i','j'};
//Printing similar arrays on same line;
cout << "\n"
<< a1 << " : " << aa1 << endl
<< a2 << " : " << aa2 << endl
<< a3 << " : " << aa3 << endl
<< a4 << " : " << aa4 << endl
<< a5 << " : " << aa5 << endl
<< a6 << " : " << aa6 << endl
<< a7 << " : " << aa7 << endl
<< a8 << " : " << aa8 << endl
<< a9 << " : " << aa9 << endl
<< a10 << " : " << aa10 << endl;
return 0;
}
Output:-
--------------------
a : aabcdefghij
ab : ab>aabcdefghij
abc : abc
abcd : abcdabc
abcde : abcde
abcdef : abcdefs░
abcdefg : abcdefgÇ
abcdefgh : abcdefghabcdefgÇ
abcdefghi : abcdefghi♣>
abcdefghij : abcdefghij
Question 1. Why does it seems as if the overflow is in a sequence (e.g 1st & 2nd overflow array)?
Question 2. Why no overflow in 3rd & 5th array(3rd & 5th arrays are similar)?