简体   繁体   中英

Own strncpy() C++

I am trying to implement my own version of strncpy(), i found a source code from this link .

But I encountered a 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++));

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 ); write to a char array instead:

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. As such it is located in a read-only part of memory (and pointed at by local sample variable), thus error when writing.

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.

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.

There are two main problems with your program The first one is that function strncpy has to return destination instead of 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. It is better to use for example 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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