문제

I am getting an invalid null pointer error from this section of code. I am guessing it is something to do with the strings, but as I have only recently learnt about them, I cannot find the problem.

string batchCipherFiles()
{
int count(0);
string text, file; 
ifstream inputFile;

cout << " How many files do you wish to encrypt?: ";
cin >> count;
for (int i(1) ; i <= count ; ++i)
{
    stringstream ss;
    ss << "unencrypted" << i << ".txt";
    file = ss.str();
    inputFile.open(file.c_str(), ios::in);
    if (inputFile.fail()) 
    {
        cout << "\n An error has occurred.";
    }
    else
    {
        while (!inputFile.eof())
        {
            getline(inputFile, text);
            cout << "\n " << file << " = " << text << "\n\n";
            int applyCeasarShift(string,string);        
            applyCeasarShift(text, file);
        }
        inputFile.close();
    }
}

return (0);
}

Here is the debug line from xstring:

 #ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{   // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
        ::_CrtDbgBreak();
    }
}

Thanks in advance for any help.

EDIT: The error occurs on the Return statement, and in the extract from xstring, the yellow arrow is pointing to "::_CrtDbgBreak();"

EDIT 2:

xstring:
basic_string(const _Elem *_Ptr)
    : _Mybase()
    {   // construct from [_Ptr, <null>)
    _Tidy();
    assign(_Ptr);
    }

xutility:
template<class _Ty> inline
void _Debug_pointer(const _Ty *_First, _Dbfile_t _File, _Dbline_t _Line)
{   // test iterator for non-singularity, const pointers
if (_First == 0)
    _DEBUG_ERROR2("invalid null pointer", _File, _Line);
}

stdthrow:
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{   // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
        ::_CrtDbgBreak();
    }
}

Hopefully this is the right stuff.

도움이 되었습니까?

해결책

The problem is that string is the return type for batchCipherFiles but return (0); is being returned: either change the return type or return a string.

I think the return (0); will be implicitly converted to std::string((char*)0);, causing the crash. The documentation for std::string constructor states:

Constructs the string with the contents of null-terminated character string pointed to by s. The length of the string is determined by the first null character. s must not be a NULL pointer.

다른 팁

If the error is occurring on the return statement, it's likely that some other code is using the return value as a pointer value. The C++ Standard specifies that a 0 is treated the same as NULL when assigned to a pointer, so that's probably your problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top