“sprintf”是否有默认缓冲区长度?方法?

发布于 2024-08-27 13:28:44 字数 260 浏览 5 评论 0原文

我使用 sprintf 方法将数据格式化为要写入文件的字符串,在使用 VS 2008 的 C++ 控制台应用程序中。输入是一个特定的消息,它具有各种变量和值(例如:类型“int”和值“ 10'/输入字符串和值“abc”等)当我发送两条消息时,它工作得很好。但是,当我发送两条以上的消息时,它会给出运行时错误,提示 0xC0000005:访问冲突读取位置 0xabababab。为什么会发生这种情况?是因为方法“sprintf”有默认的缓冲区长度吗?我怎样才能克服这个问题?

I used sprintf method to format data to a string which I want to write to a file, in C++ console application using VS 2008. The Input is a particular message, which has various variables and values (ex: Type 'int' and Value '10' / Type string and value "abc", etc.) When I send a two messages it works perfectly. But When I send more than two messages it gives a runtime error saying 0xC0000005: Access violation reading location 0xabababab. Why is this happening? Is it because the method 'sprintf' has a default buffer length? How can I overcome this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-09-03 13:28:44

不,没有默认尺寸。它将假定有足够的空间,并写入格式字符串指定的内容。

您的访问冲突显示正在读取,这暗示您的参数可能有问题,也许它们与格式字符串不匹配。

您需要拥有与在格式化字符串中使用 % 代码引用的参数一样多、类型正确且顺序正确的参数。

No, there is no default size. It will assume that there is space enough, and write as much as specified by the formatting string.

Your access violation says reading, which hints that there might be something wrong with your arguments, perhaps they don't match the formatting string.

You need to have exactly as many arguments, of the proper types and in the correct order, as you reference using %-codes in the formatting string.

温暖的光 2024-09-03 13:28:44

顺便说一下,您可能想看看此页面

0xABABABAB :由 Microsoft 的 HeapAlloc() 用于在分配堆内存后标记“无人区”保护字节

By the way, you might want to have a look at this page.

0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文