-
-
Save PappasBrent/d573d1ae739b1dd3ae6f669629de7ddd to your computer and use it in GitHub Desktop.
Examples of Macros that Fall under PRE00-C-EX5 and not PRE00-C-EX4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The macro ADDRESS_OF reads the L-value of its argument x. | |
// If ADDRESS_OF were turned into a function, the L-value of | |
// its argument would change. | |
// Since it is a macro, this does not happen. | |
// Therefore, call-by-name semantics are required for ADDRESS_OF | |
// to function properly. | |
// This means ADDRESS_OF falls under EX5. | |
#define ADDRESS_OF(x) (&(x)) | |
void foo() | |
{ | |
// ADDRESS_OF is only ever invoked with an argument of type int. | |
// Therefore, ADDRESS_OF is not used to implement a generic function. | |
// This means ADDRESS_OF does not fall under EX4. | |
int x = 0; | |
int y = 0; | |
int z = 0; | |
int *a = ADDRESS_OF(x); | |
int *b = ADDRESS_OF(y); | |
int *c = ADDRESS_OF(z); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct node_t | |
{ | |
struct node_t *next; | |
}; | |
// In this example, the macro CONNECT assigns the next member of | |
// the argument u to the address of the argument v | |
// CONNECT is not used polymorphically, so it does not fall under EX4. | |
// CONNECT only works correctly under call-by-name semantics, so it falls under EX5. | |
#define CONNECT(u, v) (((u)->next) = (&(v))) | |
void bar() | |
{ | |
struct node_t x, y, z; | |
CONNECT(&x, y); | |
CONNECT(&y, z); | |
z.next = NULL; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#define CAP 32 | |
struct person_t | |
{ | |
char name[CAP]; | |
}; | |
// Assigns the name field of a person_t pointer to the given name | |
void person_t_set_name(struct person_t *p, char *name) | |
{ | |
strncpy(p->name, name, CAP); | |
} | |
// SET_NAME is defined as an alias for person_t_set_name | |
// It is only ever invoked with the same types, so it does not fall under EX4. | |
// It does not behave the same under call-by-value semantics, so it falls under | |
// EX5. | |
#define SET_NAME(p, name) (person_t_set_name((&(p)), name)) | |
void baz() | |
{ | |
struct person_t p, q, r; | |
char name[CAP]; | |
strncpy(name, "Alpha", CAP); | |
SET_NAME(p, name); | |
strncpy(name, "Beta", CAP); | |
SET_NAME(q, name); | |
strncpy(name, "Gamma", CAP); | |
SET_NAME(r, name); | |
} |
Wrap definition of CONNECT
in example 2 in parentheses
Add example 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Formatting on example 1.
Add example 2.