How will your posts cause memory corruption?
They will cause memory corruption in two ways. Both in the particular and in the abstract.
char *buffer = /*some c string */;sprintf(buffer + strlen(buffer),"%d",i);
No length checking is done above -- so
in the particular, you have memory corruption above. There are c strings that will cause the above chunk of code execute undefined behaviour.
...
In the abstract, if you don't keep the eye on the ball -- keep track of the length of every c string you append data to -- you will cause memory corruption in a project.
You'll even usually get lucky -- memory will be allocated with room between one chunk of memory and the next, and most of the time you will have enough room. Quite often because "common memory corruption" gets weeded out as the application crashes.
But the more you work with blocks of memory
without always keeping track of their length, the more memory corruption you will have.
So
never ever ever make any string longer if you don't know how long it is.
Always check to see if the buffer you are modifying will have enough room to store the string you want to store there.
I find the best way to deal with
never and
always commandments is to touch your data only with functions (ADT), and don't manipulate the data directly.
Hence the Buffer struct, and the functions I provided that start off manipulating them.