C ++-运行时检查失败#2-围绕变量'sourceCount'的堆栈已损坏

通哥

我发现了相似的SO问题,但与我的不同位置

我的函数看起来像这样:

BOOL ShallowCopy(const LPVOID psource, LPVOID pdest) {
    LPBYTE ps = reinterpret_cast<LPBYTE>(psource);
    LPBYTE pd = reinterpret_cast<LPBYTE>(pdest);
    ULONG sourceCount = 0, destCount = 0;

    std::copy(ps, ps + 8, checked_array_iterator<LPBYTE>(((LPBYTE)((LPVOID)&sourceCount)), 8)); // Get psource byte count
    std::copy(pd, pd + 8, checked_array_iterator<LPBYTE>(((LPBYTE)((LPVOID)&destCount)), 8));       //  Get pdest byte count

    if (sourceCount != destCount) {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    std::copy(ps, ps + sourceCount, checked_array_iterator<unsigned char *>(pd, destCount));
    return TRUE;
}

当我这样调用函数时:

if (!ShallowCopy(pcsbi, &csbi)) {
    cerr << _T("FATAL: Shallow copy failed.") << endl;
}

系统抛出运行时异常,并说“运行时检查失败2-变量'sourceCount'周围的堆栈已损坏”。

但是,如果将sourceCount和destCount转换为变量,则不会收到此错误:

    BOOL ShallowCopy(const LPVOID psource, LPVOID pdest) {
    LPBYTE ps = reinterpret_cast<LPBYTE>(psource);
    LPBYTE pd = reinterpret_cast<LPBYTE>(pdest);
    LPBYTE pbCount = new BYTE[8];
    ULONG sourceCount = 0, destCount = 0;

    std::copy(ps, ps + 8, checked_array_iterator<LPBYTE>(pbCount, 8));  // Get psource byte count
    sourceCount = *((PULONG)pbCount);
    std::copy(pd, pd + 8, checked_array_iterator<LPBYTE>(pbCount, 8));      //  Get pdest byte count
    destCount = *((PULONG)pbCount);

    delete[] pbCount;

    if (sourceCount != destCount) {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    std::copy(ps, ps + sourceCount, checked_array_iterator<unsigned char *>(pd, destCount));
    return TRUE;
}

当我看这2个函数时,没有什么区别,只是稍后将值存储到变量中,然后转换为目标。那么,究竟是什么导致运行时错误?

阿德里亚诺·雷佩蒂(Adriano Repetti)

ULONG被定义为unsigned long只有32位(4个字节,而不是您的代码中的8个字节)。

之所以出现该错误,是因为您在堆栈分配的变量destCount(或sourceCount,它们在何处以及以什么顺序排列只是实现细节)之后覆盖了内存在第二个示例中,它起作用是因为您分配了足够的内存(pbCount8个字节),这sourceCount = *((PULONG)pbCount);只会复制其中的4个。

我建议使用sizeof而不是硬编码的数据类型大小:

std::copy(ps, ps + sizeof(ULONG)...

请注意,您甚至可以简单地写:

sourceCount = *reinterpret_cast<PULONG>(ps);
destCount = *reinterpret_cast<PULONG>(pd);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C-“运行时检查失败#2-围绕变量'str'的堆栈已损坏。”

基本的C ++错误。运行时检查失败#2-变量'matrix'周围的堆栈已损坏

C - “运行时检查失败 #2 - 变量 'cstringValue' 周围的堆栈已损坏。”

运行时检查失败#2-围绕变量'char'的堆栈已损坏

C / C ++运行时检查失败#2-变量'gradeLetter'周围的堆栈已损坏。Visual Studio2019。如何解决此问题?

C ++:该代码可编译,但引发运行时检查失败#2-变量'num'周围的堆栈已损坏。发生了

调试信息:运行时检查失败 #2 - 变量“IpNetRow2”周围的堆栈已损坏

运行时检查失败 #2 - 变量“obj”周围的堆栈已损坏

运行时检查失败 #2 - 变量“...”周围的堆栈已损坏

运行时检查失败#2-变量'foo'周围的堆栈已损坏

运行时检查失败 #2 - 变量“month1”周围的堆栈已损坏

运行时检查失败#2-变量'sortObject'周围的堆栈已损坏。怎么修?

运行时检查失败#2-变量'd'周围的堆栈已损坏

运行时检查失败#2-变量“ primes”周围的堆栈已损坏

运行时检查失败#2-变量'result'周围的堆栈已损坏

运行时检查失败 #2 - 变量“numberchoices”周围的堆栈已损坏

C-围绕变量“ ch1”的堆栈已损坏

双链表C-围绕变量'list'的堆栈已损坏

为什么会出现运行时检查失败#2-变量'x'周围的堆栈已损坏?

Eigen RowVector超出范围会产生“运行时检查失败#2-变量X周围的堆栈已损坏”

变量周围的堆栈已损坏 - C

变量周围的堆栈已损坏 C++

变量“userStr”周围的堆栈已损坏 (C)

围绕变量''的C ++堆栈已损坏。(D3D9)

运行时检查失败 #2 - 变量“myArray”周围的堆栈已损坏。- 似乎无法弄清楚如何消除此错误

得到一个错误,指出“运行时检查失败#2-变量'rejected'周围的堆栈已损坏。(Visual Studio)

围绕变量“产品”的堆栈已损坏

变量“变量名”周围的堆栈已损坏C ++

C:变量's'周围的堆栈错误已损坏