3 ms·
Not a C programmer, so I'm sure I'm missing a ton of more subtle issues, but some of the immediate questions to ask: * What happens if s or t are longer than 1
by saulrh 8y ago
Not a C programmer, so I'm sure I'm missing a ton of more subtle issues, but some of the immediate questions to ask:
* What happens if s or t are longer than 100 characters?
* What happens if s and t are both longer than 50 characters?
* What happens if no element of s == '\0'? How about t?
* r is allocated on the stack. What happens to the memory pointed at by r when you call another function after calling combine? Say you wanted to combine three strings; could you call combine twice to build up the result?
- grenoire 8y agoBack in the day it was pretty much granted you ended your strings with a \0.
- daemin 8y agoFor the first two points, you end up overwriting the stack and most likely crash. This is how security holes were/are made. For the third point, you have two main ways of storing strings in general. The first was called pascal style, where the length is stored first in either a byte or two and then the string data with no (or an optional) terminating null character (the '\0'). The second is referred to as c style, and is done by storing the string in a memory and denoting the end of the buffer by a null character. The c-style enables certain nicer looking c code with loops and such, but it is more dangerous and potentially expensive if you recompute the length all the time. You can always mix and match the way you store the string, such as in C++ where the string class stores a length as well as a null terminated string in a buffer. For your forth point, yes, you end up corrupting the string leading to more crashes and/or it can be used as an entry point to screw around with your program's stack.