C Sprintf() 附加垃圾字符
我尝试使用 sprintf 附加一个 int、字符串和一个 int。
sprintf(str,"%d:%s:%d",count-1,temp_str,start_id);
这里,start_id的值总是相同的。 temp_str 的值是一个 char * 每次都会增加。一段时间后我得到了正确的输出,然后我的 sprintf 开始在 temp_str 和 startid 之间打印垃圾字符。所以我的 str 被损坏了。
谁能解释这种行为?
示例
计数 11 处的
11:1:2:3:1:2:3:1:2:3:1:21:3:1:2:3:1:2:3:1:2:3:1:2:3: 1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1: 2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2: 3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2
在计数 8 时
8:1:2:3:1:2:3:1:2:3:1:21:3:1:2:3:1:2:3:1:2:3:1:2:3: 1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3: 1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1: 2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1�:2
我不明白为什么以及如何将“�”附加到字符串中
I tried to use sprintf to append a int, string and an int.
sprintf(str,"%d:%s:%d",count-1,temp_str,start_id);
Here, the value of start_id is always the same. The value of temp_str which is a char * increases every time. I get correct output for some time and then my sprintf starts printing junk characters between temp_str and startid. So my str get corrupted.
Can anyone explain this behavior ?
example
at count 11
11:1:2:3:1:2:3:1:2:3:1:21:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2
at count 8
8:1:2:3:1:2:3:1:2:3:1:21:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1:2:3:1�:2
I don't understand why and how "�" is appended to the string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要么
temp_str
在某个时刻不是空终止的,或者您已经耗尽了str
的缓冲区,并且其他一些内存访问正在影响它。如果不看代码,就很难判断,但是,如果将
str
的大小加倍并且问题行为发生变化,那么很可能是后者。Either
temp_str
is not null-terminated at some point or you've blown the buffer forstr
and some other memory access is affecting it.Without seeing the code, it's a little hard to tell but, if you double the size of
str
and the problem behaviour changes, then it's probably the latter.1>尝试在使用 sprintf 之前将 str 缓冲区设置为 0
2> temp_str 的值是一个 char * 每次都会增加
你这是什么意思?
这应该是普通的字符指针,它将指向某个字符串,并且该字符串应该以 null 结尾,并且 tha 将被复制到 str
3> 组合所有三个参数的总大小不应超过 str 缓冲区的大小
1> try to memset your str buffer with 0 befor using sprintf
2> The value of temp_str which is a char * increases every time
what do u mean by this ?
this should be normal charachter pointer which will point some string and that string should be null terminated and tha will be copied to str
3> the total size by combing all three argument should not be exceed the size of str buffer
看起来字符串
temp_str
不是以 NUL 结尾的。您可以在调用sprintf
之前终止它,或者如果您知道要打印的长度,请使用%.*s
格式化运算符,如下所示:It looks like the string
temp_str
isn't NUL-terminated. You can either terminate it before the call tosprintf
, or if you know the length you want to print, use the%.*s
formatting operator like this:您正在运行 temp_str 的末尾。检查你的边界并确保它是空终止的。到达终点时停止增加太阳。
You are running off the end of temp_str. Check your bounds and make sure it's null terminated. Stop incrementing sun you get to the end.