1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define print_int_expr_and_value(n) printf("%6s == %d\n", (#n), (n))
#define print_address_expr_and_value(address) printf("%6s == %p\n", (#address), (void*)(address))
// swap_wrong_2(…) - WRONG -- detect with assert(…)
void swap_wrong_2(int** a_a_lhs, int** a_a_rhs) {
int* a_lhs = *a_a_lhs; // a_a_lhs is an int** so *a_a_lhs will be an int*
int* a_rhs = *a_a_rhs; //
*a_a_lhs = a_rhs;
*a_a_rhs = a_lhs;
}
int main(int argc, char* argv[]) {
int n1 = 3;
int n2 = 100;
printf("BEFORE:\n");
print_int_expr_and_value(n1);
print_int_expr_and_value(n2);
int* a_n1 = &n1;
int* a_n2 = &n2;
assert(a_n1 == &n1); // should be obvious
assert(*a_n1 == n1); // should be obvious
print_address_expr_and_value(&n1);
print_address_expr_and_value(&n2);
print_address_expr_and_value(a_n1);
print_address_expr_and_value(a_n2);
swap_wrong_2(&a_n1, &a_n2);
assert(a_n1 == &n1); // "I promise that a_n1 == &n1 (or else you can blame me for bugs)."
assert(*a_n1 == n1); // "I promise that *a_n1 == n1 (or else you can blame me for bugs)."
// Thanks to the assert(…), we learn that our variable names a_n1 and a_n2 were DECEPTIVE.
// Because our swap(…) was buggy, a_n1 is actually the adddress of n2 (and likewise for a_n2 and n1)
// Deceptive variable names are EVIL.
printf("\nCalled swap(&a_n1, &a_n2)\n\n");
printf("AFTER:\n");
print_int_expr_and_value(n1);
print_int_expr_and_value(n2);
print_address_expr_and_value(&n1);
print_address_expr_and_value(&n2);
print_address_expr_and_value(a_n1);
print_address_expr_and_value(a_n2);
print_int_expr_and_value(*a_n1);
print_int_expr_and_value(*a_n2);
// DECEPTIVE: a_n1 seems to mean "address of n1" but it now contains &n2 (address of n2)
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.