Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么删除[]操作会出错?_C++_Arrays_Runtime Error_Heap_Delete Operator - Fatal编程技术网

C++ 为什么删除[]操作会出错?

C++ 为什么删除[]操作会出错?,c++,arrays,runtime-error,heap,delete-operator,C++,Arrays,Runtime Error,Heap,Delete Operator,我在使用wchar\t数组后删除了它,但失败: const wchar_t t1[] = L"A string"; const wchar_t* t2 = L"Other string"; wchar_t* w = new wchar_t[wcslen(t1) + wcslen(t2) + 1]; int len = swprintf_s(w, wcslen(w), L"%s%s", t1, t2); w[len] = 0; delete[] w; 我在delete[]w失败,错误为写入

我在使用wchar\t数组后删除了它,但失败:

const wchar_t t1[] = L"A string";
const wchar_t* t2 = L"Other string";

wchar_t* w = new wchar_t[wcslen(t1) + wcslen(t2) + 1];

int len = swprintf_s(w, wcslen(w), L"%s%s", t1, t2);
w[len] = 0;

delete[] w;
我在
delete[]w失败
,错误为
写入堆缓冲区的末尾,但我在本地检查
w
是否正常:


如何修复此错误?

已解决,正如@Jonathan Potter指出的,错误是:

swprintf_s(w,wcslen(w),…w最初是一个未初始化的缓冲区,因此对其调用wcslen是不正确的。要将缓冲区的大小传递给swprintf_s吗


C没有代码<删除> /COD>运算符。将它作为C++进行延迟。您可能在其他地方有UB。请给出一个实际的测试用例,正如您在如何询问中所做的那样。<代码> SWPROTFFS(W,WCSCLIN(W))。,…
w
最初是一个未初始化的缓冲区,因此对其调用
wcslen
是不正确的。您希望将缓冲区的大小传递给
swprintf\u s
@JonathanPotter您是对的。这是错误,请勿发布文本图像。请复制/粘贴文本!