问strcpy,求详细解释!
2. strcpy(), 字符串拷贝.
char *strcpy(char *destination, const char *source)
{
while(*destinaton++=*source++);
return (destination-1);
}
3. strcat(), 字符串的连接.
char *strcat(char *target,const char *source)
{
char *original=target;
while(*target) target++; // Find the end of the string
while(*target++=*source++);
return(original);
}
上面是我拈贴的网页上的代码,有些问题不清。
请问,char* destination 存储在哪里(入参也可以是new出来的,对吧)?延长这个存储空间会不会造成对其它数据的覆盖吗?我在c里面问了这个问题,可是没有满意的答复。谁能给我详细解释?有人说标准库的代码不是这样的,那么能否给我详细的代码?我没有搜索到。把str的函数都可贴到这里,请关注。另外我看到有的代码在函数里面new了内存而返回的是char*,那么内存由谁来负责处理?谢谢。
分不是问题。如果满意,另开300也没问题。谢谢。
[解决办法]
VC 6自带的代码
- C/C++ code
/****char *strcat(dst, src) - concatenate (append) one string to another**Purpose:* Concatenates src onto the end of dest. Assumes enough* space in dest.**Entry:* char *dst - string to which "src" is to be appended* const char *src - string to be appended to the end of "dst"**Exit:* The address of "dst"**Exceptions:********************************************************************************/char * __cdecl strcat ( char * dst, const char * src ){ char * cp = dst; while( *cp ) cp++; /* find end of dst */ while( *cp++ = *src++ ) ; /* Copy src to end of dst */ return( dst ); /* return dst */}/****char *strcpy(dst, src) - copy one string over another**Purpose:* Copies the string src into the spot specified by* dest; assumes enough room.**Entry:* char * dst - string over which "src" is to be copied* const char * src - string to be copied over "dst"**Exit:* The address of "dst"**Exceptions:*******************************************************************************/char * __cdecl strcpy(char * dst, const char * src){ char * cp = dst; while( *cp++ = *src++ ) ; /* Copy src over dst */ return( dst );}
[解决办法]
strcpy和strcat都有安全问题
如果在windows下,建议用StringCchCopy,StringCchCat
[解决办法]
strcpy 功能只是将源字符串拷贝到目标字符串中
函数不提供判断目标或者源的地址是否正确有效
请问,char* destination 存储在哪里(入参也可以是new出来的,对吧)?延长这个存储空间会不会造成对其它数据的覆盖吗?我在c
//这个问题你是多虑了啊,扩展延长的存储空间靠realloc 这个系统分配内存函数来保证分配的空间有效并且可用
而不是你自己考虑的问题
[解决办法]
- C/C++ code
strcpy function char * strcpy ( char * destination, const char * source ); Copy stringCopies the C string pointed by source into the array pointed by destination, including the terminating null character.To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.Parametersdestination Pointer to the destination array where the content is to be copied. source C string to be copied. Return Valuedestination is returned.Example/* strcpy example */#include <stdio.h>#include <string.h>int main (){ char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0;} Output:str1: Sample stringstr2: Sample stringstr3: copy successful
[解决办法]
内存应该由调用者来分配,调用者来释放
调用者来检查大小是否越界
C的标准库并没有规定如何实现,但是一般的平台实现都差不多
debug版里多一个ASSERT(ptr != NULL);
[解决办法]
如果待连接的目标字符串是栈上分配的,strcat会覆盖低于目标地址的内容。
如果是堆上分配的,strcat会覆盖高于目标地址的内容。
调试环境:vc6.0
其实这从akirya贴的代码里可以看得很清楚了。。