Is there a better way to compare two C structs? - c

Let's assume that I declared a C struct for holding configuration information, which has lots of fields(~30), e.g.:
struct config {
int a;
int b;
int c;
...
char *str1;
char *str2;
...
};
What is the best way to compare two structs without simply enumerating and comparing each element?
Of course I could use the following code but my question is, is there an easier way to achieve this?
static int
cmpInstances(struct config *l, struct config *r) {
return (
l->a == r->a &&
l->b == r->b &&
l->c == r->c &&
!strcmp(l->str1, r->str1) &&
!strcmp(l->str2, r->str2)
);
}
Is there some macro that could do that? For example:
cmpInstancesViaMacro(a,b,c) would expand into
l->a == r->a &&
l->b == r->b &&
l->c == r->c

A generalized variant of so-called X Macros could be used.
In the example of the question, it would look like this:
#define CONFIG_FIELDS \
X_INT(a) \
X_INT(b) \
X_INT(c) \
X_STRING(str1) \
X_STRING(str2)
struct config {
#define X_INT(NAME) int NAME;
#define X_STRING(NAME) char *NAME;
CONFIG_FIELDS
#undef X_INT
#undef X_STRING
};
static int cmpInstances(struct config *l, struct config *r) {
#define X_INT(NAME) l->NAME == r->NAME &&
#define X_STRING(NAME) strcmp(l->NAME, r->NAME) == 0 &&
return CONFIG_FIELDS 1;
#undef X_INT
#undef X_STRING
}
With this solution, only the CONFIG_FIELDS macro needs to be changed when the structure is changed as long as there is no change to the set of types used. The structure and function would automatically be kept synchronized.
If a new type is introduced, the corresponding X Macros must be defined similar to X_INT and X_STRING above.

Perhaps some macros will help us reduce the lines of code and effort needed:
#define CMP(name) l->name == r->name &&
#define SCMP(name) !strcmp(l->name, r->name) &&
struct config
{
int a;
int b;
int c;
char *str1;
char *str2;
};
static int cmpInstances(struct config *l, struct config *r)
{
return CMP(a) CMP(b) CMP(c) SCMP(str1) SCMP(str2) 1;
}
#undef CMP
#undef SCMP
Here we define some macros that take only the name of the variable and compare them. One is written for integer comparison (CMP) and the other is written for string comparison (SCMP). Then you can use these macros to compare your data and undef them when you are done.
Thanks to chqrlie for adding 1 part.

What is the best way to compare two structs without simply enumerating and comparing each element?
The presented way is the best. Except, the pointers should point to const - nothing is getting modified.
is there an easier way to achieve this?
The presented way is the easiest way to achieve it, it's also the most readable and self-explanatory, easy to refactor and reason about. Any other will add to complexity and result in maintenance burden.
I find strcmp(...) == 0 clearer then !strcmp(...).

If you're the author of the struct, you can lay it out so there's no padding, _Static_assert that there's no padding (sizeof(whole+struct) == sum of component sizes) and then simply use one memcmp call if all the data is in the struct.
If the struct has string pointers (and you can't guarantee that a string with particular contents is stored only in one place), you'll need to do those separately.
You'll get the tightest code if you put all of them next to each other, union-them with a corresponding char*[] array, and then loop over the corresponding arrays calling strcmp on each pair and combine the result (if all were equal) with memcpy for the rest.
Something like:
#include <string.h>
struct config {
int m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16;
union {
struct { char *s1,*s2,*s3,*s4,*s5,*s6,*s7,*s8; };
char *strings[8];
};
};
_Static_assert(sizeof(struct config) == sizeof(int)*16+sizeof(char*)*8,"");
int strings_equal(char *const A[], char *const B[], size_t Count){
for(;Count;Count--) if(0!=strcmp(*A++,*B++)) return 0;
return 1;
}
int configs_equal(struct config const *A, struct config const *B){
return 0==memcmp(A,B,sizeof(int)*16)
&& strings_equal(A->strings,B->strings,sizeof(A->strings)/sizeof(A->strings[0]));
}

Related

use c Token-Pasting to access field in struct

I'm trying to use c token pasting (##) to access to struct field as below:
typedef struct
{
int a;
int b;
} TMP_T;
#define BUILD_FIELD(field) my_struct.##field
int main()
{
TMP_T my_struct;
BUILD_FIELD(a) = 5;
return 0;
}
But got the following error during compilation:
error: pasting "." and "a" does not give a valid preprocessing token
I would like to add additional case to that:
typedef struct {
int a;
int b;
}TMP_T;
#define BUILD_FIELD(my_struct,field) my_struct.##field
void func(char* name)
{
TMP_T tmp_str;
if((name == "a") || (name == "b"))
{
BUILD_FIELD(tmp_str, name) = 7;
printf("%d \n", BUILD_FIELD(a) );
}
}
int main()
{
func("a");
return 1;
}
How should I use the macro to access the specific struct and field. Is it possible? or because its a pre compiled then it couldn't be defined for various fields (a,b)
Thanks
Moti
You don't need token pasting at all:
#define BUILD_FIELD(field) my_struct.field
According to the gcc manual token pasting should result in an identifier or a number after the concatenation. The error is due to the .a not being either.
All that is required is to use the macro replacement string field. Try this:
#define BUILD_FIELD(field) my_struct.field.
So the following code will work:
#include <ansi_c.h>
typedef struct {
int a;
int b;
}TMP_T;
#define BUILD_FIELD(field) my_struct.field
int main(void)
{
TMP_T my_struct;
BUILD_FIELD(a) = 5;
printf("my_struct.a is %d\n",my_struct.a);
BUILD_FIELD(b) = 10;
printf("my_struct.b is %d\n",my_struct.b);
return 0;
}
By the way, I also ran the above code with the line: (after having read Krister Andersson's link)
#define BUILD_FIELD(field) my_struct.##field
It ran identical to the code without using ## stringification. So, there is obviously something different between our compilers.
In any case, I hope my original assertions did not add too much confusion. Your question taught me a few lessons today. Thanks! (+1)
Addressing your latest post edit:
First of all this modification will result in a compile error for an unknown field name within TMP_T here:
BUILD_FIELD(tmp_str, name) = 7;
Here is a good link that discusses Preprocessing, Compiling and Linking (of greatest interest for this discussion will be the preprocessing and compile parts). Among other things, it discusses when to use macros.
Also, regarding the line:
if((name == "a") || (name == "b"))
When doing string compares, the == is not used. strcmp(), or even strstr() are used for string comparisons.
You would need pasting if you create your struct with some "structure". For example:
typedef struct
{
int tmp_a;
int tmp_b;
} TMP_T;
#define BUILD_FIELD(field) my_struct.tmp_##field
It's used for tokenization. Your use case is the "typical" macro variable replacement.

Accessing structure members without using dot operator

I don't even know, whether what I'm asking is something stupid or not. I am not asking you to write any code for me, but an idea to do something in a better way.
I have a struct with a large number of items like this:
typedef struct _myStruct
{
int int1;
char char1;
int int2;
:
:
int int50;
}myStruct;
I have another enumeration which has a single entry for each item in myStruct.
enum
{
eINT1,
eCHAR1,
eINT2,
:
:
eINT50
} PARAMETER_ID;
I want to write a function for each data type [say one for int, one for char, one for string etc], which return the value of a member of myStruct, when the PARAMETER_ID is given as input.
For example I need a int GetInt(PARAMETER_ID) function which return the value of int1 when eINT1 is passed as an argument. Similarly I am going to have char GetCharacter(PARAMETER_ID), float GetFloat(PARAMETER_ID) etc.
The number of items in the struct can be large. So using a switch-case for each item will not be a viable option.
Only other option I can think of is using the address of the structure variable and offsetof() function to calculate the address of the parameter and then by memcpying the required bytes into a variable. In that case I need to keep the offset of each parameter somewhere, but that is not a problem.
I am looking for alternate options to do this. Any help will be greatly appreciated.
Thank you.
A large switch is a good viable option.
You might also play preprocessor tricks.
You could have a mystruct.def file containing
INTFIELD(int1)
CHARFIELD(char1)
INTFIELD(int2)
etc... Then you would include it several times; to declare the structure:
struct _myStruct {
#define INTFIELD(F) int F;
#define CHARFIELD(F) char F;
#include "mystruct.def"
#undef INTFIELD
#undef CHARFIELD
};
To declare the enumeration (using e_int1 instead of eINT1)
enum field_en {
#define INTFIELD(F) e_##F,
#define CHARFIELD(F) e_##F,
#include "mystruct.def"
#undef INTFIELD
#undef CHARFIELD
};
To implement the accessor,
int get_int(struct _myStruct*s, enum field_en f)
{
switch (f) {
#define INTFIELD(F) case e_##F: return s->F;
#define CHARFIELD(F) /*nothing*/
#include "mystruct.def"
#undef INTFIELD
#undef CHARFIELD
default: return 0;
}}
I don't claim this is better or more readable code, but that kind of programming style does appear in some C or C++ programs (e.g. GCC internals with its gcc/tree.def)
If you code is a very large code base, and you are ready to spend days of work (e.g. because you have a lot of such struct and don't want to play such tricks) you might consider making a GCC extension with MELT (a high-level domain specific language to extend GCC) to help you; you probably can make a MELT extension to generate the accessor functions for you.
You could also convince your boss to generate both the struct, the enum and the accessor functions from an ad-hoc descriptive file (using awk, python or whatever). GCC does such tricks for its options file, e.g. gcc/common.opt
At last, if the header containing the _myStruct is so sacred that you are not allowed to touch it, and if it is very cleanly formatted, you might make an ad-hoc (e.g. awk) script to get that declaration and process it.
NB a good compiler optimizes dense switch statements as indexed jumps which take constant time, even for hundred of cases.
#include <stddef.h>
#include <stdio.h>
struct S
{
int int1;
char char1;
int int2;
char char2;
long long1;
} myStruct = {12345, 'A', 321, 'B', -1L};
enum
{
eINT1 = offsetof(struct S, int1),
eCHAR1 = offsetof(struct S, char1),
eINT2 = offsetof(struct S, int2),
eCHAR2 = offsetof(struct S, char2),
eLONG1 = offsetof(struct S, long1),
} PARAMETER_ID;
char GetChar(int para_id)
{
return *((char*)((char *)&myStruct + para_id));
}
int GetInt(int para_id)
{
return *((int*)((char *)&myStruct + para_id));
}
long GetLong(int para_id)
{
return *((long*)((char *)&myStruct + para_id));
}
void main(void)
{
printf("offsetof int1 = %d\n", eINT1);
printf("offsetof char1 = %d\n", eCHAR1);
printf("offsetof int2 = %d\n", eINT2);
printf("offsetof char2 = %d\n", eCHAR2);
printf("offsetof long1 = %d\n", eLONG1);
printf("int1 = %d\n", GetInt (eINT1));
printf("char1 = %c\n", GetChar(eCHAR1));
printf("int2 = %d\n", GetInt (eINT2));
printf("char2 = %c\n", GetChar(eCHAR2));
printf("long1 = %ld\n", GetLong(eLONG1));
}
You partially answer your own question, offsetof is meant to be used for this very purpose. You have to consider struct padding/alignment. I think you are looking for something similar to this:
#include <stddef.h> // size_t, offsetof
#include <string.h> // memcpy
#include <stdio.h>
typedef struct
{
int int1;
char char1;
int int2;
int int50;
} myStruct;
typedef enum
{
eINT1,
eCHAR1,
eINT2,
eINT50,
ITEMS_IN_STRUCT
} myEnum;
static const size_t MYSTRUCT_MEMBER_OFFSET [ITEMS_IN_STRUCT] =
{
offsetof(myStruct, int1),
offsetof(myStruct, char1),
offsetof(myStruct, int2),
offsetof(myStruct, int50),
};
static const myStruct MS;
static const size_t MYSTRUCT_MEMBER_SIZE [ITEMS_IN_STRUCT] =
{
sizeof(MS.int1),
sizeof(MS.char1),
sizeof(MS.int2),
sizeof(MS.int50)
};
void myStruct_get_member (void* result, const myStruct* ms, myEnum id)
{
memcpy (result,
(char*)ms + MYSTRUCT_MEMBER_OFFSET[id],
MYSTRUCT_MEMBER_SIZE[id]);
}

Is it possible to check if a variable is of type struct?

Suppose I have the following structs:
struct A{
int a;
} a;
struct B{
A a;
int b;
} b;
how to check if b is of type B or if b is of the type A?
Do you mean at runtime, given a void * that points to one or the other? Unfortunately, this is not possible; C doesn't have any kind of runtime type information mechanism.
At compile time:
#define WARN_IF_DIFFERENT_STRUCT_TYPE_1(T, o) do { \
T temp = (o); \
(void) temp; \
} while (0)
example:
struct B b;
/* will warn if b is not of struct A type */
WARN_IF_DIFFERENT_STRUCT_TYPE_1(struct A, b);
With two objects passed as macro parameters using typeof GNU extension:
#define WARN_IF_DIFFERENT_STRUCT_TYPE_2(o1, o2) do { \
typeof(o1) temp = (o2); \
(void) temp; \
} while (0)
example:
struct A a;
struct B b;
/* will warn if a and b are not the same struct type */
WARN_IF_DIFFERENT_STRUCT_TYPE_2(a, b);  
No as far as C is concerned, data is just a string of bits. How you use and interpret the data is up to the programmer.
It's just like how a char can represent a character, a positive number or a negative number (or anything else for that matter). It depends on the context and how it's used by the programmer.
No. Unlike C++, which uses a vtable to maintain type associations at runtime, C provides no method for runtime type-checking.
A possible workaround would be to assign a type identifer as the first element of the struct:
struct A {
int type;
int a;
} a; a.type = 'A';
struct B {
int type;
A a;
int b;
} b; b.type = 'B';
// Now, given a (void*) pointer `ptr`, of either `A` or `B`...
if ( *(int*)ptr == 'A')
{
// ...
}
elseif ( *(int*)ptr == 'B')
{
// ...
}
If you are going to write your own type checking mechanism I recommend writing macros to fill in the type field for you.
typedef struct A {
void *type;
int a;
} A;
This macro initializes each structure with it's type information using stringification:
#define InitializeType(ptr, type) \
((ptr)->type == (void *)(#type ## __FILE__ ##))
#define IsType(ptr, type) \
((ptr)!=NULL && (ptr)->type != (void *)(#type ## __FILE__ ##))
It fills in the type field with a string containing the name of the struct and the file it is located in. You can have two structs with the same name across multiple source files, however you can't have two structs with the same name in the same source file, thus the reason for including the name of the source file that the type was initialized in. You would then use them like so:
A alpha;
InitializeType(&alpha, A);
IsType(&alpha, A);
There are a couple of cavets though; you have to use string pooling compiler flag, you have to encapsulate your structs in "classes" so that type checking and initializing is localized to the source file that contains the private struct, and you have to have void *type as the first parameter in each struct.
You could hack it? If really neccessary and you only have a couple structs.
struct A{
int a;
int identifer = 0;
} a;
struct B{
A a;
int b;
int identifer = 1;
} b;
And if your code you can be like
if object->identifer == 0
bla bla bla

Accessing a general structure member using a variable

Not entirely sure my question title describes what I want to do, but couldn't think how better to word it!! I'm using C, and perhaps the pseudocode below will describe what I'm trying to do:
typedef struct obj
{
char *str1;
char *str2;
char *str3;
} object;
/* global variable */
object *glob;
void black_box_function(local, member) ????
{
/* Do something with glob->member and local->member */
}
void main()
{
object *ob1, *ob2;
/* Initialise glob, ob1 and ob2 somewhere */
black_box_function(ob1, str1);
black_box_function(ob2, str3);
}
Hopefully, you can see what I'm trying to do. I have a "black-box" function that will do something with a particular member, and I need to be able to tell the black-box function which member to use.
I don't want to just pass the member directly to the function, like in this code, as that won't fit into the rest of my code easily.
black_box_function(ob1->member, glob->member)
You could do the following magic (with GCC extensions):
#define black_box(local, member) black_box_function((local), __builtin_offsetof(object, member))
void black_box_function(object *local, int offset)
{
char *lmember = ((void *)local) + offset;
char *gmember = ((void *)global) + offset;
/* do stuff */
}
However, you must know in advance the type of your members. Keep in mind that C is not a dynamically typed language, so you have no runtime introspection at all.
EDIT: You can implement offsetof() functionality without resorting to GCC extensions, like this:
#define offsetof(type, field) ((int) (unsigned long) &((type *) 0)->field)
Perhaps you could create accessor functions for your struct and pass those accessors as function pointer arguments instead of passing the members directly
typedef struct
{
int a;
int b;
} foo;
typedef int* (*accessor)(foo*);
int* get_a(foo* f) { return &f->a; }
int* get_b(foo* f) { return &f->b; }
void black_box_function(foo* object, accessor fn)
{
int* p = fn(object);
}
int main(void)
{
foo bar1;
foo bar2;
black_box_function(&bar1, get_a);
black_box_function(&bar2, get_b);
return 0;
}
Since all are char*, you can redefine the struct like:
typedef struct obj
{
char **str; // Array of c
} object;
Then you can send the index of str from main which you want work with:
black_box_function(obj1, index)
So you can it like obj1->str[i] in your blackbox.
Btw, black-box_function will not compile.
On a side note: A little more info/code on your blackbox function and compilable code would give a better picture of what you are trying to do.

What is your favorite C programming trick? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
For example, I recently came across this in the linux kernel:
/* Force a compilation error if condition is true */
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
So, in your code, if you have some structure which must be, say a multiple of 8 bytes in size, maybe because of some hardware constraints, you can do:
BUILD_BUG_ON((sizeof(struct mystruct) % 8) != 0);
and it won't compile unless the size of struct mystruct is a multiple of 8, and if it is a multiple of 8, no runtime code is generated at all.
Another trick I know is from the book "Graphics Gems" which allows a single header file to both declare and initialize variables in one module while in other modules using that module, merely declare them as externs.
#ifdef DEFINE_MYHEADER_GLOBALS
#define GLOBAL
#define INIT(x, y) (x) = (y)
#else
#define GLOBAL extern
#define INIT(x, y)
#endif
GLOBAL int INIT(x, 0);
GLOBAL int somefunc(int a, int b);
With that, the code which defines x and somefunc does:
#define DEFINE_MYHEADER_GLOBALS
#include "the_above_header_file.h"
while code that's merely using x and somefunc() does:
#include "the_above_header_file.h"
So you get one header file that declares both instances of globals and function prototypes where they are needed, and the corresponding extern declarations.
So, what are your favorite C programming tricks along those lines?
C99 offers some really cool stuff using anonymous arrays:
Removing pointless variables
{
int yes=1;
setsockopt(yourSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
}
becomes
setsockopt(yourSocket, SOL_SOCKET, SO_REUSEADDR, (int[]){1}, sizeof(int));
Passing a Variable Amount of Arguments
void func(type* values) {
while(*values) {
x = *values++;
/* do whatever with x */
}
}
func((type[]){val1,val2,val3,val4,0});
Static linked lists
int main() {
struct llist { int a; struct llist* next;};
#define cons(x,y) (struct llist[]){{x,y}}
struct llist *list=cons(1, cons(2, cons(3, cons(4, NULL))));
struct llist *p = list;
while(p != 0) {
printf("%d\n", p->a);
p = p->next;
}
}
Any I'm sure many other cool techniques I haven't thought of.
While reading Quake 2 source code I came up with something like this:
double normals[][] = {
#include "normals.txt"
};
(more or less, I don't have the code handy to check it now).
Since then, a new world of creative use of the preprocessor opened in front of my eyes. I no longer include just headers, but entire chunks of code now and then (it improves reusability a lot) :-p
Thanks John Carmack! xD
I'm fond of using = {0}; to initialize structures without needing to call memset.
struct something X = {0};
This will initialize all of the members of the struct (or array) to zero (but not any padding bytes - use memset if you need to zero those as well).
But you should be aware there are some issues with this for large, dynamically allocated structures.
If we are talking about c tricks my favourite has to be Duff's Device for loop unrolling! I'm just waiting for the right opportunity to come along for me to actually use it in anger...
using __FILE__ and __LINE__ for debugging
#define WHERE fprintf(stderr,"[LOG]%s:%d\n",__FILE__,__LINE__);
In C99
typedef struct{
int value;
int otherValue;
} s;
s test = {.value = 15, .otherValue = 16};
/* or */
int a[100] = {1,2,[50]=3,4,5,[23]=6,7};
Once a mate of mine and I redefined return to find a tricky stack corruption bug.
Something like:
#define return DoSomeStackCheckStuff, return
I like the "struct hack" for having a dynamically sized object. This site explains it pretty well too (though they refer to the C99 version where you can write "str[]" as the last member of a struct). you could make a string "object" like this:
struct X {
int len;
char str[1];
};
int n = strlen("hello world");
struct X *string = malloc(sizeof(struct X) + n);
strcpy(string->str, "hello world");
string->len = n;
here, we've allocated a structure of type X on the heap that is the size of an int (for len), plus the length of "hello world", plus 1 (since str1 is included in the sizeof(X).
It is generally useful when you want to have a "header" right before some variable length data in the same block.
Object oriented code with C, by emulating classes.
Simply create a struct and a set of functions that take a pointer to that struct as a first parameter.
Instead of
printf("counter=%d\n",counter);
Use
#define print_dec(var) printf("%s=%d\n",#var,var);
print_dec(counter);
Using a stupid macro trick to make record definitions easier to maintain.
#define COLUMNS(S,E) [(E) - (S) + 1]
typedef struct
{
char studentNumber COLUMNS( 1, 9);
char firstName COLUMNS(10, 30);
char lastName COLUMNS(31, 51);
} StudentRecord;
For creating a variable which is read-only in all modules except the one it's declared in:
// Header1.h:
#ifndef SOURCE1_C
extern const int MyVar;
#endif
// Source1.c:
#define SOURCE1_C
#include Header1.h // MyVar isn't seen in the header
int MyVar; // Declared in this file, and is writeable
// Source2.c
#include Header1.h // MyVar is seen as a constant, declared elsewhere
Bit-shifts are only defined up to a shift-amount of 31 (on a 32 bit integer)..
What do you do if you want to have a computed shift that need to work with higher shift-values as well? Here is how the Theora vide-codec does it:
unsigned int shiftmystuff (unsigned int a, unsigned int v)
{
return (a>>(v>>1))>>((v+1)>>1);
}
Or much more readable:
unsigned int shiftmystuff (unsigned int a, unsigned int v)
{
unsigned int halfshift = v>>1;
unsigned int otherhalf = (v+1)>>1;
return (a >> halfshift) >> otherhalf;
}
Performing the task the way shown above is a good deal faster than using a branch like this:
unsigned int shiftmystuff (unsigned int a, unsigned int v)
{
if (v<=31)
return a>>v;
else
return 0;
}
Declaring array's of pointer to functions for implementing finite state machines.
int (* fsm[])(void) = { ... }
The most pleasing advantage is that it is simple to force each stimulus/state to check all code paths.
In an embedded system, I'll often map an ISR to point to such a table and revector it as needed (outside the ISR).
Another nice pre-processor "trick" is to use the "#" character to print debugging expressions. For example:
#define MY_ASSERT(cond) \
do { \
if( !(cond) ) { \
printf("MY_ASSERT(%s) failed\n", #cond); \
exit(-1); \
} \
} while( 0 )
edit: the code below only works on C++. Thanks to smcameron and Evan Teran.
Yes, the compile time assert is always great. It can also be written as:
#define COMPILE_ASSERT(cond)\
typedef char __compile_time_assert[ (cond) ? 0 : -1]
I wouldn't really call it a favorite trick, since I've never used it, but the mention of Duff's Device reminded me of this article about implementing Coroutines in C. It always gives me a chuckle, but I'm sure it could be useful some time.
#if TESTMODE == 1
debug=1;
while(0); // Get attention
#endif
The while(0); has no effect on the program, but the compiler will issue a warning about "this does nothing", which is enough to get me to go look at the offending line and then see the real reason I wanted to call attention to it.
I'm a fan of xor hacks:
Swap 2 pointers without third temp pointer:
int * a;
int * b;
a ^= b;
b ^= a;
a ^= b;
Or I really like the xor linked list with only one pointer. (http://en.wikipedia.org/wiki/XOR_linked_list)
Each node in the linked list is the Xor of the previous node and the next node. To traverse forward, the address of the nodes are found in the following manner :
LLNode * first = head;
LLNode * second = first.linked_nodes;
LLNode * third = second.linked_nodes ^ first;
LLNode * fourth = third.linked_nodes ^ second;
etc.
or to traverse backwards:
LLNode * last = tail;
LLNode * second_to_last = last.linked_nodes;
LLNode * third_to_last = second_to_last.linked_nodes ^ last;
LLNode * fourth_to_last = third_to_last.linked_nodes ^ second_to_last;
etc.
While not terribly useful (you can't start traversing from an arbitrary node) I find it to be very cool.
This one comes from the book 'Enough rope to shoot yourself in the foot':
In the header declare
#ifndef RELEASE
# define D(x) do { x; } while (0)
#else
# define D(x)
#endif
In your code place testing statements eg:
D(printf("Test statement\n"));
The do/while helps in case the contents of the macro expand to multiple statements.
The statement will only be printed if '-D RELEASE' flag for compiler is not used.
You can then eg. pass the flag to your makefile etc.
Not sure how this works in windows but in *nix it works well
Rusty actually produced a whole set of build conditionals in ccan, check out the build assert module:
#include <stddef.h>
#include <ccan/build_assert/build_assert.h>
struct foo {
char string[5];
int x;
};
char *foo_string(struct foo *foo)
{
// This trick requires that the string be first in the structure
BUILD_ASSERT(offsetof(struct foo, string) == 0);
return (char *)foo;
}
There are lots of other helpful macros in the actual header, which are easy to drop into place.
I try, with all of my might to resist the pull of the dark side (and preprocessor abuse) by sticking mostly to inline functions, but I do enjoy clever, useful macros like the ones you described.
Two good source books for this sort of stuff are The Practice of Programming and Writing Solid Code. One of them (I don't remember which) says: Prefer enum to #define where you can, because enum gets checked by the compiler.
Not specific to C, but I've always liked the XOR operator. One cool thing it can do is "swap without a temp value":
int a = 1;
int b = 2;
printf("a = %d, b = %d\n", a, b);
a ^= b;
b ^= a;
a ^= b;
printf("a = %d, b = %d\n", a, b);
Result:
a = 1, b = 2
a = 2, b = 1
See "Hidden features of C" question.
I like the concept of container_of used for example in lists. Basically, you do not need to specify next and last fields for each structure which will be in the list. Instead, you append the list structure header to actual linked items.
Have a look on include/linux/list.h for real-life examples.
I think the use of userdata pointers is pretty neat. A fashion losing ground nowdays. It's not so much a C feature but is pretty easy to use in C.
I use X-Macros to to let the pre-compiler generate code. They are especially useful for defining error values and associated error strings in one place, but they can go far beyond that.
Our codebase has a trick similar to
#ifdef DEBUG
#define my_malloc(amt) my_malloc_debug(amt, __FILE__, __LINE__)
void * my_malloc_debug(int amt, char* file, int line)
#else
void * my_malloc(int amt)
#endif
{
//remember file and line no. for this malloc in debug mode
}
which allows for the tracking of memory leaks in debug mode. I always thought this was cool.
Fun with macros:
#define SOME_ENUMS(F) \
F(ZERO, zero) \
F(ONE, one) \
F(TWO, two)
/* Now define the constant values. See how succinct this is. */
enum Constants {
#define DEFINE_ENUM(A, B) A,
SOME_ENUMS(DEFINE_ENUMS)
#undef DEFINE_ENUM
};
/* Now a function to return the name of an enum: */
const char *ToString(int c) {
switch (c) {
default: return NULL; /* Or whatever. */
#define CASE_MACRO(A, B) case A: return #b;
SOME_ENUMS(CASE_MACRO)
#undef CASE_MACRO
}
}
Here is an example how to make C code completly unaware about what is actually used of HW for running the app. The main.c does the setup and then the free layer can be implemented on any compiler/arch. I think it is quite neat for abstracting C code a bit, so it does not get to be to spesific.
Adding a complete compilable example here.
/* free.h */
#ifndef _FREE_H_
#define _FREE_H_
#include <stdio.h>
#include <string.h>
typedef unsigned char ubyte;
typedef void (*F_ParameterlessFunction)() ;
typedef void (*F_CommandFunction)(ubyte byte) ;
void F_SetupLowerLayer (
F_ParameterlessFunction initRequest,
F_CommandFunction sending_command,
F_CommandFunction *receiving_command);
#endif
/* free.c */
static F_ParameterlessFunction Init_Lower_Layer = NULL;
static F_CommandFunction Send_Command = NULL;
static ubyte init = 0;
void recieve_value(ubyte my_input)
{
if(init == 0)
{
Init_Lower_Layer();
init = 1;
}
printf("Receiving 0x%02x\n",my_input);
Send_Command(++my_input);
}
void F_SetupLowerLayer (
F_ParameterlessFunction initRequest,
F_CommandFunction sending_command,
F_CommandFunction *receiving_command)
{
Init_Lower_Layer = initRequest;
Send_Command = sending_command;
*receiving_command = &recieve_value;
}
/* main.c */
int my_hw_do_init()
{
printf("Doing HW init\n");
return 0;
}
int my_hw_do_sending(ubyte send_this)
{
printf("doing HW sending 0x%02x\n",send_this);
return 0;
}
F_CommandFunction my_hw_send_to_read = NULL;
int main (void)
{
ubyte rx = 0x40;
F_SetupLowerLayer(my_hw_do_init,my_hw_do_sending,&my_hw_send_to_read);
my_hw_send_to_read(rx);
getchar();
return 0;
}
if(---------)
printf("hello");
else
printf("hi");
Fill in the blanks so that neither hello nor hi would appear in output.
ans: fclose(stdout)

Resources