本文介绍了wcscat_s函数-缓冲区错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单:这段代码有什么用?

The question is simple: what's frong with this piece of code?

  size_t  buff = 1;
  size_t  new_buff;
  WCHAR   *var_path;
  WCHAR   *dir_path;

  var_path = new WCHAR[buff];
  new_buff = GetEnvironmentVariableW(L"APPDATA", var_path, buff);

  if (new_buff == 0) {
    return 1;
  } else if (new_buff > buff) {
    delete[] var_path;

    var_path = new WCHAR[new_buff];
    GetEnvironmentVariableW(L"APPDATA", var_path, new_buff);
  }

  dir_path = new WCHAR[new_buff];
  wcscpy_s(dir_path, new_buff, var_path);
  wcscat_s(dir_path, new_buff, L"\\directory");

它表示wcscat_s

推荐答案

您只为dir_path分配了new_buff个字符(并告诉wcscat_s该大小),然后您希望在其后附加更多字符.您需要分配new_buff plus 的长度L"\\directory",并告知wcscat_s该实际大小.

You only allocate new_buff characters for dir_path (and tell wcscat_s about that size), then you want to append more characters to it. You need to allocate new_buff plus the length of L"\\directory", as well as tell wcscat_s about that actual size.

这篇关于wcscat_s函数-缓冲区错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 00:51