0

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.

5
  • 1
    on which line happens the error ? Commented Apr 29, 2012 at 19:11
  • 1
    showing the top of the stacktrace is completely useless.. what's the originating line? Commented Apr 29, 2012 at 19:11
  • Please show the entire stack trace. We need to know what called _CrtDbgBreak(), and what called that function, and so on. Commented Apr 29, 2012 at 19:16
  • Do the files unencrypted1.txt, unencrypted2.txt, etc. exist on your system? Does the error go away if you give a full path? Commented Apr 29, 2012 at 19:20
  • These files do exist, yes. I haven't tried that but it needs to work on more than one computer so I don't know if that would work. Commented Apr 29, 2012 at 19:22

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I changed the function to an int and it works! Thank you very much, very quick reply too.
@user1364552: If you don't need to return a value, use void. I.e. void batchCipherFiles() { /****/; return; }
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.