SIGSEGV我的程序出现故障
SIGSEGV Fault with my program
IDE
代码块
明32制造3.82.90
下面是我的程序的调用堆栈
调用堆栈
0 0x45cb6b std::string::assign(std::string const&)()(??:?)
1 0x1??()(??:?)
2 0xf72ac0??()(??:?)
30x402332商店::initItem(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:107)
4 0x4020e3商店::initMenu(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:68)
50x402093商店::initShop(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:54)
60x401f58商店:商店(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:46)
70x4013c1 main()(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\main.cpp:10)
问题发生在#3。以下是相关部分的代码片段
1.导致SIGSEGV故障的方法
bool Shop::initItem()
{
int count = 0, tempPrice;
std::string input;
std::stringstream tempSTRM;
std::vector <Item>::iterator itemITR = menu.items.begin();
Item tempITM;
do
{
std::cout << "Input item name for item[" << count+1 << "]t:t";
std::getline(std::cin, input);
if(input == "0")
break;
tempITM.itemName = input;
std::cout << "Input the price for the item[" << count+1 << "]t:t";
std::getline(std::cin, input);
tempSTRM.str(input);
tempSTRM >> tempPrice;
tempITM.price = tempPrice;
menu.items.push_back(tempITM);
tempITM = *itemITR; //this line is causing the problem
std::cout << tempITM.itemName << "t" << tempITM.price << "n";
itemITR++;
count++;
}while(input !="0");
return false;
}
2.菜单结构
struct MenuItems
{
std::string menuName;
std::vector <Item> items;
};
typedef struct MenuItems Menu;
3.项目结构
struct Item
{
std::string itemName;
int price;
bool promoStatus;
double promoDiscount;
};
typedef struct Item Item;
4 Main(只有代码的这一部分与我现在正在构建的内容有关)
#include <windows.h> //leftover code from previous experiment
#include <tchar.h> //leftover code from previous experiment
#include "CoffeeShop.h"
using namespace std;
int main()
{
Shop* CoffeeShop = new Shop();
//Other leftover codes
return 0;
}
我的意图是:获得我试图推动的任何东西的输出(因为我正在构建项目,所以一旦其他部分准备好,我就可以自信地产生输出。)
由于这会引发SIGSEGV问题,所以我目前的想法行不通。尝试过放置指针对象Item并访问itemITR,但也不起作用。任何人都可以揭露我(在意识形态上)做错了什么,我应该怎么做?
--编辑--
Shop::Shop()
{
if(!initShop())
std::cout << "Object creation succeeded!n";
else
std::cout << "Object creation failed!n";
}
bool Shop::initShop()
{
if(!initMenu())
{
std::cout << "Object creation succeeded!n";
return false;
}
else
{
std::cout << "Object creation failed!n";
return true;
}
}
bool Shop::initMenu()
{
if(!initItem())
{
std::cout << "Object Creation succeeded!n";
return false;
}
else
{
std::cout << "Object creation failed!n";
return true;
}
}
流程->Shop构造函数调用initShop,然后initShop调用initMenu,initMenu调用initItem
商店->initShop->initMenu->initItem
--编辑2--
已解决。增加
itemITR = menu.items.begin();
线后
menu.items.push_back(tempITM);
谢谢!
迭代器是用给定的向量初始化的:
std::vector <Item>::iterator itemITR = menu.items.begin();
然后在向量中插入一些东西:
menu.items.push_back(tempITM);
这可能会使迭代器无效。因此,取消引用它是UB,并在此处导致segfault
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- 对程序故障进行分段
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 程序接收了信号sigsegv,在代码块中调试时的细分故障
- lambda回调中Android Cocos2D-X应用程序上的SEG故障
- G :内部编译器错误:分割故障(程序CC1PLUS) - 我在哪里开始
- 当使用e时没有空格时,奇怪的程序故障
- 程序将拒绝进度的奇怪故障(刽子手分配)
- 二进制搜索树操作程序错误:分割故障(核心倾倒)
- 复制CORBA ::任何包含字符串的corba程序SEG故障
- 程序执行因细分故障而停止:11
- GDB执行时,Apache IGNITE C 客户端应用程序具有许多细分故障
- 程序收到信号sigsegv,分段故障.在al_draw_tinted_bitmap中(bitmap = 0x0,tint
- PRNG程序故障.无法输入随机数量的选择,并且将始终从PRNG中回答2
- 将程序的故障与模板Main联系起来
- 分割故障程序编译为共享库,但不静态
- 在一个动态分配的数组的程序中分割故障
- C 程序获取细分故障
- 当程序遇到故障时,如何在信号处理函数中写入日志和上传日志到MYSQL
- 一个简单的分段故障程序