Sign in to follow this  
huaner

strcat

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this