strcat

Started by
6 comments, last by Zahlman 17 years ago
Hi: I want to write a function of strcat. the code as fellow. but i have a question.if the capacity of firsttr is smaller than the capacity between firststr and secondstr.how to deal with it clearly?

char* StrContact(char *firststr, const char *secondstr)
{
	char *tempstr;

	int i, j;
	i = strlen(firststr);
	j = strlen(secondstr);
	tempstr = firststr;
	while(*tempstr != '\0')
	{
		tempstr++;
	}

	while(*secondstr != '\0')
	{
		*tempstr = *secondstr;
		secondstr++;
		tempstr++;
	}

	return firststr;
}
for example: char a[6] = "fd34"; StrContact(a, "world!"); cout << a << endl; how to deal with the it automatically? thanks!
Advertisement
malloc() a new string of the needed size and return it. Of course, the caller has to free() it.

BTW, it's "as follows" and "StringConcatenate".
Then maybe you'd have a better time implementing strncat:

char* StrConcatN(char *firststr, const char *secondstr, int buffer_size);

May I ask why you're reinventing C standard library functions? Every compiler provides a good, working version of strncat (and strcat, if it ever was 'working'). Moreover, the C++ standard template library renders this kind of stuff virtually useless, if you're using C++.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Just found a "real world" example of strcat:
http://www.google.com/codesearch?hl=en&q=+strcat+show:BOegq4pdd4c:ZgXzinmg-fY:YpVkODN7elQ&sa=N&cd=5&ct=rc&cs_p=ftp://ftp.openbsd.org/pub/OpenBSD/3.9/sys.tar.gz&cs_f=./sys/lib/libkern/strcat.c
If you're using cout, then you're using C++, in which case you need to ask yourself why you're not using std::string.
Assuming this is an exercise, otherwise listen to the advice above.

The thing is that there is absolutely no way for your current function to be able to tell whether or not the first char array is long enough to accept the concatenation or not.

strlen() just tells you the position of the first null character in the array. It tells you nothing about how big the allocated array is. Even if you examine the data in the array after the null, it is not possible to tell whether that data is contained in the array or not. Even if it appears to be other data, it could just be random data left there from a previously deallocated block of memory.

So the short answer is that there is no way to handle this gracefully, which is presumabley why the standard library strcat() does not either. As has been said, if you want to write a C style function that can protect against this, you would also have to pass the size of the first array as a parameter to the function, a bit like strncat().
Use the string library.

EDIT: sorry, Evil Steve already said that!

Take a look at our website for C and C++ string examples.
www.nextdawn.nl
Quote:Original post by huaner
Hi:
I want to write a function of strcat. the code as fellow. but i have a question.if the capacity of firsttr is smaller than the capacity between firststr and secondstr.how to deal with it clearly?


You can't, really. The real strcat() doesn't either. The problem is that there is actually no way, inside the function, to find out what the "capacity" actually *is*.

The library also provides strncat(), which takes another parameter that says what the capacity is for the firststr. But then the caller has to get it right; strncat() will believe whatever it is told, because it can't check.

For these reasons and many more, C++ programmers will use the standard library type 'std::string' to represent text data. It provides many of its own functions for handling text, which are safe (because the data structure holds a pointer to some allocated memory, keeps track of the capacity, and automatically manages the memory - e.g. if the appended text will not fit, it re-allocates, puts stuff into the new memory block, and releases the old memory block).

Doing these things by yourself is a lot of work; but in C, it's probably the best way to make sure you don't have any memory problems. (You will also still need to clean up your "strings" manually when they go out of scope, because C does not have 'destructors'.) In C++, you should not re-invent the wheel; use std::string.

This topic is closed to new replies.

Advertisement