Memory Leak is a well-known kind of bug in C/C++. When a string is longer than expected, it will visit the memory of next array which will cause the issue and leak some information. You can see a simple example bellow:
As we see, if the length of the input string is equal to or larger than the limit length of array, the extra part won’t be stored and the information of next array will be leaked out while outputting. The output will stop until a ‘\0’ character (the symbol of end of a string) is found. In this problem, there will never be unexpected end of the program and the last array won’t leak other information.
Source code given as follow:
Now a simpler source code will be given. What will the output be?
Multiple test cases, the first line is an integer T (T <= 20), indicating the number of test cases.
The first line of each test case contains a non-empty string, the definition of strings, formatted as “char s1[s1_len], s2[s2_len]...;”. “char ” is the type of the array which will never change. s1, s2... is the name of the array. s1_len, s2_len... is the length limit. If nothing goes wrong, the array should be able to store the input string and a ‘\0’. The definitions of different arrays will be separated by a comma and a space. The length limits are positive and the Sum of length limit will be less than 10000.
Then, there will be several lines of string which consists two or three parts.
The first part is “gets” or “cout”, the second part will a string s, indicates the name of the array. s will contains only lower case letters and number digits and start with letters. s will be different in one case. If the first part is “gets”, then there will be the third part, a string which should be input into array s, the length of input string will be less than 1000 and contains only visible ASCII characters. “gets” operation will rewrite the array no matter what was in the array before and add a ‘\0’ after the string. Different parts are separated by a space.
Case ends with “return 0;”
The whole input file is less than 10MB.
For each “cout”, you should output a line contains what will actually output, which means you should consider the issue of memory leak. If the requested array is empty, you should output an empty line.
3 char a, b, c; gets a C++ gets b Hello, world! gets c guys cout a cout b cout c return 0; char a; cout a gets a 233 gets a 2333 cout a gets a 12345 cout a return 0; char str1, str2, str3; gets str1 welcome gets str2 to gets str3 Shandong cout str1 return 0;
C++ Helloguys guys 2333 12345 welcometoShand