Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, am getting core while dereference the list iterator. Hence, i required condition for not executing this lines. I have tried using list empty and size also. but it was no use of it.


#include<iostream>
#include<bits/stdc++.h>
using namespace std;

struct CtxThrottlingContainer
{
   string pci;
   string status;

   CtxThrottlingContainer()
   {
      pci = "";
      status = "";
   }
};

struct CtxThrottlingData
{
   map <int, list<CtxThrottlingContainer> > PendingThrottlingTriggerMap;
   map <string, string> m_ApplicableThrtlProfileMap;
};

map<string,CtxThrottlingData> g_SessionLvlThrottlingMap;

void New_Insertation()
{

   CtxThrottlingData ctx;
#if 0
   CtxThrottlingContainer c1;
   c1.pci = "pci";
   c1.status = "status";

   list<CtxThrottlingContainer> l1;
   l1.push_back(c1);
   ctx.PendingThrottlingTriggerMap.insert(make_pair(123, l1));
   ctx.m_ApplicableThrtlProfileMap.insert(make_pair("maths", "second"));
#endif

   g_SessionLvlThrottlingMap["total_key"] = ctx;
}

int main()
{
   map<string,CtxThrottlingData>::iterator itr = g_SessionLvlThrottlingMap.find("maths");
   if(itr == g_SessionLvlThrottlingMap.end())
   {
      New_Insertation();
   }

   itr = g_SessionLvlThrottlingMap.find("total_key");
   if(itr == g_SessionLvlThrottlingMap.end())
   {
      New_Insertation();
   }
   else
   {
      cout<<"g_SessionLvlThrottlingMap Key value is : "<<itr->first<<endl;

      map<int, list<CtxThrottlingContainer> > ::iterator itr2 = itr->second.PendingThrottlingTriggerMap.begin();
      list<CtxThrottlingContainer>::iterator it4 = itr2->second.begin();
      map <string, string> ::iterator itr3 = itr->second.m_ApplicableThrtlProfileMap.begin();

      cout<<"PendingThrottlingTriggerMap key is : "<<itr2->first<<endl;

      //if(it4 != itr2->second.end());
      if(itr2->second.empty())
      {
      if((it4->pci != "") && (it4->status != ""))
         {
            cout<<"PendingThrottlingTriggerMap list is : "<<it4->pci<<endl;
            cout<<"PendingThrottlingTriggerMap list is : "<<it4->status<<endl;
         }
      }

      itr3 = itr->second.m_ApplicableThrtlProfileMap.find("maths");
      if(itr3 != itr->second.m_ApplicableThrtlProfileMap.end())
      {
         cout<<"m_ApplicableThrtlProfileMap 1st : "<<itr3->first<<endl;
         cout<<"m_ApplicableThrtlProfileMap 2nd : "<<itr3->second<<endl;
      }
   }
}


What I have tried:

Tried list empty and list size but getting core dump continuously help me which is useful for proceed furthur.
Posted
Updated 20-Mar-23 4:43am
Comments
mathiv327 20-Mar-23 9:02am    
Note that : am not insert any values in global map, so if i dereference the value it's gets core. but solve to be required one condition for without those execution.

1 solution

it4 points to the beginning of the list 'itr2->second'. You then check whether 'itr2->second' is empty, and if it is, you attempt to dereference it4. If 'itr2->second' is empty, it4 cannot be dereferenced.

Rewrite your code to invert the condition - if (!itr2->second.empty())
 
Share this answer
 
Comments
mathiv327 22-Mar-23 1:19am    
empty is not working, added condition but run time execution gets into the block
Daniel Pfeffer 22-Mar-23 5:38am    
make sure you are calling !empty(), rather than empty().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900