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 8886127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:28:57+00:00 2026-06-14T21:28:57+00:00

I have two maps to store a list of User objects as values. The

  • 0

I have two maps to store a list of User objects as values. The keys for those values are a uint32_t and a SocketAddress struct as defined below.

The first map inserts values just fine but looking at the locals while debugging, I can see that the second doesn’t seem to insert values at all. Here’s the relevant code:

SocketAddress:

struct SocketAddress {
    sockaddr from;
    socklen_t fromlen;
    SocketAddress& operator=(const SocketAddress &source);
    bool operator==(const SocketAddress &source) const;
    bool operator!=(const SocketAddress &source) const;
    bool operator<(const SocketAddress &source) const;
    bool operator>(const SocketAddress &source) const;
    bool operator<=(const SocketAddress &source) const;
    bool operator>=(const SocketAddress &source) const;
};

Socket::SocketAddress& Socket::SocketAddress::operator=(const SocketAddress &source) {
    memcpy(&from, &source.from, source.fromlen);
    fromlen = source.fromlen;
    return *this;
}

bool Socket::SocketAddress::operator==(const SocketAddress &source) const {
    return (fromlen == source.fromlen && memcmp(&from, &source.from, fromlen) == 0);
}
bool Socket::SocketAddress::operator!=(const SocketAddress &source) const {
    return !this->operator==(source);
}
bool Socket::SocketAddress::operator<(const SocketAddress &source) const {
    return (fromlen < source.fromlen || memcmp(&from, &source.from, fromlen) < 0);
}
bool Socket::SocketAddress::operator>(const SocketAddress &source) const {
    return (fromlen > source.fromlen || memcmp(&from, &source.from, source.fromlen) > 0);
}
bool Socket::SocketAddress::operator<=(const SocketAddress &source) const {
    return !this->operator>(source);
}
bool Socket::SocketAddress::operator>=(const SocketAddress &source) const {
    return !this->operator<(source);
}

User Constructor and associated variables:

std::map<uint32_t, std::shared_ptr<User>> User::_usersListBySession;
std::map<Socket::SocketAddress, std::shared_ptr<User>> User::_userListByAddress;
std::atomic<unsigned int> User::_nextSessionID = 0;

User::User(const Socket::SocketAddress& addr) {
    address = addr;

    sessionID = ++_nextSessionID;

            // This seems to work just fine
    _usersListBySession.insert(std::pair<uint32_t, std::shared_ptr<User>>(sessionID, std::shared_ptr<User>(this)));
            // This does not
    _userListByAddress.insert(std::pair<Socket::SocketAddress, std::shared_ptr<User>>(addr, std::shared_ptr<User>(this)));
}

Definition of address:

    const Socket::SocketAddress& address

Segment of code that doesn’t operate as intended.

    std::shared_ptr<User> user = User::getUserWithAddress(address);
    if (!user) {
        user = std::shared_ptr<User>(new User(address));
    }

Map search functions:

std::shared_ptr<User> User::getUserWithAddress(const Socket::SocketAddress& addr) {
    return _userListByAddress[addr];
}

std::shared_ptr<User> User::getUserWithSessionID(uint32_t sessionid) {
    return _usersListBySession[sessionid];
}

When making the call to User::getUserWithAddress(address), the user returned has no user! Looking at the pair in memory, it looks like the address is stored as a key, but no pointer to the user is stored. I’m not sure what to think! Anyone have any ideas?


Edit:
It looks like there were a few problems identified by the users below though it doesn’t look like that was the cause of my problems.

After fixing the operators, I’ve isolated the issue down to these lines:

assert(this->address == addr);
_userListByAddress.insert(std::pair<Socket::SocketAddress, std::shared_ptr<User>>(addr, std::shared_ptr<User>(this)));
assert(this->address == addr);

The first assertion passes, the second fails.


Edit #2:

Looks like I’ve solved the issue by doing this:

std::shared_ptr<User> user(this);
_usersListBySession.insert(std::pair<uint32_t, std::shared_ptr<User>>(sessionID, user));
assert(this->address == addr); // works
_userListByAddress.insert(std::pair<Socket::SocketAddress, std::shared_ptr<User>>(addr, user));
assert(this->address == addr); // works

I have no idea why. Sounds like a job for another question.

  • 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-06-14T21:28:59+00:00Added an answer on June 14, 2026 at 9:28 pm

    This implementation is not correct:

    bool Socket::SocketAddress::operator<(const SocketAddress &source) const {
        return (fromlen < source.fromlen || memcmp(&from, &source.from, fromlen) < 0);
    }
    

    One possible correct way is:

    bool Socket::SocketAddress::operator<(const SocketAddress &source) const {
        if (fromlen < source.fromlen) return true;
        else if (fromlen > source.fromlen) return false;
        //else: fromlen == source.fromlen
        return (memcmp(&from, &source.from, fromlen) < 0);
    }
    

    Your implementation is not correct because it does not imply strong ordering, you can easily find two SockedAddr objects a, b which are at the same time: a < b and b < a…

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

Sidebar

Related Questions

I have two maps whose keys are String s and whose values are Set<MyObject>
We have a configuration where each property maps to two values in the DB,
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
I have two maps of type Map<Long, Integer>, one named oldValues representing the old
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I have a Perl script which maps two drives, and then proceeds to copy
I have a requirement to create two different maps in C++. The Key is
I would like to have a mapping which maps two string into one string.
I have two maps: Map<String, Object> map1; Map<String, Object> map2; I need to receive
I have a two ibatis sql maps that are linked together via a sub

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.