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

  • Home
  • SEARCH
  • 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 243657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:56:22+00:00 2026-05-11T20:56:22+00:00

I have the following struct in C++: struct routing_entry { unsigned long destSeq; //

  • 0

I have the following struct in C++:

struct routing_entry {
        unsigned long destSeq;  // 32 bits
        unsigned long nextHop   // 32 bits
        unsigned char hopCount; // 8 bits
}; 

And I have the following function:

routing_entry Cnode_router_aodv::consultTable(unsigned int destinationID ) {    
    routing_entry route;

    if ( routing_table.find(destinationID) != routing_table.end() )
        route = routing_table[destinationID];

    return route; // will be "empty" if not found
}

“routing_table” is a stl::map defined as follows:

map< unsigned long int, routing_entry > routing_table;

My question now is, when using the consultTable function, I want to check that the return value is actually initialized, some how like in Java pseudocode (because I come from the Java camp):

Route consultTable(int id) {
    Route r = table.find(id);
    return r;
}

then checking if r == null

  • 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-11T20:56:23+00:00Added an answer on May 11, 2026 at 8:56 pm

    There are a few problems here. The most urgent may be what happens when the destination ID is not found. Since you have no constructor on the routing_entry and you are not default initialising, it will have undefined values.

    // the data inside route is undefined at this point
    routing_entry route;
    

    One way to handle this is to default initialise. This works by instructing the compiler to fill the structure with zeros. This is kind of a trick borrowed from C but it works well here.

    routing_entry route={0};
    

    You mention you are coming from Java, unlike in Java, structure and class members are not 0 initialised, so you should really handle that somehow. Another way is to define a constructor:

    struct routing_entry
    {
      routing_entry()
      : destSeq(0)
      , nextHop(0)
      , hopCount(0)
      { }
    
                unsigned long destSeq;  // 32 bits
                unsigned long nextHop;   // 32 bits
                unsigned char hopCount; // 8 bits
    };
    

    Also note that in C++, the size of the integer and char members is not defined in bits. The char type is 1 byte (but a byte is a not defined, but usually 8 bits). The longs are usually 4 bytes these days but can be some other value.

    Moving on to your consultTable, with the initialisation fixed:

    routing_entry Cnode_router_aodv::consultTable(unsigned int destinationID )
    {    
      routing_entry route={0};
    
      if ( routing_table.find(destinationID) != routing_table.end() )
            route = routing_table[destinationID];
    
      return route; // will be "empty" if not found
    }
    

    One way to tell might be to check if the structure is still zeroed out. I prefer to refactor to have the function return bool to indicate success. Additionally, I always typedef STL structures for simplicity, so I’ll do that here:

    typedef map< unsigned long int, routing_entry > RoutingTable;
    RoutingTable routing_table;
    

    Then we pass in a reference to the routing entry to populate. This can be more efficient for the compiler, but probably that is irrelevant here – anyway this is just one method.

    bool Cnode_router_aodv::consultTable(unsigned int destinationID, routing_entry &entry)
    {
      RoutingTable::const_iterator iter=routing_table.find(destinationID);
      if (iter==routing_table.end())
        return false;
      entry=iter->second;
      return true;
    }
    

    You would call it like this:

    routing_entry entry={0};
    if (consultTable(id, entry))
    {
      // do something with entry
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a peek at Geokit, it is a Ruby Gem… May 12, 2026 at 8:02 pm
  • Editorial Team
    Editorial Team added an answer Cross database queries are somewhat slower that within the same… May 12, 2026 at 8:02 pm
  • Editorial Team
    Editorial Team added an answer Try closing the input tag before the script tag. EDIT:… May 12, 2026 at 8:02 pm

Related Questions

I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have made a dll that compiles fine in 32bit mode, but when compiling
I have the following use case, a struct with some boolean and int variables
I have yet to find a good reference on this topic. For this example

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.