[英]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. 由于以下事实,我正在编写自己的
strcpy
string.h
中的默认值仅接受const char *
作为要从中复制的源字符串。 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. 它在第一个字符
dest[i]=source[i]
行处给出了SIGSEGV, Segmentation Fault
gdb中的SIGSEGV, Segmentation Fault
错误。 I'm pretty sure dest[i]
isn't a string literal, so I should be able to modify it. 我很确定
dest[i]
不是字符串文字,因此我应该可以对其进行修改。
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. 这是为
str2
分配字符串文字-您声称自己没有做的事情。 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. 这也导致内存泄漏是在此之前,你
malloc
倒是一些内存,并指派它str2
为好。 But not enough memory to hold the string. 但是没有足够的内存来容纳字符串。 Typically an
int
is 4 bytes and you need 9 bytes to store that string. 通常,一个
int
是4个字节,您需要9个字节来存储该字符串。
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. 您要执行的操作是,该操作分配与字符串中一样多的字节,再加上一个额外的字节以在末尾存储
\\0
终止字符。
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. 或在某些系统上,您可以使用POSIX函数
strdup()
在一个便捷的函数调用中有效地完成上述工作。
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
. int main(){
-这是main
的不正确定义。 Should be int main(int argc, char **argv)
应该是
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
. -定义
str
,然后(可能)分配8个字节的内存,并将其地址分配给str
。 malloc
takes a size_t
argument, so the cast (int)sizeof(double)
is incorrect. malloc
使用size_t
参数,因此size_t
(int)sizeof(double)
不正确。 Also, in C the return value of malloc
should never be cast. 同样,在C语言中, 绝对不要转换
malloc
的返回值。 So this line should be char * str = malloc(sizeof(double));
所以这行应该是
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));
应该是
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. 您在这里有两个选择-在定义
str
时不分配内存或先释放它。 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". -在这里,您要告诉您的例程将常量字符串“ hello”复制到常量字符串“ 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".
问题在于常量字符串“ 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. -您试图释放常量字符串“ hello”,该常量不是动态分配的内存。 This needed to be done prior to the assignment
str = "hello";
这需要在赋值
str = "hello";
之前完成str = "hello";
. 。
free(str2;
- same problem as above. This needed to be done prior to the assignment str2 = "hey jude";
. free(str2;
-与上述相同的问题。这需要在赋值str2 = "hey jude";
。
}
- correct }
-正确
Best of luck. 祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.