无效的空指针 - C++
Invalid Null pointer - C++
我从这段代码中收到无效的空指针错误。我猜这与琴弦有关,但由于我最近才了解它们,我找不到问题所在。
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 << "nn";
int applyCeasarShift(string,string);
applyCeasarShift(text, file);
}
inputFile.close();
}
}
return (0);
}
这是来自 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();
}
}
提前感谢任何帮助。
编辑:错误发生在 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();
}
}
希望这是正确的东西。
问题是string
是batchCipherFiles
的返回类型,但返回return (0);
:要么更改返回类型,要么返回string
。
我认为return (0);
将被隐式转换为std::string((char*)0);
,导致崩溃。std::string
构造函数的文档指出:
使用以 s 指向的以 null 结尾的字符串的内容构造字符串。字符串的长度由第一个空字符确定。s 不得为 NULL 指针。
如果 return 语句上发生错误,则可能是其他代码正在使用返回值作为指针值。C++ 标准指定在分配给指针时将 0 视为与 NULL 相同,因此这可能是您的问题。
相关文章:
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 运行时错误:引用绑定到类型为"int"的空指针
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 为什么我在空指针错误(链表)中获取成员访问权限
- 从向量到空指针的 memcpy(反之亦然)不起作用
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 成员访问是否在空指针上定义C++?
- 尝试将对象插入空指针数组时出现分段错误
- 为什么我们需要在 C++ 中检查空指针,而在 Java 中不需要?
- 是否允许向空指针添加零?
- 为什么 C 样式字符串的工作空指针检查不?
- 在函数内初始化无符号字符指针将返回空指针
- 为什么多维数组中的空字符串文本衰减为空指针?
- C++ 取消引用指向矢量的空指针时的分段错误
- 错误 C6011:取消引用空指针"NAME"。C++
- cppcheck取消引用空指针
- 打印空指针时,std::cout 可以打印 "NULL" 而不是 0 吗?
- 递增空指针无法正确设置值
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 将多映射转换为空指针,然后转换回多映射