C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 写入初始化缓冲区时swprintf_s失败大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用visual studio 2010在C语言中编写一个程序.我使用SWPrintf_s函数将格式化的字符串写入wchar_t缓冲区.
当我尝试写入已初始化的缓冲区时,我收到以下错误.

Unhandled exception at 0x77b3fbda in svats.exe: 0xC00000FD: Stack overflow.

而有时

Unhandled exception at 0xfefefefe in svats.exe: 0xC0000005: Access violation.

以下是产生访问冲突的代码.

wchar_t wBuff[1024] = L"b";
int test;
test = swprintf_s(wBuff,sizeof(wBuff),L"a%s","test");

和堆栈溢出的代码.

wchar_t wBuff[1024] = L"b";
int test;
test = swprintf_s(wBuff,L"test");

现在第二段代码工作了一次,不知道为什么.

谁知道问题是什么?

Ps.这些文件没有加载,任何人都知道为什么?是因为visual studio是32位而我的操作系统是64位吗?

'svats.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\user32.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll',CAnnot find or open the PDB file
'svats.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll',CAnnot find or open the PDB file

解决方法

@H_262_37@
int main() {
  wchar_t wBuff[1024] = L"b";
  int test;
  test = swprintf_s(wBuff,_countof(wBuff),"test");
}

这段代码会起作用.如pmg所述,第二个参数应为1024,而不是2048.当您执行sizeof时,它将返回以字节为单位的大小.但是,swprintf_s需要缓冲区中可用的字符数.您可以使用_countof,它基本上扩展到与您建议的相同.

大佬总结

以上是大佬教程为你收集整理的c – 写入初始化缓冲区时swprintf_s失败全部内容,希望文章能够帮你解决c – 写入初始化缓冲区时swprintf_s失败所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。