使用 strcpy() 时出现分段错误?
我有一个全局结构:
struct thread_data{
char *incall[10];
int syscall arg_no;
int client_socket;
};
在 main() 中
char buffer[256];
char *incall[10];
struct thread_data arg_to_thread;
strcpy(incall[0],buffer); /*works fine*/
strcpy(arg_to_thread.incall[0],buffer); /*causes segmentation fault*/
为什么会发生这种情况,请提出解决办法。
谢谢
I have a global structure:
struct thread_data{
char *incall[10];
int syscall arg_no;
int client_socket;
};
and in main()
char buffer[256];
char *incall[10];
struct thread_data arg_to_thread;
strcpy(incall[0],buffer); /*works fine*/
strcpy(arg_to_thread.incall[0],buffer); /*causes segmentation fault*/
Why does this happen and Please suggest a way out.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
段错误意味着出现了问题。但没有段错误并不意味着某些没有错误。如果两种情况基本相同,一种出现段错误,另一种则没有,通常意味着它们都错了,但只有其中一种恰好触发了段错误。
查看 char* incall[10] 行,这意味着您有一个包含 10 个指向 char 的指针的数组。默认情况下,这些指针将指向随机位置。因此,strcpying 到 incall[0] 中会将字符串复制到随机位置。这很可能会出现段错误!您需要首先初始化 incall[0](使用
malloc
)。所以一个更大的问题是为什么第一行没有出现段错误?我想原因是,恰好之前内存中的内容都是有效的指针。因此,strcpy 不会出现段错误,它只是覆盖其他内容,这会导致完全意外的行为。因此,您必须修复两行代码。
另一个问题(一旦你解决了这个问题)是 strcpy 本身非常危险——因为它会复制字符串直到找到 0 字节然后停止,你永远无法确定它到底会复制多少字节。复制(除非您使用
strlen
来分配目标内存)。因此,您应该使用strncpy
来限制复制到缓冲区大小的字节数。A segfault means that something is wrong. But no segfault does not mean that something isn't wrong. If two situations are basically the same, and one segfaults and the other does not, it usually means that they are both wrong, but only one of them happens to be triggering the segfault.
Looking at the line
char* incall[10]
, what that means is you have an array of 10 pointers to a char. By default, these pointers will be pointing at random places. Therefore, strcpying into incall[0] will be copying the string to a random location. This is most likely going to segfault! You need to initialise incall[0] first (usingmalloc
).So a bigger question is why doesn't the first line segfault? I would imagine the reason is that it just so happens that whatever was in memory before was a valid pointer. Therefore, the strcpy doesn't segfault, it just overwrites something else which will later cause completely unexpected behaviour. So you must fix both lines of code.
Another issue (once you have fixed that) is that
strcpy
itself is highly dangerous -- since it copies strings until it finds a 0 byte and then stops, you can never be sure exactly how much it's going to copy (unless you usestrlen
to allocate the destination memory). So you should usestrncpy
instead, to limit the number of bytes copied to the size of the buffer.您还没有初始化指针
incall[0]
,所以上帝只知道第一个strcpy()
写入到哪里。你很不幸你的程序没有立即崩溃。您尚未初始化指针
arg_to_thread.incall[0]
,因此上帝只知道第二个strcpy()
写入何处。你很幸运,你的程序现在就崩溃了,而不是稍后。在这两种情况下都不是编译器的错;您必须始终确保初始化指针。
You've not initialized the pointer
incall[0]
, so goodness only knows where the firststrcpy()
writes to. You are unlucky that your program does not crash immediately.You've not initialized the pointer
arg_to_thread.incall[0]
, so goodness only knows where the secondstrcpy()
writes to. You are lucky that your program crashes now, rather than later.In neither case is it the compiler's fault; you must always ensure you initialize your pointers.
strcpy
。请改用strncpy
。strcpy
是臭名昭著的缓冲区溢出漏洞来源 - 这是一个安全和维护的噩梦,实际上没有任何借口。strcpy
. Usestrncpy
instead.strcpy
is a notorious source of buffer overflow vulnerabilities - a security and maintenance nightmare for which there really isn't an excuse.