Author |
Message |
wtd
|
Posted: Mon Dec 11, 2006 11:40 pm Post subject: C TYS |
|
|
Consider:
code: | void strcpy(char *dest, char *src)
{
while (*dest++ = *src++);
} |
Implement the smallest change possible to this to get a function with the following signature.
code: | void strcat(char *dest, char *src); |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Mon Dec 11, 2006 11:45 pm Post subject: (No subject) |
|
|
Extra kudos to the programmer who can explain the performance issue with repeated uses of the resulting strcat function, and how it can be remedied. |
|
|
|
|
 |
ericfourfour
|
Posted: Tue Dec 12, 2006 1:06 am Post subject: (No subject) |
|
|
For the extra kudos:
msdn2 wrote: "Because strcat does not check for sufficient space in strDestination before appending strSource, it is a potential cause of buffer overruns." (http://msdn2.microsoft.com/en-us/library/h1x0y282(VS.80).aspx)
If you want to avoid it, here is a useful guide. Here is one quote from that page that might answer the question though:
msdn2 wrote: "Preventing buffer overruns is primarily about writing good code. Always validate all your inputs and fail gracefully when necessary."
I guess you should give the kudos to the person who understands what all of that means though.  |
|
|
|
|
 |
wtd
|
Posted: Tue Dec 12, 2006 1:10 am Post subject: (No subject) |
|
|
Not bad, but the problem I asked about was performance related.  |
|
|
|
|
 |
md

|
Posted: Tue Dec 12, 2006 2:12 am Post subject: (No subject) |
|
|
As there really isn't any ways of checking that the character array is big enough before adding to it without changing the function to accept maximum lengths... you really have to ignore buffer overruns.
Oh, I know the answer; and I almost figured it out entirely on my own before giving in ask asking for the wtd's version  |
|
|
|
|
 |
Monstrosity_
|
Posted: Tue Dec 12, 2006 12:03 pm Post subject: (No subject) |
|
|
wtd wrote: Extra kudos to the programmer who can explain the performance issue with repeated uses of the resulting strcat function, and how it can be remedied.
If you mean calling the function with the same 'dest' repeatedly, then the performance issue would be the need to traverse the character array (which would get increasingly larger with each call) looking for the null character. A way to avoid this is returning something meaningful. |
|
|
|
|
 |
wtd
|
Posted: Tue Dec 12, 2006 1:36 pm Post subject: (No subject) |
|
|
What would be "meaningful"?  |
|
|
|
|
 |
md

|
Posted: Tue Dec 12, 2006 3:24 pm Post subject: (No subject) |
|
|
The end of the character string after concatenation  |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Monstrosity_
|
Posted: Tue Dec 12, 2006 4:16 pm Post subject: (No subject) |
|
|
wtd wrote: What would be "meaningful"? 
Yes, I left that part vague for the same reason I didn't do the actual TYS. Which besides laziness, was that I'll leave it for someone else who wishes to test thier skills ;p. Also, I'm sure when people actually do write the code, they will be able to come up with some more (perhaps less important) performance issues.
md gave a good meaningful return value, I can think of at least one more. Lets see what people can come up with... |
|
|
|
|
 |
[Gandalf]

|
Posted: Tue Dec 12, 2006 9:08 pm Post subject: (No subject) |
|
|
My first, not so good, but working attempt:
c: | void strcat(char *dest, char *src)
{
int len = 0;
while (*(dest+len++) != '\0');
while (*(dest+++sizeof(char)*len-1) = *src++);
} |
Please excuse the lack of whitespace. |
|
|
|
|
 |
wtd
|
Posted: Tue Dec 12, 2006 9:22 pm Post subject: (No subject) |
|
|
Much simpler than that, Gandalf.
Do you understand how the strcpy function works? |
|
|
|
|
 |
Andy
|
Posted: Tue Dec 12, 2006 9:28 pm Post subject: (No subject) |
|
|
tsk tsk gandalf...
c++: |
void strcat(char *dest, char *src)
{
while (*dest++ = (*dest)?*dest:*src++);
}
|
for strings that do not end with a substring of 0s
c++: |
void strcat(char *dest, char *src)
{
while (*dest++ = (*dest) ? *dest: *((src+=1 + (*(dest+1)=0)) -1));
}
|
|
|
|
|
|
 |
wtd
|
Posted: Tue Dec 12, 2006 10:16 pm Post subject: (No subject) |
|
|
Simpler than that.  |
|
|
|
|
 |
Monstrosity_
|
Posted: Tue Dec 12, 2006 10:35 pm Post subject: (No subject) |
|
|
Sorry guys, couldn't help myself
[Gandalf] wrote: My first, not so good, but working attempt:
c: | void strcat(char *dest, char *src)
{
int len = 0;
while (*(dest+len++) != '\0');
while (*(dest+++sizeof(char)*len-1) = *src++);
} |
Please excuse the lack of whitespace.
sizeof(char) == 1
Andy wrote: tsk tsk gandalf...
c++: |
void _strcat(char *dest, char *src)
{
while (*dest++ = (*dest) ? *dest: *((src+=1 + (*(dest+1)=0)) -1));
}
|
I can't speak for C++, but in C we call this undefined behaviour. A variable can only be modified once in an expression. |
|
|
|
|
 |
Monstrosity_
|
Posted: Tue Dec 12, 2006 10:54 pm Post subject: (No subject) |
|
|
uhg, is there no edit button or am I blind?
Anyway, I reread what I wrote and it's not that your modifying a variable twice, it's that your both modifying and reading a variable in the same expression. Sorry about that. |
|
|
|
|
 |
|