Strange array overflow

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)?
How does the cout know when the char array ends? By finding the zero at the end of every well-formed char array. If there is no zero, as in your arrays, it cannot know when they end and it will keep going until it does find a zero, which will be in some piece of memory off the end of your array. This other memory is random. Could be anything in there. Anything at all. Any patterns you see are fluke.
Last edited on
Thanks
Moschops


Just after your replay I made a few changes for my understanding, and it porved and explaned as you said.


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 but keeping last entry as 0 (zero) char arrays;

char aa1[1]={0};
char aa2[2]={'a',0};
char aa3[3]={'a','b',0};
char aa4[4]={'a','b','c',0};
char aa5[5]={'a','b','c','d',0};
char aa6[6]={'a','b','c','d','e',0};
char aa7[7]={'a','b','c','d','e','f',0};
char aa8[8]={'a','b','c','d','e','f','g',0};
char aa9[9]={'a','b','c','d','e','f','g','h',0};
char aa10[10]={'a','b','c','d','e','f','g','h','i',0};

//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 :
ab : a
abc : ab
abcd : abc
abcde : abcd
abcdef : abcde
abcdefg : abcdef
abcdefgh : abcdefg
abcdefghi : abcdefgh
abcdefghij : abcdefghi


All the overflow arrays stopped at encountering the zero.
Topic archived. No new replies allowed.