Sign in to follow this  

mystrcpy

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

char *mystrcpy(char *strDest, const char *strSrc)
{
	char *address=strDest;
	while(*strDest++=*strSrc++);
	*strDest++='\0';
	return address;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char a[]="awp";
	char b[]="abcdefghigklmn";
	out<<mystrcpy(a,b);
}


it is answer by a doctor to write a char *mystrcpy(char *strDest, const char *strSrc). I think it is dangous, if strSrc < strDest, strSrc will be overlay. [Edited by - LessBread on January 26, 2006 3:54:40 AM]

Share this post


Link to post
Share on other sites
I'm not sure what you are asking.

If you are running into a problem with this, it is because of this:


char a[]="awp";
char b[]="abcdefghigklmn";


You are trying to copy 15 characters (including \0) from b into an area of memory that only has 4 bytes reserved. You should be getting segmentation faults whenever you try it.

You solution could be to reallocate memory for 'a' before writing to it, or to measure it and truncate 'b' appropriately.

Share this post


Link to post
Share on other sites
You could probably get away with 15 characters due to the compiler padding the string to a default length, but that doesn't make the usage safe. It's up to the programmer to make certain the destination is large enough. The code for mystrcpy isn't very different from the code for strcpy.

If the length of strSrc < the length of strDest, there won't be any overlay due to the null termination of the string. Whatever was in strDest beyond the length of strSrc will simply be lost.

At any rate, these routines aren't truly safe anyway.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
also note that the code isn't minimal. the third line within the cpy function is redundant. if you're expecting the second argument to be a C null terminated string.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for an explanation see stroustrup, the c++ programming language, chapter 6.2.5, 3rd edition (should be the same in all the other ones)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
also note that the code isn't minimal. the third line within the cpy function is redundant. if you're expecting the second argument to be a C null terminated string.
Yes the destination string is double-null terminated, being more dangerous than terminating with 1 null char because the programmer may only have a buffer big enough for one null char.

I have written my own strcpy routines before and it was for a valid purpose. I was working with an embedded C compiler and using a mixture of near and far pointers. althought there were functions like strcpy_n_f for near to far etc, they didn't actually work! Yes were were using them correctly, and yes we were calling the right ones.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why do people keep calling C-strings "null-terminated"? The name of the '\0' character is NUL, and nothing else. So, the correct name is nul-terminated.

Programmers these days....

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Why do people keep calling C-strings "null-terminated"? The name of the '\0' character is NUL, and nothing else. So, the correct name is nul-terminated.

Programmers these days....


Because "NUL" is simply the abbreviation for the word "NULL" which was used in the ASCII standard character set reference table. Similarly, it's called backspace, not just BS; a carriage return, not just a CR; and the thing that old terminal printers used to make noise was a bell, not a BEL. Apparently, programmers these days are less prone to nitpicking.

Share this post


Link to post
Share on other sites
#define NULL (void *)0

Ok. So NULL is a void pointer and NUL is a char. They both evaluate to 0.

I suppose it's because programmers these days aren't as familiar with ASCII code as were programmers from days of yore... [smile]

Share this post


Link to post
Share on other sites

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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