fgets() reads a max of num-1 bytes, so in this case, it only reads up to 20 bytes.
fgets() reads a max of num-1 bytes, so in this case, it only reads up to 20 bytes.
but fgets() also adds an end of string char, so again, it's writing past the end of the array, by one char.
In Debug mode, Visual C++ puts 4 special chars before and after the memory you allocated (0xFD I think), and if it sees that those chars have changed, it knows you wrote past the end (or beginning) of your buffer. If you step through in debug mode and look at the memory address for your pointer you should see something like:
If you execute one line at a time and look at the memory after each step, you might see 1 or more of the FD's turn into something else. That would be your bug.Code:FD FD FD FD 34 38 54 43 ... FD FD FD FD
Another, slightly more advanced method would be to add a data breakpoint to &originalExtension + sizeof(originalExtension), so that when the memory beyond originalExtension is modified, it will break. Handy stuff.
This only applies to when using new, however. All stack variables are filled with 0xCD I believe, but no bytes before or after. That only applies to new.