自己的 strncpy() C++
Own strncpy() C++
我正在尝试实现我自己的 strncpy() 版本,我从中找到了源代码 link。
但是我遇到了一个Unhandled exception at 0x00411ad5 in exercise 2.exe: 0xC0000005: Access violation writing location 0x00417800.
每次代码到这个代码while((x++ < n) && (*dest++ = *source++));
完整代码如下:
char *strncpy(char * destination, const char * source, size_t n){
char *dest;
dest = destination;
size_t x=0;
while((x++ < n) && (*dest++ = *source++)); //this is where unhandled exception occurs
while(x++ < n){
*dest++ = 0;
}
return dest;
}
int main(){
char *sample = "blue";
char * sample2 = "red";
cout << strncpy(sample, sample2, 5);
getch();
return 0;
}
请告诉我为什么会出现这种情况,我该如何解决?谢谢!
您不能写入字符串常量 (sample
);改为写入 char
数组:
int main(){
char *sample = "blue";
char buffer[5];
cout << strncpy(buffer, sample, sizeof(buffer));
getch();
return 0;
}
您的目的地是 "blue"
,这是一个字符串文字,即常量。因此它位于内存的只读部分(并由本地 sample
变量指向),因此写入时出错。
试试这个:
int main(){
char sample[] = "blue";
char * sample2 = "red";
cout << strncpy(sample, sample2, 5);
getch();
return 0;
}
这使得 sample
成为本地可写内存中的一个数组。
首先,已经向您解释过您不能覆盖这样定义的字符串。
其次,如果该函数 returns 指针指向复制字符串的末尾,则不能使用 cout << strncpy。
你的程序有两个主要问题
第一个是函数 strncpy
必须 return destination
而不是 dest
char *strncpy(char * destination, const char * source, size_t n){
char *dest;
dest = destination;
size_t x=0;
while((x++ < n) && (*dest++ = *source++)); //this is where unhandled exception occurs
while(x++ < n){
*dest++ = 0;
}
// return dest;
return destination;
}
第二个是字符串文字是不可变的。任何修改字符串文字的尝试都会导致未定义的行为。
因此main函数应该改写成下面的方式
int main(){
char sample[] = "blue";
char * sample2 = "red";
cout << strncpy(sample, sample2, sizeof( sample ) );
getch();
return 0;
}
另外,使用名称为 x
的变量作为计数也是一种糟糕的编程风格。最好使用例如 i
.
我会把函数写得更简单
char * strncpy( char *destination, const char *source, size_t n )
{
char *dest = destination;
while ( n-- && ( *dest++ = *source++ ) );
while ( n-- ) *dest++ = '[=12=]';
return destination;
}
我正在尝试实现我自己的 strncpy() 版本,我从中找到了源代码 link。
但是我遇到了一个Unhandled exception at 0x00411ad5 in exercise 2.exe: 0xC0000005: Access violation writing location 0x00417800.
每次代码到这个代码while((x++ < n) && (*dest++ = *source++));
完整代码如下:
char *strncpy(char * destination, const char * source, size_t n){
char *dest;
dest = destination;
size_t x=0;
while((x++ < n) && (*dest++ = *source++)); //this is where unhandled exception occurs
while(x++ < n){
*dest++ = 0;
}
return dest;
}
int main(){
char *sample = "blue";
char * sample2 = "red";
cout << strncpy(sample, sample2, 5);
getch();
return 0;
}
请告诉我为什么会出现这种情况,我该如何解决?谢谢!
您不能写入字符串常量 (sample
);改为写入 char
数组:
int main(){
char *sample = "blue";
char buffer[5];
cout << strncpy(buffer, sample, sizeof(buffer));
getch();
return 0;
}
您的目的地是 "blue"
,这是一个字符串文字,即常量。因此它位于内存的只读部分(并由本地 sample
变量指向),因此写入时出错。
试试这个:
int main(){
char sample[] = "blue";
char * sample2 = "red";
cout << strncpy(sample, sample2, 5);
getch();
return 0;
}
这使得 sample
成为本地可写内存中的一个数组。
首先,已经向您解释过您不能覆盖这样定义的字符串。
其次,如果该函数 returns 指针指向复制字符串的末尾,则不能使用 cout << strncpy。
你的程序有两个主要问题
第一个是函数 strncpy
必须 return destination
而不是 dest
char *strncpy(char * destination, const char * source, size_t n){
char *dest;
dest = destination;
size_t x=0;
while((x++ < n) && (*dest++ = *source++)); //this is where unhandled exception occurs
while(x++ < n){
*dest++ = 0;
}
// return dest;
return destination;
}
第二个是字符串文字是不可变的。任何修改字符串文字的尝试都会导致未定义的行为。
因此main函数应该改写成下面的方式
int main(){
char sample[] = "blue";
char * sample2 = "red";
cout << strncpy(sample, sample2, sizeof( sample ) );
getch();
return 0;
}
另外,使用名称为 x
的变量作为计数也是一种糟糕的编程风格。最好使用例如 i
.
我会把函数写得更简单
char * strncpy( char *destination, const char *source, size_t n )
{
char *dest = destination;
while ( n-- && ( *dest++ = *source++ ) );
while ( n-- ) *dest++ = '[=12=]';
return destination;
}