本文介绍了运行时检查失败#2 - 变量'length'周围的堆栈已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力改变我们的应用程序以支持unicode特性。因此,在进行更改时,我必须将所有char转换为wchar_t,并且我们有一些包装器流类,我转换为支持wchar_t。现在我的问题是在对以下代码进行更改时遇到运行时检查失败#2 - 变量'length'周围的堆栈已损坏。虽然如果我继续我得到正确的价值观。请让我知道如何摆脱这个错误。



I am working on changing our application to support unicode characteristics . Hence while doing the changes i had to convert all char to wchar_t and we had some wrapper stream classes which i converted to support wchar_t . Now my issue is while doing the change to the below code i encounter Run-Time Check Failure #2 - Stack around the variable 'length' was corrupted. though if i continue i get the proper values . Please let me know how to get rid of this error.

FDIStream&
FDIStream::
operator>>(std::wstring& data)
{
    if (CanReadData())
    {
        int length = -1;
        *this >> length;

        if (length >= 0)
        {
            // See if length is a valid value (not pass eof)
            if (length > GetLength()) {
                throw FDException("Corrupted file");
            }

            wchar_t* buffer = new wchar_t[length];

            try
            {
                ReadBytes(buffer, length);

                data = std::wstring(buffer,length);
            } catch (...)
            {
                delete[] buffer;
                throw;
            }

            delete[] buffer;
        }
    }

    return *this;
}

推荐答案

ReadBytes(buffer, length);



to


to

ReadBytes(buffer, length*sizeof(wchar_t));



如果长度保持字符数,可能会发生同样的问题,因为字符串长度和字节数不再重合。

In这种情况可能你想要改变:


If length hold the count of chars the same problem could happen because the string length and the number of bytes are no more coincident.
In this case maybe you want change:

data = std::wstring(buffer,length);



使用:


With:

data = std::wstring(buffer,length/sizeof(wchar_t));


data = FDCoreUtils::ConvertToFromBigEndianFormat(static_cast(data));



函数因为它有一些字节操作它...



内联字符

FDLoByte(短值)

{

返回static_cast< char>(价值& 0x000000FF);

}



inline int

FDHiWord(int value)

{

return(value>> 16)& 0x0000FFFF;

}



inline int

FDLoWord(int value)

{

返回值& 0x0000FFFF;

}



我是unicode和字符串转换的新手,请让我知道我应该寻找什么以及我应该在哪里看解决此问题。


function as that has some byte manipluations it ...

inline char
FDLoByte(short value)
{
return static_cast<char>(value & 0x000000FF);
}

inline int
FDHiWord(int value)
{
return (value >> 16) & 0x0000FFFF;
}

inline int
FDLoWord(int value)
{
return value & 0x0000FFFF;
}

I am very new to unicode and string conversions please let me know what i should look for and where i should look for to resolve this issue .


这篇关于运行时检查失败#2 - 变量'length'周围的堆栈已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:45
查看更多