sprintf有更多问题吗? [英] More problems with sprintf?

查看:53
本文介绍了sprintf有更多问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[在新主题中重新发布,作为一个论坛颠簸似乎没有工作:)]


发现了sprintf(参见这里),我意识到这是我的时间转换功能的想法,见主题。所以我采取了暴跌,转换它,它工作了一段时间,但现在出了问题!


这个函数传递一个整数秒,并返回一个文本字符串 HH:MM"从中衍生出来。但它不再起作用了;我想我正在做一些愚蠢的事情!


请注意,txt4 [4]和txt7 [7]是全局暂存器变量。

[Repost in new thread, as a forum bump doesn''t seem to work :)]

Having discovered sprintf (see here), I realised that it was idea for my time conversion function, see this thread. So I took the plunge, and converted it, and it worked for a while, but now something has gone wrong!

This function is passed an integer number of seconds, and returns a text string " HH:MM" derived from it. But it doesn''t work anymore; I guess I''m doing something dumb!

Note that txt4[4] and txt7[7] are global scratchpad variables.

展开 | 选择 | Wrap | 行号

推荐答案

你得到的是什么输入值?


你也应该这样做能够通过一次调用sprintf完成整个事情。
For what input value do you get that behaviour?

Also you should be able to do the whole thing with a single call to sprintf.


对函数的初始调用以0开始,然后每秒递增。当我检查我的代码时,我让它运行了几分钟;看起来好不好?


是的,我确定我可以通过一次通话完成它,但我一次只吃一个字节:)
The initial call to the function starts with 0, then increments every second. I let it run for a couple of minutes while I checked my code; does it look OK?

Yes, I''m sure I could do it with a single call, but I''m eating my elephant one byte at a time :)


除了时间数组和txt7不足以处理你可能获得的最大值之外,代码看起来还不错。


小时是秒/ 60/60,秒是一个int所以最大小时值(假设32位int是1193046,这可以是正数或负数,因此小时字段的最大字符数是8.分钟是秒/ 60%因此它的最大值也是59,无论是正数还是负数,因此它的最大字段宽度为3.加上空格字符加冒号加上NULL终止符表示时间[]和txt7 []的最小缓冲区大小应为


BufferSize = 8 + 3 + 1 + 1 + 1 = 14

两倍于程序提供的7个字节。实际输出你具有255:256长度为8个字节并且超出缓冲区l在你的程序中导致未定义的行为。


255是一个完全合理的值,因为有足够的运行时间或不正确的调用代码将错误的值传递给函数。
<几分钟的
256很难解释。你没有说,IIRC,如果这个程序是单线程或多线程的,你不必要地使用全局缓冲区txt4和txt7也没有在本地分配,允许程序的任何部分改变它。此时某种缓冲区损坏(或意外重用)似乎是分钟值最明显的解释。
The code looks ok apart from the fact the the time array and txt7 are not nearly long enough to deal with the maximum values you might get.

Hours is seconds / 60 / 60, seconds is an int so the maximum value of hours (assuming a 32bit int is1193046 and this can be positive or negative so the maximum number of characters for the hours field is 8. Minutes is seconds / 60 % 60 so its maximum value is 59 again either positive or negative so its maximum field width is 3. Plus a space character plus a colon plus a NULL terminator means your minimum buffer size for time[] and txt7[] should be

BufferSize = 8 + 3 + 1 + 1 + 1 = 14

Twice as long as the 7 bytes provided by your program. In fact the output you have "255:256" is 8 bytes long and overruns the buffer length causing undefined behaviour in your program.

255 is a completely plausible value for hours given either enough running time or incorrect calling code passing the wrong value into the function.

256 for minutes is much harder to explain. You haven''t said, IIRC, if this program is single threaded or multi-threaded and you are unnecessarily using the global buffer txt4 and txt7 is also not allocated locally allowing any part of your program to alter it. At this time some sort of buffer corruption (or accidental reuse) seems like the most obvious explanation for the minutes value.


这篇关于sprintf有更多问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆