开发者

C++ Debug Assertion Error

So, I've been playing around with c++ a bit and decided to write a program that involves opening and writing to a fil开发者_开发技巧e in binary mode. I am not too familiar with the iostream functionality of c++ (I mostly do API based programming), but I read several technical guides on the subject and wrote some code. The code is meant to open one file, read it's data to a buffer, and then convert that buffer to another format and write it to another file. The problem is that it keeps throwing a "Debug Assertion" error which apparently revolves around the invalid use of a null pointer. However, I couldn't make sense of it when I looked through the code. I probably just misused the iostream library or made a simple logic error. I need to have the separate SetMemBlock function as I plan on using the same base for formatting different output on a variety of functions. This is just my prototype. Anyways, here's my quick n' dirty class setup:

const DebugMode = true;

class A
{
public:
    bool FileFunction( const char *, const char * );

protected:
    bool SetMemBlock( char *, std::fstream &, std::streamoff & );

private:
    std::fstream SrcFileStream;
    std::fstream DestFileStream;
};

bool A::SetMemBlock( char* MemBlock, std::fstream & FileStream, std::streamoff & Size )
{
    std::streamoff TempOff = 0;
    //This is meant to check for a non-empty buffer and to see if the stream is valid.
    if( MemBlock != 0 || !FileStream.is_open() )
        return false;

    TempOff = FileStream.tellg();
    FileStream.seekg(0, std::ios::end);
    Size = FileStream.tellg();

    MemBlock = new( std::nothrow ) char[ (int) Size ];

    if( MemBlock == 0 )
        return false;

    FileStream.seekg(0, std::ios::beg);
    FileStream.read( MemBlock, (int) Size );

    if( !FileStream )
        return false;

    FileStream.seekg(TempOff);
    return true;
}

bool A::FileFunction( const char * SrcFile, const char * DestFile )
{
    char * MemBlock = 0;
    std::streamoff Size = 0;

    SrcFileStream.open( SrcFile, std::ios::binary | std::ios::in );
    DestFileStream.open( DestFile, std::ios::binary | std::ios::out );

    if( !SrcFileStream.is_open() || !DestFileStream.is_open() )
        return false;

    if( DebugMode )
    {
        std::cout<<"Files opened succesfully...\nNow writing memory block..."<<std::endl;
    }

    if( !SetMemBlock( MemBlock, SrcFileStream, Size ) )
    {
        std::cout<<"An error occured when reading to memory block!"<<std::endl;
        return false;
    }

    if( DebugMode )
    {
        std::cout<<"Memory block written..."<<std::endl;
    }

    DestFileStream.seekp( std::ios::beg );
    DestFileStream.write( MemBlock, Size );

    SrcFileStream.close();
    DestFileStream.close();
    delete[] MemBlock;
    return true;

}


You're passing MemBlock to SetMemBlock by value. The function therefore just sets the value of a local copy, which has no effect on the calling function; the value of MemBlock in the calling function thus remains garbage. Using it as a pointer will probably then lead to an assertion (if you're lucky) or an out-and-out crash (if you're not.) You want to pass that argument by reference instead.

If you don't know what these terms mean, Google "pass by value" and "pass by reference". You really need to understand the difference!


Pass MemBlock by reference:

bool A::SetMemBlock( char*& MemBlock, std::fstream & FileStream, std::streamoff & Size )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜