2025-01-10 08:15:31 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// In C we need a length in bytes to check for equality
|
|
|
|
void compareObject(void *a, void *b, size_t len) {
|
2025-01-11 17:54:29 +01:00
|
|
|
if (a == b)
|
|
|
|
std::cout << "Same address" << std::endl;
|
|
|
|
|
|
|
|
// Being explicit about pointer type allows pointer arithmetic
|
|
|
|
unsigned char *i1 = (unsigned char *)a;
|
|
|
|
unsigned char *i2 = (unsigned char *)b;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
if (i1[i] != i2[i]) {
|
|
|
|
std::cout << "Different value" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2025-01-10 08:15:31 +01:00
|
|
|
}
|
|
|
|
|
2025-01-11 17:54:29 +01:00
|
|
|
std::cout << "Same value" << std::endl;
|
2025-01-10 08:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> void cmp(T &a, T &b) {
|
2025-01-11 17:54:29 +01:00
|
|
|
if (&a == &b)
|
|
|
|
std::cout << "Same object" << std::endl;
|
2025-01-10 08:15:31 +01:00
|
|
|
|
2025-01-11 17:54:29 +01:00
|
|
|
if (a == b)
|
|
|
|
std::cout << "Same value" << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << "Different value" << std::endl;
|
2025-01-10 08:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2025-01-11 17:54:29 +01:00
|
|
|
int a = 10, b = 10, c = 12;
|
2025-01-10 08:15:31 +01:00
|
|
|
|
2025-01-11 17:54:29 +01:00
|
|
|
std::cout << "Checking template version" << std::endl;
|
2025-01-10 08:15:31 +01:00
|
|
|
|
2025-01-11 17:54:29 +01:00
|
|
|
cmp(a, b);
|
|
|
|
cmp(a, c);
|
2025-01-10 08:15:31 +01:00
|
|
|
|
2025-01-11 17:54:29 +01:00
|
|
|
std::cout << "Checking C version" << std::endl;
|
2025-01-10 08:15:31 +01:00
|
|
|
|
2025-01-11 17:54:29 +01:00
|
|
|
compareObject(&a, &b, sizeof(int));
|
|
|
|
compareObject(&a, &c, sizeof(int));
|
2025-01-10 08:15:31 +01:00
|
|
|
}
|