This doesn't work because your array has the size 2 bytes (2 * size of char). C and C++ do not check for array bounds. This results in you writing past the end of the array.
Because the stack (Please note the illustration on the wiki is upside down in regards to memory addresses) is built from high memory addresses to lower memory addresses but data is written from low to high you will most likely (depending on how your compiler works and it's settings, etc) write into other variables that you have declared before this point, the function parameters, the return pointer or even outside the valid scope of your programm past the end of the stack.
This is undefined behavior and depends heavily on the state of the stack and less so on the input that is written into the array. What will likely happen is a program crash due to the return pointer being overwritten and pointing to some random address which may or may not be inside your program or a segmentation fault if you try and write into protected memory that your program doesn't own.
In the worst case, you have a buffer overflow vulnerability in which an attacker may craft an input that overwrittes the function return address on the stack to make the program execution jump to an address desired by the attacker, usually the stack itself (the array that was just filled with the attackers data) and execute it. The attacker would fill more of the array with some processor instructions to do anything he wants. While modern operating systems prevent this primitive kind of overflow vulnerability it shouldn't happen to begin with. And there are easy workarounds for the more basic measures, such as address randomization.
An array is a number of memory cells that are located one after the other in memory. Each one can hold a character (or whatever the datatype is you have used.)
For example char name[30];
will tell your compiler to reserve 30 cells for characters. name
will be treated as a pointer to a char object that has the address of the first cell in the array(name[0]
). Please note that at this point, random data may be in those cells. When you then enter a string into those cells via cin.getline it may look like this:
['M', 'y', ' ', 'N', 'a', 'm', 'e', '\0', 'f', '&', '\0', '\0', 'y', '\0', ... 'i'] (30 characters total)
C++ and C recognize the end of a string by the first NULL
value. Therefore you must at least have 1 more space in your array for this null value than your maximum expected character limit. In this case, the maximum length of the name is 29 characters because the null-terminator requires 1 extra space and this fills all 30 cells. It is a common fault to go out of bounds by 1 index in loops or by forgetting about the null-terminator. This is hard to debug as it rarely causes crashes but only subtle, most often unreproducable bugs by overwriting variables or part of them.
Fixed array sizes are bad practice for variable input lenghts. You should look into memory allocation and handling pointers for strings. Or you could use the conveniet std::string objects.
Another bad practice is to use magic numbers, like 30 in my example. it's better to define a constant with a strong name and use that instead. For example
const int MAX_NAME_LEN = 30;
If you want to do some experimentation you can declare an array and fill it with a pattern of data like [\xAB, \xAB, \xAB, \xAB, ...]
. When your operating system tosses you the invalid jump address you will see the data pattern in the address of the error message (0xABABABAB) (if you just overwrote the return address on the stack, not written into protected memory).