数组在用new分配内存后无法使用_countof如何解决

当我按照下面这样写是没有问题的,但可能有栈溢出,需要用new

WCHAR wSystemDirectory[260];
rsize_t destsz1 = GetSystemDirectoryW(wSystemDirectory, MAX_PATH);
wcscat_s(wSystemDirectory, _countof(wSystemDirectory), slash);

但是用了new过后,即下面代码

WCHAR * wSystemDirectory=new WCHAR[260];
rsize_t destsz1 = GetSystemDirectoryW(wSystemDirectory, MAX_PATH);
wcscat_s(wSystemDirectory, _countof(wSystemDirectory), slash);

由于是指针_countof无法使用

请问有没有一个办法解决这个问题,即又要在堆上分配内存,又可以使用_countof
    

看不懂但是支持一下

我自己想出了一个方法,新建一个函数如下

WCHAR * getpath(WCHAR * pathmem, wchar_t * add)
{
    WCHAR path[260];
    wcscpy_s(path, _countof(path), pathmem);
    wcscat_s(path, _countof(path), add);
    return path;
}

然后在主函数中

WCHAR *pathmem=new WCHAR[260] ;
WCHAR *wSystemDirectory=new WCHAR[260];
rsize_t destsz1 = GetSystemDirectoryW(pathmem, MAX_PATH);
wSystemDirectory = getpath(pathmem, slash);

这样返回到主函数中的值就是在堆中,也只有一个destsz在栈中,不会存在内存溢出

具体为啥这么就行了,我讲不清,有大神知道的可以说一下,感激不尽

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答

本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。

​​​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。