[英]segmentation fault on strcpy();
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
typedef struct client
{
int threadid;
int argc;
char *argv[3];
} client;
void exit(int status);
void error(char *msg);
void *threadClient(void *socket_desc);
int main(int argc, char *argv[])
{
client info[10];
pthread_t thread[10];
printf("%s\n%s\n%s\n", argv[0], argv[1], argv[2]);
// Error happens here
for (int i=0; i<=10; i++)
{
info[i].threadid = i;
strcpy(info[i].argv[0], argv[0]);
strcpy(info[i].argv[1], argv[1]);
strcpy(info[i].argv[2], argv[2]);
info[i].argc = argc;
printf("here");
if (pthread_create(&thread[i], NULL, threadClient, (void*)&info[i]) < 0)
{
perror("could not create thread");
return 1;
}
sleep(3);
}
pthread_exit(NULL);
return 0;
}
During the loop, when I am trying to copy the info from argv
to my struct I get a segmentation fault.在循环期间,当我尝试将信息从
argv
复制到我的结构时,出现分段错误。 Why does it happen?为什么会发生?
Program received signal SIGSEGV, Segmentation fault.
程序收到信号 SIGSEGV,分段错误。 __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296 296 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
__strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296 296 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:没有这样的文件或目录。
There are two problem here.这里有两个问题。
First, your argv
array in your info
structure is an array of pointers.首先,您的
info
结构中的argv
数组是一个指针数组。 These start out uninitialized.这些开始时未初始化。 When you later call
strcpy
, giving one of these array elements as the first parameter, it expects that pointer to point to valid memory.当您稍后调用
strcpy
并将这些数组元素之一作为第一个参数时,它期望该指针指向有效内存。 So you end up dereferencing an uninitialized pointer.所以你最终会取消引用一个未初始化的指针。 This invokes undefined behavior , which in this case manifests as a segfault.
这会调用未定义的行为,在这种情况下表现为段错误。
You need to assign something to these pointers.您需要为这些指针分配一些东西。 You can either use
strdup
to make a copy of these strings:您可以使用
strdup
制作这些字符串的副本:
info[i].argv[0] = strdup(argv[0]);
info[i].argv[1] = strdup(argv[1]);
info[i].argv[2] = strdup(argv[2]);
Or, if you don't plan on modifying these values, you can just copy the pointer values directly:或者,如果您不打算修改这些值,则可以直接复制指针值:
info[i].argv[0] = argv[0];
info[i].argv[1] = argv[1];
info[i].argv[2] = argv[2];
The second issue is an off-by-one error in your loop:第二个问题是循环中的逐一错误:
for (int i=0; i<=10; i++){
Because you use <=
, your indexes into the array will range from 0 to 10. However, your array only has 10 elements (with indexes 0 to 9), so you're writing past the end of the array.因为您使用
<=
,所以您在数组中的索引范围为 0 到 10。但是,您的数组只有 10 个元素(索引为 0 到 9),因此您写入的内容超出了数组的末尾。 This also invokes undefined behavior.这也会调用未定义的行为。
Change your conditional to <
as follows:将您的条件更改为
<
,如下所示:
for (int i=0; i<10; i++){
Your struct is defined to contain three pointers ( char *argv[3]
).您的结构被定义为包含三个指针(
char *argv[3]
)。 When you create an array of those structs in the stack ( client info[10]
) space is reserved for these pointers, among other things.当您在堆栈(
client info[10]
)中创建这些结构的数组时,会为这些指针等保留空间。 The structs aren't initalized to anything, so the pointers don't point to any sensible memory location.结构没有初始化为任何东西,所以指针不指向任何合理的内存位置。 Hence, accessing them as with
strcpy(info[i].argv[0], argv[0]);
因此,像使用
strcpy(info[i].argv[0], argv[0]);
一样访问它们strcpy(info[i].argv[0], argv[0]);
is in error.出错了。
Either allocate the space you use explicitly:要么分配您显式使用的空间:
info[i].argv[0] = malloc(strlen(argv[0])+1);
strcpy(info[i].argv[0], argv[0]);
Or use strdup()
to copy the strings:或者使用
strdup()
复制字符串:
info[i].argv[0] = strdup(argv[0]);
Remember to free()
the space you allocated afterwards.记住要
free()
之后分配的空间。
Also, I don't think you should declare a function called exit()
yourself, it's a standard function and should be in stdlib.h
.另外,我认为您不应该自己声明一个名为
exit()
的函数,它是一个标准函数,应该在stdlib.h
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.