c++ - 在 Visual Studio 2005 中使用 string::iterator 的空指针问题

标签 c++ string visual-studio-2005 iterator

我正在处理一些遗留代码。在以下场景中,遗留代码在生产模式下工作。我正在尝试构建遗留代码的命令行版本以用于测试目的。我怀疑这里存在环境设置问题,但我对 C++ 和 Visual Studio 比较陌生(长期使用 eclipse/java 的人)。

此代码试图从流中读取字符串。它读得很短,在我的调试场景中它的值为 11。然后,它应该读入 11 个字符。但是这段代码在第一个字符上就乱码了。具体来说,在下面的 read 方法中,ptr 为 null,因此 fread 调用会抛出异常。为什么 ptr 为 NULL?

需要澄清的是,ptr 在 operator>>(string) 和 operator>>(char) 调用之间变为 null。

Mystream& Mystream::operator>>( string& str )
{
string::iterator                it;
short                           length;

*this >> length;

if( length >= 0 )
{
    str.resize( length );
    for ( it = str.begin(); it != str.end(); ++it )
    {
        *this >> *it;
    }
}

return *this;
}

读取短片的方法在这里,查看文件缓冲区等。这看起来工作正常。

Mystream& Mystream::operator>>(short& n )
{
    read( ( char* )&n, sizeof( n ) );
    SwapBytes( *this, ( char* )&n, sizeof( n ) );
    return *this;
}

现在读入一个char的方法是:

Mystream& Mystream::operator>>(char& n )
{
    read( ( char* )&n, sizeof( n ) );
    return *this;
}

读取方法是:

Mystream& Mystream::read( char* ptr, int n )
{
fread( (void*)ptr, (size_t)1, (size_t)n, fp );
return *this;
} 

有一件事我不明白,在字符串输入法中,*it 是一个字符对吗?那么为什么 operator>>(char &n) 方法会在该行上被调度?在调试器中,它看起来像 *it 是一个 0,(尽管一位同事告诉我他在这些事情上不信任 2005 调试器)因此,看起来 &n 被视为空指针,因此读取方法抛出异常。

您能提供的任何见解都将非常有帮助!

谢谢 约翰

附言。出于好奇,Swap Bytes 看起来像这样:

inline void SwapBytes( Mystream& bfs, char * ptr, int nbyte, int nelem = 1)
{ 
    // do we need to swap bytes?
if( bfs.byteOrder() != SYSBYTEORDER )
    DoSwapBytesReally( bfs, ptr, nbyte, nelem );
}

DoSwapBytesReally 看起来像:

void DoSwapBytesReally( Mystream& bfs, char * ptr, int nbyte, int nelem )
{
    // if the byte order of the file
    // does not match the system byte order
    // then the bytes should be swapped
int i, n;
char temp;

#ifndef _DOSPOINTERS_
char *ptr1, *ptr2;
#else _DOSPOINTERS_
char huge *ptr1, huge *ptr2;
#endif _DOSPOINTERS_

int nbyte2;

nbyte2 = nbyte/2;

for ( n = 0; n < nelem; n++ ) 
{
    ptr1 = ptr;
    ptr2 = ptr1 + nbyte - 1;

    for ( i = 0; i < nbyte2; i++ ) 
    {
        temp = *ptr1;
        *ptr1++ = *ptr2;
        *ptr2-- = temp;
    }

    ptr += nbyte;
}
}

最佳答案

我会扔掉这个烂摊子并重新开始。从代码推断,如果你实际工作过,它大致相当于这样的东西:

MyStream::operator>>(string &s) { 
    short size;

    fread((void *)&size, sizeof(size), 1, fP);
    size = ntohs(size); // oops: after reading edited question, this is really wrong.
    s.resize(size);
    fread((void *)&s[0], 1, size, fp);
    return *this;
}

在这种情况下,将大部分工作委派给其他职能部门似乎并没有太大收获——这更直接地完成了工作,但仍然没有比原来的更长或更复杂(如果有的话,我会说相反的话)。

关于c++ - 在 Visual Studio 2005 中使用 string::iterator 的空指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798771/

相关文章:

c++ - 使用 C++ 驱动程序在 Cassandra 中配置连接池

c++ - 重载运算符 : operand order when using C++ literals

regex - R:提取包含在两个子字符串之间的所有子字符串的最快方法

python - 如何将字符串数组转换为字符串数组?

c++ - 为什么在输入变量 b 时出现错误。(第 8 行)

C++ 简单指针传递

java - 计算行数和单词数

c++ - 如何在 Windows 上构建 x64 Boost 库?

c++ - 如何在 Visual Studio 2005 中创建 ATL/C++ ActiveX DLL

c++ - 从 Visual Studio 2005 移植到 2008 或更高版本