C++:列出赛格故障
C++ : List Seg Fault
谁能解释我为什么会这样?
当我在 if-else 块内调用成员函数 printEvent() 时,我得到了正确的输出,但是当我在块之后调用 printEvent() 时,我得到了这个未知的结果。
list<Event*> currentEvents;
for (j=1; j <= eventsNum; ++j)
{
Event * newEvent;
if (isIndividual(inFile)) {
IndividualEvent newIndi = returnIndi(inFile);
newEvent = &(newIndi);
newEvent->printEvent();
}else {
TeamEvent newTeam = returnTeam(inFile);
newEvent = &(newTeam);
newEvent->printEvent();
}
cout << "WHY?" << endl;
newEvent->printEvent();
currentEvents.push_back(newEvent);
}
TVNode.push_back(newEmission);
}
输出
<Filiko1>
WHY HERE?
<Filiko1q<15-06-2015,14:00<Athens<0`W ����Athensq<Football<1<0>!0����Footballq<2q<Olympiakos<PSG!
����OlympiakosxW^��DW����PSG^��DWPW�^��DWi�n
上面的代码是一个大练习的一部分!
if or else block 后面的newEvent
指向已销毁的对象,因为对象的生存期受定义对象的范围限制。在您的情况下,对象 newIndi
和 newTeam
将在 if-else 语句后被销毁,而指针仍指向其中一个newEvent
。
请参阅此链接以了解C++范围:http://en.cppreference.com/w/cpp/language/scope
相关文章:
- 分段故障(堆芯转储)矢量
- C++中的动态铸造故障
- 数组的指针从不分段故障
- vscode g++链路故障:体系结构x86_64的未定义符号
- 访问被拒绝后,c++中的故障保护代码
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 调试 CUDA MMU 故障
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- 正在处理故障(堆芯转储)
- 在 Boost::fiber 中引发的BOOST_ASSERT故障 Visual Studio "Debug" 构建
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- C++函数过载会导致 SEG 故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 当新的故障时,是否有必要留出一些紧急内存?
- Python & C-C++ 扩展模块案例段故障
- 带升压的 SEG 故障::make_shared / 特征3 内存.h.
- 在 c++ 中,发生故障后是否需要 stream.clear()?
- 分段故障背包问题
- 分段故障 11,从类函数显示动态 C 字符串