2012-04-29 4 views
-1

이 코드 섹션에서 잘못된 널 포인터 오류가 발생합니다. 나는 그것이 문자열과 관련이 있다고 추측하고 있지만, 최근에야 그들에 대해 배웠기 때문에 문제를 발견 할 수 없습니다. 사전에 어떤 도움Null 포인터가 잘못되었습니다 - C++

#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(); 
    } 
} 

감사 :

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); 
} 

여기 xstring에서 디버그 라인입니다.

편집 : 오류가 Return 문에서 발생하고 xstring에서 추출 할 때 노란색 화살표가 ":: _ CrtDbgBreak();"를 가리키고 있습니다.

편집 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(); 
    } 
} 

는 희망이 오른쪽 물건입니다.

+1

어떤 줄에 오류가 발생합니까? – Chris

+1

스택 트레이스의 맨 위를 보여주는 것은 완전히 쓸모가 없습니다. 원본 라인은 무엇입니까? –

+0

전체 스택 추적을 표시하십시오. 우리는 무엇이'_CrtDbgBreak()'인지, 무엇이 _that_ 함수인지 등을 알아야합니다. –

답변

4

batchCipherFiles에 대한 반환 유형은 string이지만, return (0);이 반환되는 문제는 반환 유형을 변경하거나 string을 반환하는 것입니다.

나는 이 암시 적으로 std::string((char*)0);으로 변환되어 충돌을 일으킬 것이라고 생각합니다. std::string constructor에 대한 설명서는 다음과 같이 설명합니다.

s가 가리키는 null 종료 문자 스트링의 내용으로 문자열을 구성합니다. . 자 열의 길이는 첫 x 째 널 문자에 의해 판별됩니다. s는 NULL 포인터가 아니어야합니다.

+0

함수를 int로 변경했는데 작동합니다! 대단히, 아주 빠른 답장을 주셔서 감사합니다. – user1364552

+0

@ user1364552 : 값을 반환 할 필요가 없으면'void'를 사용하십시오. 나는. 'void batchCipherFiles() {/ **** /; 반환; }' – MSalters

1

return 문에서 오류가 발생하는 경우 다른 코드에서 반환 값을 포인터 값으로 사용하고있을 가능성이 큽니다. C++ 표준에서는 포인터에 할당 될 때 0이 NULL과 동일하게 처리되도록 지정하므로 문제 일 가능성이 큽니다.

+0

정확 : "other code"는'string :: string (const char *)'입니다. – MSalters