加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:37:21 所属栏目:百科 来源:网络整理
导读:我正在使用visual studio 2010在C语言中编写一个程序.我使用swprintf_s函数将格式化的字符串写入wchar_t缓冲区. 当我尝试写入已初始化的缓冲区时,我收到以下错误. Unhandled exception at 0x77b3fbda in svats.exe: 0xC00000FD: Stack overflow. 而有时 Unha
我正在使用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:WindowsSysWOW64ntdll.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64kernel32.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64KernelBase.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64user32.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64gdi32.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64lpk.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64usp10.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64msvcrt.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64advapi32.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64sechost.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64rpcrt4.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64sspicli.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64cryptbase.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64ws2_32.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64nsi.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64imm32.dll',Cannot find or open the PDB file
'svats.exe': Loaded 'C:WindowsSysWOW64msctf.dll',Cannot find or open the PDB file

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读