简体   繁体   中英

Segmentation fault in my strcpy

I'm writing my own strcpy due to the fact that the default one in string.h only accept a const char * as a source string to copy from. I'm trying this very basic prototype (yes, the return isn't meaningful, I'm just trying things):

int copyStrings(char * dest, char * source){
    int i=0;
    while(source[i]!='\0'){
        dest[i]=source[i];
        i++;
    }
    dest[i]='\0';
    return 0;
}

and it gives me SIGSEGV, Segmentation Fault error in gdb, at the line dest[i]=source[i] , right at the first character. I'm pretty sure dest[i] isn't a string literal, so I should be able to modify it.

What am I doing wrong?

EDIT: here's the calling

int main(){
    char * str = (char*)malloc((int)sizeof(double));
    char * str2 = (char *)malloc((int)sizeof(int));
    str = "hello";
    str2 = "hey jude";
    copyStrings(str2, str);
    free(str);
    free(str2);
    return 0;
}

This is assigning a string literal to str2 - the very thing that you claim you aren't doing. This is actually the cause of your segfault.

str2 = "hey jude";

It also is causing a memory leak as prior to this, you malloc 'd some memory and assigned it to str2 as well. But not enough memory to hold the string. Typically an int is 4 bytes and you need 9 bytes to store that string.

What you want to do is this, which allocates as many bytes as there are in the string, plus an extra one to store the \\0 terminating character at the end.

str2 = malloc(strlen("hey jude")+1);
strcpy(str2,"hey jude");

or on some systems you can use POSIX function strdup() which effectively does the job of the above in one handy function call.

str2 = strdup("hey jude");

Let's go at it line by line and see where it goes wrong:

int main(){
    char * str = (char*)malloc((int)sizeof(double));
    char * str2 = (char *)malloc((int)sizeof(int));
    str = "hello";
    str2 = "hey jude";
    copyStrings(str2, str);
    free(str);
    free(str2);
    return 0;
}

int main(){ - this is an improper definition of main . Should be int main(int argc, char **argv)

char * str = (char*)malloc((int)sizeof(double)); - defines str , then allocates (probably) 8 bytes of memory and assigns its address to str . malloc takes a size_t argument, so the cast (int)sizeof(double) is incorrect. Also, in C the return value of malloc should never be cast. So this line should be char * str = malloc(sizeof(double));

char * str2 = (char *)malloc((int)sizeof(int)); - all the same problems as the preceding line. Should be char *str2 = malloc(sizeof(int));

str = "hello"; - causes a memory leak, because the memory you JUST ALLOCATED two lines earlier is now irretrievably lost. You've got two options here - either don't allocate the memory when defining str or free it first. Let's do the latter:

free(str);
str = "hello";

str2 = "hey jude"; - same problem, similar solution:

free(str2);
str2 = "hey jude";

copyStrings(str2, str); - here you're telling your routine to copy the constant string "hello" over the top of the constant string "hey jude". This will work fine on some systems , but will blow up on other systems . The question is in the treatment of the constant string "hey jude". If it's stored in modifiable memory the code will work just fine. If it's stored in memory which is marked as being unmodifiable, however, it will blow up. It seems that the latter is the case on your system. To fix this you probably want to go back to the previous line and change it to

str2 = malloc(20);

That's more memory than you'll need, but it will work just fine.

free(str); - you're attempting to free the constant string "hello", which is not dynamically allocated memory. This needed to be done prior to the assignment str = "hello"; .

free(str2; - same problem as above. This needed to be done prior to the assignment str2 = "hey jude"; .

} - correct

Best of luck.

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