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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:49:12+00:00 2026-05-23T11:49:12+00:00

This example : #include <iostream> #include <cstring> struct A { int a; bool b;

  • 0

This example :

#include <iostream>
#include <cstring>

struct A
{
    int  a;
    bool b;
};

bool foo( const A a1, const A a2 )
{
    return ( 0 == std::memcmp( &a1, &a2, sizeof( A ) ) );
}

int main()
{
    A a1 = A();
    a1.a = 5;a1.b = true;
    A a2 = A();
    a2.a = 5;a2.b = true;

    std::cout<<std::boolalpha << foo( a1, a2 ) << std::endl;
}

is going to produce false, because of padding.

I do not have access to the foo function, and I can not change the way the comparison is done.

Assuming a bool occupies 1 byte (that is true on my system), if I change the struct A to this :

struct A
{
  int a;
  bool b;
  char dummy[3];
};

then it works fine on my system (the output is true).

Is there anything else I could do to fix the above problem (get the true output)?

  • 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-23T11:49:12+00:00Added an answer on May 23, 2026 at 11:49 am

    The first one is not working because of padding in the struct. The padding is having different bit patterns for both objects.

    If you use memset to set all the bits in the object before using it, then it will work:

    A a1;
    std::memset(&a1, 0, sizeof(A));
    a1.a = 5;a1.b = true;
    
    A a2;
    std::memset(&a2, 0, sizeof(A));
    a2.a = 5;a2.b = true;
    

    Online demos:

    • http://www.ideone.com/mVmsn (Original code written by you)
    • http://www.ideone.com/Q13QO (My modification)

    By the way, you can write operator<, operator== etc, for PODs also.

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

Sidebar

Related Questions

//C++ Example #include <iostream> using namespace std; int doHello (std::string&); int main() { std::string
Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
Consider next example : #include <iostream> template< int a > void foo(); int main(int
Let me explain with an example - #include <iostream> void foo( int a[2], int
Please take a look at this example: #include <iostream> #include <vector> #include <string> using
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
Looking on the internet for C++ brainteasers, I found this example: #include <iostream> using

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.