写入初始化缓冲区时 swprintf_s 失败

发布于 2024-12-05 06:10:33 字数 2241 浏览 4 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

机场等船 2024-12-12 06:10:33
int main() {
  wchar_t wBuff[1024] = L"b";
  int test;
  test = swprintf_s(wBuff,_countof(wBuff),L"a%s","test");
}

这段代码可以代替。正如pmg所述,第二个参数应该是1024,而不是2048。当你执行sizeof时,它会返回大小字节。但是 swprintf_s 需要缓冲区中可用的字符数。您可以使用 _countof ,它本质上扩展到与已经向您建议的相同。

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

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.

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