trojanfoe wrote:All you need to do it make sure you initialise your structures before use, then the padding bytes are in a known state (0), and the equality operator will work without problem. This is much more efficient than field-by-field comparison as the internal memcmp will generally have been implemented in assembler.
The == comparison simply does not work in the C or C++ language for a struct. It will work in C++ if you define a == operator, but that will require the field by field comparison. For instance:
C:\tmp>type t.c
#include <stdio.h>
#include <string.h>
/*
* Concept source: "Calendrical Calculations"
* by Nachum Dershowitz and Edward M. Reingold
*/
typedef struct datetime {
long long njdate;
int year;
int month;
char *localized_month_name;
int day_of_month;
char *localized_weekday_name;
int hours;
int minutes;
int seconds;
int nanoseconds;
} datetime_type;
int main(void)
{
datetime_type d0 =
{733290, 2008, 9, "September", 5, "Friday", 17, 56, 23, 1234567};
datetime_type d1 =
{733290, 2008, 9, "September", 5, "Friday", 17, 56, 23, 1234567};
if (d0 == d1)
puts("same");
else
puts("different");
if (memcmp(&d0, &d1, sizeof (datetime_type)) == 0)
puts("same");
else
puts("different");
return 0;
}
C:\tmp>type t.cpp
#include <stdio.h>
#include <string.h>
/*
* Concept source: "Calendrical Calculations"
* by Nachum Dershowitz and Edward M. Reingold
*/
typedef struct datetime {
long long njdate;
int year;
int month;
char *localized_month_name;
int day_of_month;
char *localized_weekday_name;
int hours;
int minutes;
int seconds;
int nanoseconds;
} datetime_type;
int main(void)
{
datetime_type d0 =
{733290, 2008, 9, "September", 5, "Friday", 17, 56, 23, 1234567};
datetime_type d1 =
{733290, 2008, 9, "September", 5, "Friday", 17, 56, 23, 1234567};
if (d0 == d1)
puts("same");
else
puts("different");
if (memcmp(&d0, &d1, sizeof (datetime_type)) == 0)
puts("same");
else
puts("different");
return 0;
}
C:\tmp>cl t.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
t.cpp
t.cpp(27) : error C2676: binary '==' : 'datetime_type' does not define this operator or a conversion to a type acceptable to the predefined operator
C:\tmp>cl t.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
t.c
t.c(27) : error C2088: '==' : illegal for struct
C:\tmp>
Whereas this version (which is legal C or C++) may output "same" or may output "different" depending upon whether constant folding is in action:
Code: Select all
#include <stdio.h>
#include <string.h>
/*
* Concept source: "Calendrical Calculations"
* by Nachum Dershowitz and Edward M. Reingold
*/
typedef struct datetime {
long long njdate;
int year;
int month;
char *localized_month_name;
int day_of_month;
char *localized_weekday_name;
int hours;
int minutes;
int seconds;
int nanoseconds;
} datetime_type;
int main(void)
{
datetime_type d0 =
{733290, 2008, 9, "September", 5, "Friday", 17, 56, 23, 1234567};
datetime_type d1 =
{733290, 2008, 9, "September", 5, "Friday", 17, 56, 23, 1234567};
if (memcmp(&d0, &d1, sizeof (datetime_type)) == 0)
puts("same");
else
puts("different");
return 0;
}