Reputation: 1
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
map<string,int>m;
string name;
int choice, marks;
while(n>0)
{
cin>>choice>>name;
if(choice==1)
{
cin>>marks;
map<string,int>::iterator itr=m.find(name);
if(itr==m.end())
{
m.insert(make_pair(name,marks));
}
else{
itr->second=itr->second+marks;
}
}
else if(choice==2)
{
map<string,int>::iterator itr=m.find(name);
itr->second=0;
}
else if(choice==3){
map<string,int>::iterator itr=m.find(name);
cout<<itr->second<<endl;
}
n--;
}
return 0;
}
[New LWP 144748] [Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault. #0 0x00007f4200000000 in ?? ()
To enable execution of this file add add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py line to your configuration file "//.gdbinit".
To completely disable this security protection add set auto-load safe-path / line to your configuration file "//.gdbinit".
For more information about this security protection see the "Auto-loading safe path" section in the GDB manual. E.g., run from the shell: info "(gdb)Auto-loading safe path"*
Upvotes: 0
Views: 338
Reputation: 1
What is the input to your program?
A segmentation fault occurs when you access memory outside of your memory location.
I suspect choice 2 and choice 3 can cause segmentation fault anytime. Because even though the input name is not present in the map, you are trying to change its value. Its not safe.
Upvotes: 0