So I have this code below in where 'char Example[10]' can contain 10 characters. But if I use cin command and pointer to store the input and then print it out, the program doesnt really care if the input is more than 10 characters long, it stores and prints all the extra characters too (unless it has spaces).
I understand that because it uses a pointer so it then just goes to that address and stores everything without caring the maximum amount of characters.
The question is: Where it actually stores the extra data and can this fuck up or overwrite some data which is preserved to addresses following the actual address?
(Even if this might not be anyhow right-kind of coding, Im still interested to hearing the answer)
I just compiled this on both cpp.sh and visual studio 2013/2015 And it did not store more than 10 characters. I entered around 20, it cut them off and only stored 10, then it printed out only 10.
So I have this code below in where 'char Example[10]' can contain 10 characters. But if I use cin command and pointer to store the input and then print it out, the program doesnt really care if the input is more than 10 characters long, it stores and prints all the extra characters too (unless it has spaces).
The overload for operator<< for this particular case is on char*. There is no size information available to it, so there is no way for it to respect the size of the array.
I understand that because it uses a pointer so it then just goes to that address and stores everything without caring the maximum amount of characters.
The question is: Where it actually stores the extra data and can this fuck up or overwrite some data which is preserved to addresses following the actual address?
The answer is: It results in undefined behavior. Don't use arrays without limiting the input extraction to a size that doesn't end up overrunning the array.