写入初始化缓冲区时 swprintf_s 失败
我正在使用 Visual Studio 2010 在 Windows 中编写一个 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,sizeof(wBuff),L"a%s",L"test");
现在第二段代码可以工作一次,不知道为什么。
有人知道问题是什么吗?
附言。这些文件没有被加载,有人知道为什么吗?是因为 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
I am writing a program in C for windows using visual studio 2010. I am using the swprintf_s function to write a formatted string to a wchar_t buffer.
I am getting the following errors when I attempt to write to a buffer that has been initialized.
Unhandled exception at 0x77b3fbda in svats.exe: 0xC00000FD: Stack overflow.
and sometimes
Unhandled exception at 0xfefefefe in svats.exe: 0xC0000005: Access violation.
Here is the code that produces the access violation.
wchar_t wBuff[1024] = L"b";
int test;
test = swprintf_s(wBuff,sizeof(wBuff),L"a%s","test");
and the code for the stack overflow.
wchar_t wBuff[1024] = L"b";
int test;
test = swprintf_s(wBuff,sizeof(wBuff),L"a%s",L"test");
Now the 2nd piece of code worked once, don't know why.
Anyone know what the problem is?
PS. These files aren't getting loaded, anyone know why? Is it because visual studio is 32 bit and my OS is 64 bit?
'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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码可以代替。正如pmg所述,第二个参数应该是1024,而不是2048。当你执行sizeof时,它会返回大小字节。但是 swprintf_s 需要缓冲区中可用的字符数。您可以使用
_countof
,它本质上扩展到与已经向您建议的相同。This code would work instead. As stated by pmg, the second parameter should be 1024, not 2048. When you do sizeof, it will return the size in bytes. However
swprintf_s
expects the number of characters available in the buffer. You can either use_countof
which essentially expands to the same as what has already been suggested to you.