将字符串复制到无符号缓冲区:分段错误
我正在尝试将两个整数和一个字符串复制到缓冲区并打印缓冲区元素。我得到第三个 printf 语句的段错误:
id = 102;
len = 3;
str = "working";
memmove(buffer,&message_id,sizeof(id));
memmove(buffer+(sizeof(id)),&len,sizeof(len));
memmove(buffer+(2*sizeof(id)),&string, sizeof(str));
printf("1 is: %d\n", buffer[0]);
printf("2 is: %d\n", buffer[4]);
printf("3 is %s\n, buffer[8])); // here is where i get the seg fault should
be a string
/***/
bufer is declared as unsinged char buffer[444];
我不知道为什么这会出现段错误?
i am trying to copy two integers and a character string to a buffer and print the buffer elements out. I get a seg fault for the third printf statement:
id = 102;
len = 3;
str = "working";
memmove(buffer,&message_id,sizeof(id));
memmove(buffer+(sizeof(id)),&len,sizeof(len));
memmove(buffer+(2*sizeof(id)),&string, sizeof(str));
printf("1 is: %d\n", buffer[0]);
printf("2 is: %d\n", buffer[4]);
printf("3 is %s\n, buffer[8])); // here is where i get the seg fault should
be a string
/***/
bufer is declared as unsinged char buffer[444];
I dont know why this would seg fault?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
buffer[8]
是一个char
,%s
等待字符串,意思是char *
,传递而是使用&buffer[8]
。您会遇到分段错误,因为 printf 尝试将 char 视为指向 char 的指针,这是地址(如果传递了一个字符,则它不太可能是有效的)编辑:正如 David 所评论的,如果复制字符串的起点与其前面的值相关,请不要使用固定值,而应使用
&buffer[8]
使用buffer+(2*sizeof(id))
或buffer[2*sizeof(id)]
buffer[8]
is achar
,%s
waits for string, meaningchar *
, pass&buffer[8]
instead. You get segmentation fault because printf tries to treat a char as a pointer to char, which is address (and if a char is passed it's unlikely to be a valid one)EDIT: as David commented, if the starting point for copying the string is related to the values before it, don't use a fixed value, instead of
&buffer[8]
usebuffer+(2*sizeof(id))
orbuffer[2*sizeof(id)]
您的代码存在几个问题,但最重要的一点是
memmove()
不会复制字符串的空字符字节。这为您提供了两个选择:
memmove(buffer+sizeof(id)+sizeof(len), str, strlen(str) +1);
'\0'
(又名0
)结尾:memmove(buffer+sizeof(id)+ sizeof(len), str, strlen(str));
buffer[sizeof(id)+ sizeof(len) + strlen(str) + 1] = 0;
无论如何,代码现在工作了。另一个问题是您尝试使用
sizeof(str)
指定字符串的长度。这是错误的,您应该执行strlen(str)
。最后一件事,为了清楚和安全起见,不要执行2*sizeof(id)
。如果稍后您决定更改变量类型,那么您就完蛋了。正确的方法是sizeof(id)+sizeof(len)
。就这样。There were several issues on your code, but the most important point is that
memmove()
does not copy the null character byte of the string.This gives you two options:
memmove(buffer+sizeof(id)+sizeof(len), str, strlen(str) +1);
'\0'
(aka.0
) on your buffer:memmove(buffer+sizeof(id)+sizeof(len), str, strlen(str));
buffer[sizeof(id)+ sizeof(len) + strlen(str) + 1] = 0;
Anyway, the code works now. Another issue was that you were trying to specify the length of the string with
sizeof(str)
. That's wrong, and you should be doingstrlen(str)
. One last thing, for clarity and security purposes don't do2*sizeof(id)
. If later you decide to change the variable type you are screwed. The right way wold besizeof(id)+sizeof(len)
. That's all.主要问题是您试图通过仅传递一个字符来打印字符串。这是因为
buffer[8]
引用索引 8 处的char
,而不是从该位置开始的字符串。因此,您需要获取buffer[8]
的地址,使其成为字符串或char*
。它出现段错误的原因是
printf
尝试打印从第一个字符给出的地址(即字符串内容本身)开始的字符串,该字符串不是有效的指针。还有不少错别字和错误。工作版本如下:
如果您在启用所有警告的情况下编译了此应用程序(即
-Wall
),您的编译器(至少 GCC 会这样做)应该警告您有关错误的信息,如下所示:警告不应该被忽视!
The main problem is that you are trying to print the string by passing only a character. This is because
buffer[8]
refers to thechar
at index 8, not the string starting at that position. So you need to take the address ofbuffer[8]
to make it a string orchar*
.The reason why it segfaults is that
printf
attempts to print a string starting at the address given by the first char (ie. the string contents itself), which is not a valid pointer.There are also number of typos and mistakes. A working version is below:
If you had compiled this app with all warnings enabled (ie.
-Wall
), your compiler (at least GCC does) should warn you about your mistake like this:Warnings should not be ignored!