开发者

Segmentation fault when passing to a function "map containers" by reference

开发者 https://www.devze.com 2023-01-27 03:23 出处:网络
Greetings everyone, I have a map of maps typedefboost::unordered_map <flow_t, individual_flow_table, fhash, fequal_to>flow_redundancy_map_t;

Greetings everyone,

I have a map of maps

typedef  boost::unordered_map <flow_t, individual_flow_table, fhash, fequal_to>   flow_redundancy_map_t; 

flow_t is a structure of 5 unsigned integers, fhash and fequal_to are done using the std way as prescribed in boost library.

   struct individual_flow_table{

redundancy_t flow_redundancy; 
     //flow_redundancy is again a map -please see typedef below

unsigned long long redundant_bytes;

unsigned long long num_accesses;

unsigned long long flow_size;

 };

typedef map <unsigned long long , pkt_data> redundancy_t; 
  //pkt_data is a structure of 3 unsigned long long

 flow_redundancy_map_t sorted_flows;

I insert into sorted_flows "individual_flow_tables" by initializing the longs and then clearing the "flow_redundancy" map.

Later I access the map "sorted_flows" using "flow_t" as the key and I get the "flow_redundancy" map as the value (Code below)

          local_flow_redundancy = it->second.redundant_bytes;

      local_num_accesses = it->second.num_accesses;

       winnow(it->second.flow_redundancy);

       it->second.redundant_bytes = local_flow_redundancy ;

       it->second.num_accesses = local_num_accesses ;

           /* More code */

          void winnow(redundancy_t & red_table)

          {         unsigned long long some_key; 
                    pkt_data  some_value;
                  //more code for computation of some_key 

                  redundancy_t::iterator it = red_table.find(some_key);

                  if(it!=red_table.end())

                  /*Some updating of the red_table*/
                  else

                     red_table.ins开发者_运维知识库ert(make_pair(some_key, some_value));

            }                  

I get a segmentation fault on the insert to "red_table". I run with valgrind it gives me "Conditional jump or move depends on uninitialised value(s)" at the insert line of the code.

Alternatively, instead of passing by reference "red_table" in function "winnow", I indexed again (using the same flow_t) into the "sorted_flow" map and got the "red_table" in place. It worked fine.

I suppose it has to do something with passing by reference. I am a bit green on C++, though reasonably proficient in C. Any pointers will be very helpful. I can also provide more clarification if needed.

Thank You Govind


I don't think there's enough information here to be able to advise you directly. (It would help if the code samples were better formatted, but that's only part of the problem.)

However, you could try breaking down the code that's failing into separate lines, so that you can get a better sense of what causes the failure. For example:

    pair<unsigned long long, pkt_data> tmpValue = make_pair(some_key, some_value);
    red_table.insert(tmpValue);

(EDIT: Note that red_table is (apparently) a map of unsigned long long --> pkt_data. How, then, does it make sense to insert the result of make_pair(some_key, some_value) when some_value is an unsigned long long?)

You could also try initializing some_key and some_value, and see if that makes the valgrind error go away:

unsigned long long some_key = 0, some_value = 0;

and if so, try initializing one at a time:

unsigned long long some_key = 0, some_value;

and

unsigned long long some_key, some_value = 0;

to see which of these is producing the uninitialized value warning.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号