简体   繁体   English

自己的strncpy()C ++

[英]Own strncpy() C++

I am trying to implement my own version of strncpy(), i found a source code from this link . 我正在尝试实现自己的strncpy()版本,我从此链接中找到了源代码。

But I encountered a Unhandled exception at 0x00411ad5 in exercise 2.exe: 0xC0000005: Access violation writing location 0x00417800. 但是我Unhandled exception at 0x00411ad5 in exercise 2.exe: 0xC0000005: Access violation writing location 0x00417800.遇到Unhandled exception at 0x00411ad5 in exercise 2.exe: 0xC0000005: Access violation writing location 0x00417800. everytime the code reaches this code while((x++ < n) && (*dest++ = *source++)); 每当代码到达该代码while((x++ < n) && (*dest++ = *source++));

Here is the complete code: 这是完整的代码:

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;
}

Please tell me why this occurs and how should I fix it? 请告诉我为什么会发生这种情况,我该如何解决? Thanks! 谢谢!

You cannot write to a string constant ( sample ); 您不能写一个字符串常量( sample )。 write to a char array instead: 改为写入一个char数组:

int main(){
    char *sample = "blue";
    char buffer[5];

    cout << strncpy(buffer, sample, sizeof(buffer));
    getch();
    return 0;
}

Your destination is "blue" which is a string literal, that is a constant. 您的目的地是"blue" ,它是一个字符串常量,是一个常量。 As such it is located in a read-only part of memory (and pointed at by local sample variable), thus error when writing. 因此,它位于内存的只读部分(并由本地sample变量指向),因此在写入时会出错。

Try this: 尝试这个:

int main(){
    char sample[] = "blue";
    char * sample2 = "red";

    cout << strncpy(sample, sample2, 5);
    getch();
    return 0;
}

which makes sample an array in local, writeable memory. 这使得sample在本地可写内存中成为数组。

First, it was already explained to you that you can't overwrite a string that is defined like that. 首先,已经向您说明了您不能覆盖这样定义的字符串。
Second, you cant use cout << strncpy if that function returns pointer to the end of the copied string. 其次,如果该函数返回指向复制字符串末尾的指针,则不能使用cout << strncpy。

There are two main problems with your program The first one is that function strncpy has to return destination instead of dest 程序有两个主要问题。第一个问题是函数strncpy必须返回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;
    }

The second one is that string literals are immutable. 第二个是字符串文字是不可变的。 Any attempt to modify a string literal results in undefined behaviour. 尝试修改字符串文字会导致未定义的行为。

Thus main function should be rewritten the following way 因此,主要功能应通过以下方式重写

int main(){
    char sample[] = "blue";
    char * sample2 = "red";

    cout << strncpy(sample, sample2, sizeof( sample ) );

    getch();

    return 0;
}

Also it is a bad style of programming to use variable with name x as a count. 使用名称为x变量作为计数也是一种糟糕的编程风格。 It is better to use for example i . 最好使用例如i

I would write the function simpler 我会写更简单的功能

char * strncpy( char *destination, const char *source, size_t n )
{
        char *dest = destination;

        while ( n-- && ( *dest++  = *source++ ) );
        while ( n-- ) *dest++ = '\0';

        return destination;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM