Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 955379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:22:20+00:00 2026-05-16T00:22:20+00:00

I have a problem putting data into an unordered_map using a struct as key:

  • 0

I have a problem putting data into an unordered_map using a struct as key:


typedef struct osdpi_flow_identificator {
 u32 lower_ip;
 u32 upper_ip;
 u16 lower_port;
 u16 upper_port;
 u8 protocol;
} osdpi_flow_identificator_t;

// custom hash function for osdpi_flow_identificator
struct osdpi_flow_hash {
 std::size_t operator() (osdpi_flow_identificator * key) const {
  hash hash_function;
  std::size_t returnValue =
   hash_function(key->lower_ip)
   + hash_function(key->upper_ip)
   + hash_function(key->lower_port)
   + hash_function(key->upper_port)
   + hash_function(key->protocol);

  printf(" calculated hash: %i\n", returnValue);

  return returnValue;
 }
};

typedef unordered_map <osdpi_flow_identificator*, osdpi_flow*, osdpi_flow_hash> osdpi_flows_hashmap;
typedef osdpi_flows_hashmap::value_type osdpi_flows_pair;
static osdpi_flows_hashmap osdpi_flows;

My problem is that the hash function returns the same value for osdpi_flow_identificators having the same value, but


 osdpi_flow_identificator * flow_id = new osdpi_flow_identificator;
 osdpi_flows_hashmap::const_iterator iter;
 iter = osdpi_flows.find(flow_id);
 if (iter != osdpi_flows.end()) {
  ...

doesn’t find it, although an entry with a flow_id having exactly the same values, is already in the hash map. I verified it, by outputting the whole hash map and also the hash function prints out the same value. So it’s quite hard for me to understand, why unordered_map can’t find the entry with the same hash.

I also tried to overload operator== and operator<, which I sometimes found as tip in the net, but those functions also were not called.

What solves the issue, but, of course, later ends up with a segmentation fault, is to leave flow_id uninitialized – then the entry can be found correctly.

Any help is appreciated!

Thanks
Steffen

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:22:21+00:00Added an answer on May 16, 2026 at 12:22 am

    Ok, I finally found it 🙂

    In fact you need to define an EqualOperator for your hash, since you are using pointers.
    It should be something like :

    struct Eq
    {
      bool operator() ( osdpi_flow_identificator * id1, osdpi_flow_identificator * id2) const
      {
        id1->lower_ip == id2->lower_ip && // compare all fields.
      }
    };
    

    then your hashmap declaration becomes

    typedef unordered_map <osdpi_flow_identificator*, osdpi_flow*, osdpi_flow_hash, Eq> osdpi_flows_hashmap;
    

    Another way to do it is to store objects in the hashmap, instead of pointers. It depends if you really want pointers in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

THE PROBLEM I have four game servers collecting data. Putting data into a single
I have problem while loading data into html select when users press or click
I have a dataset that is showing the correct data, but putting it into
I have been putting some data I have into my database. One of the
I have a simple problem while putting text in multiple table! please have a
I have problem with my query on C, I’m using the oci8 driver. This
I have problem SIMILAR to preventing form data reposting, but not quite the same
I'm using a JMS queue to read from and insert data into a postgres
I am putting together a WCF Data Service for PatientEntities using Entity Framework. My
i have a problem with getting content from a XML into a mysql database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.