Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from n1be/master

Some improvements to comments in adv_c code
  • Loading branch information...
commit d1fb9b1d580eb77b5f740c59a918d03258630cf5 2 parents 807205b + f23368d
@dgrnbrg authored
View
2  1-typedefs.c
@@ -72,7 +72,7 @@ part2(void)
mile b = a;
#if 0
- Meter_t c = { 19 };
+ Meter_t c = { 19 }; # This code protects against mixing up units ...
Miles_t d = c; //invalid!
#endif
}
View
1  2-special_types.c
@@ -11,6 +11,7 @@ struct Foo {
int c : 16;
};
//NOTE: bitfield int a:1 has possible values 0 and -1. Watch out for sign extension!
+//NOTE: avoid bitfields since endian-ness is indeterminate
struct Foo_normal {
int a;
View
7 3-functions.c
@@ -24,7 +24,12 @@ main(int argc, char **argv)
//stupidly, func = *func
printf("(*func)(5) = %d\n", (*func)(5));
- //cdecl.org deciphers function pointers
+ // NOTE: func contains a pointer to trampoline within
+ // the current stack frame. That pointer is not
+ // valid once this function exits and the frame
+ // is popped off the stack.
+
+ //http://cdecl.org/ deciphers function pointers
//e.g. int (*x(int (*)(int )))(int )
//x is a function that takes
//(pointer to function(int) returning int)
View
5 4-jumptable.c
@@ -40,6 +40,7 @@ switch_dispatch(int op, int x, int y)
return x / y;
case OP_SUB:
return x - y;
+ //NOTE: case ranges, "n ... m", is a gcc-specific extension
case 7 ... 10:
printf("switch_dispatch invoked with opcode in range 7 to 10\n");
break;
@@ -75,11 +76,13 @@ func_dispatch(int op, int x, int y)
//let's make a jumptable with function pointers
//compare with and without static
static int (*jumptable[OP_MAX])(int a, int b) = {
+ // NOTE: This type of initializer guarantees entire array
+ // is initialized.
//named index notation lets us specify things out-of-order
[OP_MUL] = mul,
[OP_SUB] = sub,
[OP_DIV] = div,
- [OP_ADD] = add
+ [OP_ADD] = add
};
return jumptable[op](x, y);
//or
View
5 5-macros.c
@@ -55,8 +55,9 @@ main(int argc, char **argv)
//scoped variable decls
//surround args with parens to isolate
#define max1(x, y) ((x) > (y) ? (x) : (y))
-//({}) is gcc
-//typeof(x) is gcc
+//({}) is gcc a extension called expression block.
+//An expression block evaluates to value of last expression.
+//typeof(x) is another gcc extension.
#define max2(x, y) ({\
typeof(x) _x = (x); \
typeof(y) _y = (y); \
View
7 5.5-xmacros.c
@@ -46,7 +46,10 @@ main(void)
LIST
};
#undef X
- goto *table2[baz];
+
+ unsigned int opcode = baz;
+interpret:
+ goto *table2[opcode];
//by defining GOTO_END as this type of macro, we can
//put it in the for loop
@@ -68,6 +71,8 @@ main(void)
}
the_end:
+// NOTE: If the intent is to interpret multiple opcodes,
+// could fetch next opcode and re-iterate at interpret:.
return 0;
}
View
9 Makefile
@@ -2,8 +2,15 @@ all: 1-typedefs 2-special_types 3-functions 4-jumptable 5-macros 5.5-xmacros 6-a
# You may need to remove -fnested-functions if your GCC doesn't support it
# This appears to be the case for earlier versions of GCC on Linux
+ifneq ($(strip $(shell uname -s)),Linux)
+FNESTED_FUNCTIONS_OPTION := -fnested-functions
+else
+FNESTED_FUNCTIONS_OPTION :=
+endif
+$(warning Using FNESTED_FUNCTIONS_OPTION = $(FNESTED_FUNCTIONS_OPTION))
+
%: %.c
- gcc -std=gnu99 -fnested-functions $< -o $@
+ gcc -std=gnu99 $(FNESTED_FUNCTIONS_OPTION) $< -o $@
jumptable.asm: 4-jumptable
objdump -D 4-jumptable > jumptable.asm
Please sign in to comment.
Something went wrong with that request. Please try again.