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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:23:19+00:00 2026-06-01T16:23:19+00:00

Consider the folowing snippet: #define IPV4_MAX_BYTELEN 4 struct gen_entry { struct in_addr addr; struct

  • 0

Consider the folowing snippet:

#define IPV4_MAX_BYTELEN  4
struct gen_entry
{
  struct in_addr addr;
  struct in_addr mask;
  ..
};
unsigned char key[40];

memcpy (key, &fec->addr, IPV4_MAX_BYTELEN);
memcpy (key + IPV4_MAX_BYTELEN, &fec->mask, IPV4_MAX_BYTELEN);
..

What I want is to merge both IP address and mask in a binary key.
Is it ok to merge it this way, provided that array size is enough for
this purpose? (or I’m missing something?)

Thanks !

  • 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-01T16:23:21+00:00Added an answer on June 1, 2026 at 4:23 pm

    This would normally be called “concatenate” rather than “merge”.

    For portability, you could explicitly use the s_addr field of struct in_addr, rather than assuming it’s the first thing. Although maybe that’s guaranteed by Posix, I’m not sure. I think Posix also guarantees that the sizeof an IPv4 address is 4 bytes, so you’re OK there.

    Your code doesn’t clear the other 32 bytes of the array, so it can’t (yet) be used as a key in any useful way. Unless key is defined at file scope, in which case it’s initialized to zeroes.

    Other than those minor niggles, I don’t see anything wrong with what you’re doing.

    As a hypothetical portability issue – even if there are no padding bytes before the s_addr field of struct in_addr, if there are padding bits in the value then you could in theory get false negatives. Suppose you construct two keys from different sources that have the same value but different padding bits – then they should yield the same key but in fact do not. I wouldn’t worry about it, though: any implementation weird enough to have padding bits in integer types is probably too weird to provide the Posix networking API.

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

Sidebar

Related Questions

Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)
Consider the following PHP snippet: <?php class Is { function __get($key) { $class =
Consider the following HTML snippet: <p>Space&nbsp;Test</p> When this HTML is used in a web
Consider the following snippet: struct Foo { static const T value = 123; //Where
Consider the following snippet: struct Base { virtual ~Base() {} virtual void Foo() const
Consider the following snippet (error handling missing on purpose): void* foo(const char *path, off_t
Consider the following code snippet: #include <vector> using namespace std; void sub(vector<int>& vec) {
Consider the following snippet of code: // get number of sheep in DataTable by
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider the following snippet: get '/hello/:name' do |n| Hello #{n}! end How can I

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.