From 7b8c5e3212bd740d3ef9dc29c64796d5dc3f6036 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 12 Jun 2011 22:36:31 -0400 Subject: [PATCH 01/25] doing some puzzles! --- .gitignore | 1 + gowrikumar/00-sizeof.c | 21 +++++++++++++++++++++ gowrikumar/Makefile | 3 +++ 3 files changed, 25 insertions(+) create mode 100644 .gitignore create mode 100644 gowrikumar/00-sizeof.c create mode 100644 gowrikumar/Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..874b249 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gowrikumar/* diff --git a/gowrikumar/00-sizeof.c b/gowrikumar/00-sizeof.c new file mode 100644 index 0000000..9156974 --- /dev/null +++ b/gowrikumar/00-sizeof.c @@ -0,0 +1,21 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) +int array[] = {23,34,12,17,204,99,16}; + +int main() +{ + int d; + + for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) + printf("%d\n",array[d+1]); + + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile new file mode 100644 index 0000000..3670e45 --- /dev/null +++ b/gowrikumar/Makefile @@ -0,0 +1,3 @@ +all: 00-sizeof + +00-sizeof: 00-sizeof.o From 81f7260e3fa6f88f50b19cc8faa645f998e05334 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 12 Jun 2011 22:37:29 -0400 Subject: [PATCH 02/25] adding url because I forget stuff and don't know how to use bookmarks --- gowrikumar/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index 3670e45..76fbc35 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -1,3 +1,6 @@ +# puzzles from http://www.gowrikumar.com/c/ + + all: 00-sizeof 00-sizeof: 00-sizeof.o From 7f11279bfde5e2bc75eb7b05f265490f4310692e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 12 Jun 2011 23:02:18 -0400 Subject: [PATCH 03/25] ummmm, fixed? --- Makefile | 9 +++++++++ gowrikumar/00-sizeof.c | 18 ++++++++++-------- gowrikumar/Makefile | 4 ++++ 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ef38af9 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +DEBUG := +export DEBUG + + +all: + cd gowrikumar && $(MAKE) + + +.PHONY: all diff --git a/gowrikumar/00-sizeof.c b/gowrikumar/00-sizeof.c index 9156974..8981bfe 100644 --- a/gowrikumar/00-sizeof.c +++ b/gowrikumar/00-sizeof.c @@ -1,18 +1,20 @@ -/** - * :author: Dan Buch (daniel.buch@gmail.com) - */ - #include -#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) -int array[] = {23,34,12,17,204,99,16}; + +int array[] = {23, 34, 12, 17, 204, 99, 16}; +#define TOTAL_ELEMENTS sizeof(array) / sizeof(array[0]) + int main() { int d; - for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) - printf("%d\n",array[d+1]); +#ifdef DEBUG + printf("sizeof(array) = %d\n", TOTAL_ELEMENTS); +#endif + + for (d = 0; d < TOTAL_ELEMENTS ; d++) + printf("%d\n", array[d]); return 0; } diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index 76fbc35..a90a693 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -3,4 +3,8 @@ all: 00-sizeof + 00-sizeof: 00-sizeof.o + + +.PHONY: all From 7532ba866a753e6ffbc505aecae69d927d5a6bff Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 12 Jun 2011 23:51:17 -0400 Subject: [PATCH 04/25] not much to show for this one, but I'm pretty sure I get it :-P --- .gitignore | 3 ++- gowrikumar/02-dowhile.c | 20 ++++++++++++++++++++ gowrikumar/Makefile | 4 +++- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 gowrikumar/02-dowhile.c diff --git a/.gitignore b/.gitignore index 874b249..841658b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -gowrikumar/* +gowrikumar/00-sizeof +gowrikumar/02-dowhile diff --git a/gowrikumar/02-dowhile.c b/gowrikumar/02-dowhile.c new file mode 100644 index 0000000..eba0649 --- /dev/null +++ b/gowrikumar/02-dowhile.c @@ -0,0 +1,20 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +int main() +{ + int i = 1; + do + { + printf("%d\n", i); + } while(++i < 15); + return 0; +} + + + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index a90a693..ebef3f9 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -1,10 +1,12 @@ # puzzles from http://www.gowrikumar.com/c/ -all: 00-sizeof +all: 00-sizeof 02-dowhile 00-sizeof: 00-sizeof.o +00-dowhile: 00-dowhile.o + .PHONY: all From fc20882f74a52d9d8ff1fd4f28b67a9d3074489d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 13 Jun 2011 22:22:12 -0400 Subject: [PATCH 05/25] simplifying the Makefile a touch --- gowrikumar/Makefile | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index ebef3f9..6290501 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -1,12 +1,7 @@ # puzzles from http://www.gowrikumar.com/c/ -all: 00-sizeof 02-dowhile - - -00-sizeof: 00-sizeof.o - -00-dowhile: 00-dowhile.o +all: $(patsubst %.c,%,$(wildcard *.c)) .PHONY: all From 90e101db98c948b9ff905351ac499843a8c7a5c3 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 16 Jun 2011 21:29:28 -0400 Subject: [PATCH 06/25] exercise 3 --- .gitignore | 1 + gowrikumar/03-stdoutbuf.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 gowrikumar/03-stdoutbuf.c diff --git a/.gitignore b/.gitignore index 841658b..ba46549 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ gowrikumar/00-sizeof gowrikumar/02-dowhile +gowrikumar/03-stdoutbuf diff --git a/gowrikumar/03-stdoutbuf.c b/gowrikumar/03-stdoutbuf.c new file mode 100644 index 0000000..4cf1fb1 --- /dev/null +++ b/gowrikumar/03-stdoutbuf.c @@ -0,0 +1,22 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +#include + +int main() +{ + printf("(Hit ^C to stop the madness.)\n"); + while(1) + { + fprintf(stdout, "hello-out "); + fprintf(stderr, "hello-err "); + sleep(1); + } + return 0; +} + + +/* vim:filetype=c:fileencoding=utf-8 + */ From e026dabefbff3b213781e475e22f7aba761c334d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 16 Jun 2011 21:33:57 -0400 Subject: [PATCH 07/25] futzing with stdout flushing solutions --- .gitignore | 2 ++ gowrikumar/03b-stdoutbuf.c | 23 +++++++++++++++++++++++ gowrikumar/03c-stdoutbuf.c | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 gowrikumar/03b-stdoutbuf.c create mode 100644 gowrikumar/03c-stdoutbuf.c diff --git a/.gitignore b/.gitignore index ba46549..740fcd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ gowrikumar/00-sizeof gowrikumar/02-dowhile gowrikumar/03-stdoutbuf +gowrikumar/03b-stdoutbuf +gowrikumar/03c-stdoutbuf diff --git a/gowrikumar/03b-stdoutbuf.c b/gowrikumar/03b-stdoutbuf.c new file mode 100644 index 0000000..deb9800 --- /dev/null +++ b/gowrikumar/03b-stdoutbuf.c @@ -0,0 +1,23 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +#include + +int main() +{ + printf("This time we'll include \\n!\n"); + printf("(Hit ^C to stop the madness.)\n"); + while(1) + { + fprintf(stdout, "hello-out\n"); + fprintf(stderr, "hello-err\n"); + sleep(1); + } + return 0; +} + + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/03c-stdoutbuf.c b/gowrikumar/03c-stdoutbuf.c new file mode 100644 index 0000000..0402ec2 --- /dev/null +++ b/gowrikumar/03c-stdoutbuf.c @@ -0,0 +1,24 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +#include + +int main() +{ + printf("This time we'll flush stdout!\n"); + printf("(Hit ^C to stop the madness.)\n"); + while(1) + { + fprintf(stdout, "hello-out "); + fprintf(stderr, "hello-err "); + fflush(stdout); + sleep(1); + } + return 0; +} + + +/* vim:filetype=c:fileencoding=utf-8 + */ From 731e1002cd71b2dd48a95c8ecaf610d7336cb3f2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 16 Jun 2011 21:46:56 -0400 Subject: [PATCH 08/25] cleaning things up a bit --- .gitignore | 6 +----- gowrikumar/Makefile | 7 +++++-- gowrikumar/bin/.keep | 0 gowrikumar/{ => src}/00-sizeof.c | 0 gowrikumar/{ => src}/02-dowhile.c | 0 gowrikumar/{ => src}/03-stdoutbuf.c | 0 gowrikumar/{ => src}/03b-stdoutbuf.c | 0 gowrikumar/{ => src}/03c-stdoutbuf.c | 0 gowrikumar/src/04-macrodef.c | 19 +++++++++++++++++++ gowrikumar/src/Makefile | 14 ++++++++++++++ 10 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 gowrikumar/bin/.keep rename gowrikumar/{ => src}/00-sizeof.c (100%) rename gowrikumar/{ => src}/02-dowhile.c (100%) rename gowrikumar/{ => src}/03-stdoutbuf.c (100%) rename gowrikumar/{ => src}/03b-stdoutbuf.c (100%) rename gowrikumar/{ => src}/03c-stdoutbuf.c (100%) create mode 100644 gowrikumar/src/04-macrodef.c create mode 100644 gowrikumar/src/Makefile diff --git a/.gitignore b/.gitignore index 740fcd7..d800b0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1 @@ -gowrikumar/00-sizeof -gowrikumar/02-dowhile -gowrikumar/03-stdoutbuf -gowrikumar/03b-stdoutbuf -gowrikumar/03c-stdoutbuf +gowrikumar/bin diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index 6290501..ef886fd 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -1,7 +1,10 @@ -# puzzles from http://www.gowrikumar.com/c/ +BINDIR := ./bin + +export BINDIR -all: $(patsubst %.c,%,$(wildcard *.c)) +all: + cd src && $(MAKE) .PHONY: all diff --git a/gowrikumar/bin/.keep b/gowrikumar/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/gowrikumar/00-sizeof.c b/gowrikumar/src/00-sizeof.c similarity index 100% rename from gowrikumar/00-sizeof.c rename to gowrikumar/src/00-sizeof.c diff --git a/gowrikumar/02-dowhile.c b/gowrikumar/src/02-dowhile.c similarity index 100% rename from gowrikumar/02-dowhile.c rename to gowrikumar/src/02-dowhile.c diff --git a/gowrikumar/03-stdoutbuf.c b/gowrikumar/src/03-stdoutbuf.c similarity index 100% rename from gowrikumar/03-stdoutbuf.c rename to gowrikumar/src/03-stdoutbuf.c diff --git a/gowrikumar/03b-stdoutbuf.c b/gowrikumar/src/03b-stdoutbuf.c similarity index 100% rename from gowrikumar/03b-stdoutbuf.c rename to gowrikumar/src/03b-stdoutbuf.c diff --git a/gowrikumar/03c-stdoutbuf.c b/gowrikumar/src/03c-stdoutbuf.c similarity index 100% rename from gowrikumar/03c-stdoutbuf.c rename to gowrikumar/src/03c-stdoutbuf.c diff --git a/gowrikumar/src/04-macrodef.c b/gowrikumar/src/04-macrodef.c new file mode 100644 index 0000000..30b641f --- /dev/null +++ b/gowrikumar/src/04-macrodef.c @@ -0,0 +1,19 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +#define f(a, b) a##b +#define g(a) #a +#define h(a) g(a) + +int main() +{ + printf("%s\n", h(f(1, 2))); + printf("%s\n", g(f(1, 2))); + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/src/Makefile b/gowrikumar/src/Makefile new file mode 100644 index 0000000..b8b21ff --- /dev/null +++ b/gowrikumar/src/Makefile @@ -0,0 +1,14 @@ +# puzzles from http://www.gowrikumar.com/c/ + +BINDIR := ../bin + +ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c)) + + +$(BINDIR)/%: %.c + $(CC) -o $@ $< + +all: $(ALL_BIN) + + +.PHONY: all From 4a783c9a7ae2de2de4be1491159c541ed7dfe354 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 16 Jun 2011 21:48:15 -0400 Subject: [PATCH 09/25] in case I feel like cleaning --- Makefile | 5 ++++- gowrikumar/Makefile | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ef38af9..de20a16 100644 --- a/Makefile +++ b/Makefile @@ -5,5 +5,8 @@ export DEBUG all: cd gowrikumar && $(MAKE) +clean: + cd gowrikumar && $(MAKE) clean -.PHONY: all + +.PHONY: all clean diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index ef886fd..00fcf23 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -6,5 +6,8 @@ export BINDIR all: cd src && $(MAKE) +clean: + rm -v $(BINDIR)/* -.PHONY: all + +.PHONY: all clean From 376ef0353f846dbc9f6fe32c43005b156679ccb3 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 14:07:11 -0400 Subject: [PATCH 10/25] just some dumb makefile cleanup --- Makefile | 10 ++++++---- gowrikumar/Makefile | 7 ++++--- gowrikumar/src/Makefile | 2 -- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index de20a16..4b51e73 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,14 @@ -DEBUG := -export DEBUG +CD = cd +RM = rm -v + +export CD RM all: - cd gowrikumar && $(MAKE) + $(CD) gowrikumar && $(MAKE) clean: - cd gowrikumar && $(MAKE) clean + $(CD) gowrikumar && $(MAKE) clean .PHONY: all clean diff --git a/gowrikumar/Makefile b/gowrikumar/Makefile index 00fcf23..37177b9 100644 --- a/gowrikumar/Makefile +++ b/gowrikumar/Makefile @@ -1,13 +1,14 @@ -BINDIR := ./bin +# puzzles from http://www.gowrikumar.com/c/ +BINDIR := $(PWD)/bin export BINDIR all: - cd src && $(MAKE) + $(CD) src && $(MAKE) clean: - rm -v $(BINDIR)/* + $(RM) $(BINDIR)/* .PHONY: all clean diff --git a/gowrikumar/src/Makefile b/gowrikumar/src/Makefile index b8b21ff..80b475b 100644 --- a/gowrikumar/src/Makefile +++ b/gowrikumar/src/Makefile @@ -1,7 +1,5 @@ # puzzles from http://www.gowrikumar.com/c/ -BINDIR := ../bin - ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c)) From 79436cc73118e4dda8226ebc24649ff477d3f552 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 14:28:00 -0400 Subject: [PATCH 11/25] futzing around with macros some more --- Makefile | 4 +++- gowrikumar/src/04b-macrodef.c | 42 +++++++++++++++++++++++++++++++++++ gowrikumar/src/Makefile | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 gowrikumar/src/04b-macrodef.c diff --git a/Makefile b/Makefile index 4b51e73..ac357a5 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ CD = cd RM = rm -v -export CD RM +CFLAGS := -std=c99 + +export CD RM CFLAGS all: diff --git a/gowrikumar/src/04b-macrodef.c b/gowrikumar/src/04b-macrodef.c new file mode 100644 index 0000000..3dd0bc4 --- /dev/null +++ b/gowrikumar/src/04b-macrodef.c @@ -0,0 +1,42 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +#define f(a, b) a##b +#define g(a) #a +#define h(a) g(a) +#define border(c) \ + for (int i = 0; i < 60; i++) { \ + printf(c); \ + } \ + printf("\n"); + +int main() +{ + printf("%s\n", h(f(1, 2))); + printf("%s\n", g(f(1, 2))); + + printf("%s\n", g(printf("dogs rule cats drool\n"))); + + char * hambones = "Cats! " g(ham); + char * bonesham = "Meats! " g(bones); + char * tmp; + + border("-"); + printf("bonesham = %s\n", bonesham); + printf("hambones = %s\n", hambones); + + border("-"); + tmp = f(ham, bones); + f(ham, bones) = f(bones, ham); + f(bones, ham) = tmp; + + printf("bonesham = %s\n", bonesham); + printf("hambones = %s\n", hambones); + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/src/Makefile b/gowrikumar/src/Makefile index 80b475b..738079a 100644 --- a/gowrikumar/src/Makefile +++ b/gowrikumar/src/Makefile @@ -4,7 +4,7 @@ ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c)) $(BINDIR)/%: %.c - $(CC) -o $@ $< + $(CC) $(CFLAGS) -o $@ $< all: $(ALL_BIN) From b1172d6c6e3b86f001227f9ced91b5fff9194b44 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 15:06:29 -0400 Subject: [PATCH 12/25] futzing with relationship between switch/case and labels --- .gitignore | 2 ++ Makefile | 3 ++- gowrikumar/src/05-switchint.c | 24 ++++++++++++++++++++++++ gowrikumar/src/05b-switchint.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 gowrikumar/src/05-switchint.c create mode 100644 gowrikumar/src/05b-switchint.c diff --git a/.gitignore b/.gitignore index d800b0d..fe46976 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ gowrikumar/bin +*.i +*.s diff --git a/Makefile b/Makefile index ac357a5..e1682e8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ CD = cd RM = rm -v -CFLAGS := -std=c99 +CC := gcc +CFLAGS := -std=c99 -Wall export CD RM CFLAGS diff --git a/gowrikumar/src/05-switchint.c b/gowrikumar/src/05-switchint.c new file mode 100644 index 0000000..6a5b568 --- /dev/null +++ b/gowrikumar/src/05-switchint.c @@ -0,0 +1,24 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +int main() +{ + int a=10; + switch(a) + { + case '1': + printf("ONE\n"); + break; + case '2': + printf("TWO\n"); + break; + defa1ut: + printf("NONE\n"); + } + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/src/05b-switchint.c b/gowrikumar/src/05b-switchint.c new file mode 100644 index 0000000..99368be --- /dev/null +++ b/gowrikumar/src/05b-switchint.c @@ -0,0 +1,32 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#define MAGIC_NUMBER 10 + +#include +int main() +{ + int a = MAGIC_NUMBER; + switch(a) + { + case '1': + printf("ONE\n"); + break; + case '2': + printf("TWO\n"); + break; + defalut: + printf("NO CAN SPELL\n"); + break; + defau1t: + printf("SO CLOSE, YET SO FAR\n"); + break; + default: + printf("NONE\n"); + } + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From 70d09f33a1e932baba65d816909336b50759a48c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 15:27:23 -0400 Subject: [PATCH 13/25] sizeof integer pointers ? --- gowrikumar/src/06-ia64segfault.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 gowrikumar/src/06-ia64segfault.c diff --git a/gowrikumar/src/06-ia64segfault.c b/gowrikumar/src/06-ia64segfault.c new file mode 100644 index 0000000..6e35d74 --- /dev/null +++ b/gowrikumar/src/06-ia64segfault.c @@ -0,0 +1,16 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +int main() +{ + int* p; + p = (int*)malloc(sizeof(int)); + *p = 10; + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From a414f515d1e18b373b46024e825148571d028e63 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 19:28:20 -0400 Subject: [PATCH 14/25] must. get. 64-bit machine. --- gowrikumar/src/06b-ia64segfault.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 gowrikumar/src/06b-ia64segfault.c diff --git a/gowrikumar/src/06b-ia64segfault.c b/gowrikumar/src/06b-ia64segfault.c new file mode 100644 index 0000000..57bc6f2 --- /dev/null +++ b/gowrikumar/src/06b-ia64segfault.c @@ -0,0 +1,29 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +#include + +int main() +{ + int* p; + + size_t size_of_int = sizeof(int); + printf("sizeof(int) = %d\n", size_of_int); + + p = (int*)malloc(sizeof(int)); + + size_t size_of_p = sizeof(p); + printf("sizeof(p) = %d\n", size_of_p); + + *p = 10; + + size_t size_of_ptr_p = sizeof(*p); + printf("sizeof(*p) = %d\n", size_of_ptr_p); + + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From f20b8ca1389e20c36286eed158120c971fea720a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 19:37:54 -0400 Subject: [PATCH 15/25] counting bits! --- gowrikumar/src/07-countbits.c | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 gowrikumar/src/07-countbits.c diff --git a/gowrikumar/src/07-countbits.c b/gowrikumar/src/07-countbits.c new file mode 100644 index 0000000..73a72dc --- /dev/null +++ b/gowrikumar/src/07-countbits.c @@ -0,0 +1,40 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +static unsigned int mask[] = { + 0x55555555, + 0x33333333, + 0x0F0F0F0F, + 0x00FF00FF, + 0x0000FFFF +}; + + +int get_bit_count(unsigned int x) +{ + + int i; + int shift; /* Number of positions to shift to right*/ + for (i = 0, shift = 1; i < 5; i++, shift *= 2) { + x = (x & mask[i])+ ((x >> shift) & mask[i]); + } + return x; +} + + +int main() +{ + int to_test[] = {0, 5, 7}; + int current; + + for (int i = 0; i < 3 ; i++) { + current = to_test[i]; + printf("get_bit_count(%d) = %d\n", current, get_bit_count(current)); + } +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From b447c7a61aae763f5f8a4b203efcd7a3193aa663 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 19:39:37 -0400 Subject: [PATCH 16/25] more futzing with whitespace in bit counter --- gowrikumar/src/07-countbits.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gowrikumar/src/07-countbits.c b/gowrikumar/src/07-countbits.c index 73a72dc..aad54a4 100644 --- a/gowrikumar/src/07-countbits.c +++ b/gowrikumar/src/07-countbits.c @@ -19,7 +19,7 @@ int get_bit_count(unsigned int x) int i; int shift; /* Number of positions to shift to right*/ for (i = 0, shift = 1; i < 5; i++, shift *= 2) { - x = (x & mask[i])+ ((x >> shift) & mask[i]); + x = (x & mask[i]) + ((x >> shift) & mask[i]); } return x; } From 9ab4fa72796297632cc68f6c751339e7a04a47a7 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 19:57:39 -0400 Subject: [PATCH 17/25] a more verbose version of the bit counter --- gowrikumar/src/07b-countbits.c | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 gowrikumar/src/07b-countbits.c diff --git a/gowrikumar/src/07b-countbits.c b/gowrikumar/src/07b-countbits.c new file mode 100644 index 0000000..e363587 --- /dev/null +++ b/gowrikumar/src/07b-countbits.c @@ -0,0 +1,67 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +static unsigned int mask[] = { + 0x55555555, + 0x33333333, + 0x0F0F0F0F, + 0x00FF00FF, + 0x0000FFFF +}; + + +int get_bit_count(unsigned int x) +{ + printf("get_bit_count(%d)\n", x); + + int i; + int x_and_mask; + int x_shifted; + int x_shifted_and_mask; + int shift; /* Number of positions to shift to right*/ + for (i = 0, shift = 1; i < 5; i++, shift *= 2) { + printf(" START loop\n"); + printf(" i = %d, x = %d, shift = %d\n", i, x, shift); + + x_and_mask = x & mask[i]; + printf(" x & mask[i] = %d\n", x_and_mask); + + x_shifted = x >> shift; + printf(" x >> shift = %d\n", x_shifted); + + x_shifted_and_mask = x_shifted & mask[i]; + printf(" (x >> shift) & mask[i] = %d\n", x_shifted_and_mask); + + x = x_and_mask + x_shifted_and_mask; + printf(" x = %d\n", x); + printf(" END loop\n"); + } + + return x; +} + + +int main() +{ + int to_test[] = {0, 5, 7}; + int current; + + /* + size_t mask_len = sizeof(mask) / sizeof(unsigned int); + + for (int i = 0; i < mask_len; i++) { + printf("mask[%d] = %d\n", i, mask[i]); + } + */ + + for (int i = 0; i < 3 ; i++) { + current = to_test[i]; + printf("get_bit_count(%d) -> %d\n", current, get_bit_count(current)); + } +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From 3c51da2e98c42321fe95825950261cc40a820cdc Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 18 Jun 2011 20:01:05 -0400 Subject: [PATCH 18/25] futzing with a tricky float increment --- gowrikumar/src/08-incrfloat.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 gowrikumar/src/08-incrfloat.c diff --git a/gowrikumar/src/08-incrfloat.c b/gowrikumar/src/08-incrfloat.c new file mode 100644 index 0000000..cae6c20 --- /dev/null +++ b/gowrikumar/src/08-incrfloat.c @@ -0,0 +1,26 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +int main() +{ + float f = 0.0f; + int i; + + for (i = 0; i < 10; i++) { + f = f + 0.1f; + } + + if (f == 1.0f) { + printf("f is 1.0 \n"); + } else { + printf("f is NOT 1.0\n"); + } + + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From cf6f47c9900479c14bbee563661d10966f6c0e09 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Jun 2011 07:28:23 -0400 Subject: [PATCH 19/25] float comparison! --- gowrikumar/src/08b-incrfloat.c | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 gowrikumar/src/08b-incrfloat.c diff --git a/gowrikumar/src/08b-incrfloat.c b/gowrikumar/src/08b-incrfloat.c new file mode 100644 index 0000000..782aedb --- /dev/null +++ b/gowrikumar/src/08b-incrfloat.c @@ -0,0 +1,43 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +#include + +int main() +{ + float f = 0.0f; + int i; + + for (i = 0; i < 10; i++) { + f = f + 0.1f; + } + + if (f == 1.0) { + printf("f is 1.0 \n"); + /* nope! */ + } + + if (f == 1.000000) { + printf("f is perhaps 1.000000?\n"); + /* nope again */ + } + + printf("f is neither 1.0 nor 1.000000\n"); + printf("(f is really %.24f)\n", f); + printf("OH NOES!\n"); + printf("But...\n"); + + double difference = fabs(f - 1.000000); + + if (difference < 0.00001) { + printf("f is close enough to 1.000000 (off by %.24f)\n", difference); + /* yes? */ + } + + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From 803101b36f1804b41a7e261380437acc456e8d21 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Jun 2011 07:37:44 -0400 Subject: [PATCH 20/25] another bug exercise --- gowrikumar/src/09-multipleassignment.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 gowrikumar/src/09-multipleassignment.c diff --git a/gowrikumar/src/09-multipleassignment.c b/gowrikumar/src/09-multipleassignment.c new file mode 100644 index 0000000..3cf7b4b --- /dev/null +++ b/gowrikumar/src/09-multipleassignment.c @@ -0,0 +1,20 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +int main() +{ + /* FIXME: the comma after "a = 1" makes the compiler angry. Either the + * ", 2" should be removed or another assignment should be added. + int a = 1, 2; + */ + int a = 1, b = 2; + printf("a: %d\n", a); + printf("b: %d\n", b); + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From b95730410bd07883895295bd7e1192cdaeb2d6bd Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Jun 2011 07:45:12 -0400 Subject: [PATCH 21/25] futzing with printf return --- gowrikumar/src/10-nestedprintf.c | 15 +++++++++++++++ gowrikumar/src/10b-nestedprintf.c | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 gowrikumar/src/10-nestedprintf.c create mode 100644 gowrikumar/src/10b-nestedprintf.c diff --git a/gowrikumar/src/10-nestedprintf.c b/gowrikumar/src/10-nestedprintf.c new file mode 100644 index 0000000..857741c --- /dev/null +++ b/gowrikumar/src/10-nestedprintf.c @@ -0,0 +1,15 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +int main() +{ + int i = 43; + printf("%d\n", printf("%d", printf("%d", i))); + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/src/10b-nestedprintf.c b/gowrikumar/src/10b-nestedprintf.c new file mode 100644 index 0000000..0e0304d --- /dev/null +++ b/gowrikumar/src/10b-nestedprintf.c @@ -0,0 +1,17 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +int main() +{ + int i = 43, nprinted; + nprinted = printf("%d\n", printf("%d", printf("%d", i))); + nprinted = printf("(the last one was %d characters)\n", nprinted); + nprinted = printf("(and that was %d characters)\n", nprinted); + return 0; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From 2f315a6353843cdb35d505f15ca993a80dc9fb89 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Jun 2011 18:27:45 -0400 Subject: [PATCH 22/25] working on "duff" example led me to futzing with gdb, found out the gdb tutorial is kinda (a lot) outdated --- .gitignore | 1 + Makefile | 4 +++- gdbtut/Makefile | 14 ++++++++++++++ gdbtut/bin/.keep | 0 gdbtut/src/Makefile | 12 ++++++++++++ gdbtut/src/segfault.c | 28 +++++++++++++++++++++++++++ gowrikumar/src/11-duff.c | 37 ++++++++++++++++++++++++++++++++++++ gowrikumar/src/11b-duff.c | 40 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 gdbtut/Makefile create mode 100644 gdbtut/bin/.keep create mode 100644 gdbtut/src/Makefile create mode 100644 gdbtut/src/segfault.c create mode 100644 gowrikumar/src/11-duff.c create mode 100644 gowrikumar/src/11b-duff.c diff --git a/.gitignore b/.gitignore index fe46976..e1392e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ gowrikumar/bin +gdbtut/bin *.i *.s diff --git a/Makefile b/Makefile index e1682e8..617e404 100644 --- a/Makefile +++ b/Makefile @@ -2,16 +2,18 @@ CD = cd RM = rm -v CC := gcc -CFLAGS := -std=c99 -Wall +CFLAGS := -std=c99 -Wall -g export CD RM CFLAGS all: $(CD) gowrikumar && $(MAKE) + $(CD) gdbtut && $(MAKE) clean: $(CD) gowrikumar && $(MAKE) clean + $(CD) gdbtut && $(MAKE) clean .PHONY: all clean diff --git a/gdbtut/Makefile b/gdbtut/Makefile new file mode 100644 index 0000000..d7d488d --- /dev/null +++ b/gdbtut/Makefile @@ -0,0 +1,14 @@ +# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/ + +BINDIR := $(PWD)/bin +export BINDIR + + +all: + $(CD) src && $(MAKE) + +clean: + $(RM) $(BINDIR)/* + + +.PHONY: all clean diff --git a/gdbtut/bin/.keep b/gdbtut/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/gdbtut/src/Makefile b/gdbtut/src/Makefile new file mode 100644 index 0000000..854655b --- /dev/null +++ b/gdbtut/src/Makefile @@ -0,0 +1,12 @@ +# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/ + +ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c)) + + +$(BINDIR)/%: %.c + $(CC) $(CFLAGS) -o $@ $< + +all: $(ALL_BIN) + + +.PHONY: all diff --git a/gdbtut/src/segfault.c b/gdbtut/src/segfault.c new file mode 100644 index 0000000..11e93a1 --- /dev/null +++ b/gdbtut/src/segfault.c @@ -0,0 +1,28 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include +#include + +int main(int argc, char **argv) +{ + char *buf; + long long huge = 8000000000000000000; + /* Okay, so this is *not* going to segfault because + * the way memory is allocated has changed since + * the tutorial was written. The segfault is supposed + * to happen when more memory is allocated than is + * available on the machine. So much for that exercise. + */ + + buf = malloc(huge); + + fgets(buf, 1024, stdin); + printf("%s\n", buf); + + return 1; +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/src/11-duff.c b/gowrikumar/src/11-duff.c new file mode 100644 index 0000000..bb7dbfd --- /dev/null +++ b/gowrikumar/src/11-duff.c @@ -0,0 +1,37 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +void duff(register char *to, register char *from, register int count) +{ + register int n = (count + 7) / 8; + switch (count % 8) { + case 0: do { *to++ = *from++; + case 7: *to++ = *from++; + case 6: *to++ = *from++; + case 5: *to++ = *from++; + case 4: *to++ = *from++; + case 3: *to++ = *from++; + case 2: *to++ = *from++; + case 1: *to++ = *from++; + } while (--n > 0); + } +} + +int main() +{ + char * to = "dogs cats babies"; + char * from = "monkey hat pants"; + int i = 16; + printf("to = %s\n", to); + printf("from = %s\n", from); + /* And here comes the segfault... */ + duff(to, from, i); + printf("to = %s\n", to); + printf("from = %s\n", from); +} + +/* vim:filetype=c:fileencoding=utf-8 + */ diff --git a/gowrikumar/src/11b-duff.c b/gowrikumar/src/11b-duff.c new file mode 100644 index 0000000..3139220 --- /dev/null +++ b/gowrikumar/src/11b-duff.c @@ -0,0 +1,40 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + +void duff(register char *to, register char *from, register int count) +{ + register int n; + + int remainder = count % 8; + if (remainder == 0) { + n = (count + 7) / 8; + do { + *to++ = *from++; + } while (--n > 0); + } else if (remainder <= 7 && remainder > 0) { + int i; + int c; + for (i = 0; (c = from[i] && c != EOF); i++) { + to[i] = c; + } + } +} + +int main() +{ + char * to = "dogs cats babies"; + char * from = "monkey hat pants"; + int i = 11; + printf("to = %s\n", to); + printf("from = %s\n", from); + /* And here comes the segfault... */ + duff(to, from, i); + printf("to = %s\n", to); + printf("from = %s\n", from); +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From 0e86ed72e28d0db41603e29aa60e7bb10400b00d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Jun 2011 18:29:53 -0400 Subject: [PATCH 23/25] here's your README, github ;-) --- README | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..52b53eb --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +I don't get paid to write C for a living, so I have to sneak in practice here +and there. Maybe once the kiddo goes to college I can become a kernel hacker +:-P (assuming there's still such a thing as a kernel.) From 74dfe433b92946393bf38a425e4adb6cf1c6f232 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Jun 2011 18:38:51 -0400 Subject: [PATCH 24/25] alternate implementation of CountBits --- gowrikumar/src/12-countbits2.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 gowrikumar/src/12-countbits2.c diff --git a/gowrikumar/src/12-countbits2.c b/gowrikumar/src/12-countbits2.c new file mode 100644 index 0000000..d79e1ce --- /dev/null +++ b/gowrikumar/src/12-countbits2.c @@ -0,0 +1,32 @@ +/** + * :author: Dan Buch (daniel.buch@gmail.com) + */ + +#include + + +int get_bit_count(unsigned int x) +{ + int count=0; + while(x) + { + count++; + x = x&(x-1); + } + return count; +} + + +int main() +{ + int to_test[] = {0, 5, 7}; + int current; + + for (int i = 0; i < 3 ; i++) { + current = to_test[i]; + printf("get_bit_count(%d) = %d\n", current, get_bit_count(current)); + } +} + +/* vim:filetype=c:fileencoding=utf-8 + */ From ef128a996fc826339bbc3d9deea376932caf6981 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 6 Nov 2011 09:53:00 -0500 Subject: [PATCH 25/25] a little futzing around with sockets while reading through the linux programming interface book --- .gitignore | 10 ++++++---- Makefile | 4 +++- socky.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 socky.c diff --git a/.gitignore b/.gitignore index e1392e7..640ef7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -gowrikumar/bin -gdbtut/bin -*.i -*.s +* +!gowrikumar/bin +!gdbtut/bin +!*.i +!*.s +!*.c diff --git a/Makefile b/Makefile index 617e404..03c063e 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,12 @@ RM = rm -v CC := gcc CFLAGS := -std=c99 -Wall -g +ALL_TARGETS := $(patsubst %.c,%,$(wildcard *.c)) + export CD RM CFLAGS -all: +all: $(ALL_TARGETS) $(CD) gowrikumar && $(MAKE) $(CD) gdbtut && $(MAKE) diff --git a/socky.c b/socky.c new file mode 100644 index 0000000..cf27e98 --- /dev/null +++ b/socky.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + +const char *SOCKNAME = "/tmp/socky"; +int sfd; +struct sockaddr_un addr; + + +int main(int argc, char *argv[]) +{ + sfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sfd == -1) { + fprintf(stderr, "FAILED TO ALLOCATE SOCKET\n"); + goto error; + } + + memset(&addr, 0, sizeof(struct sockaddr_un)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, SOCKNAME, sizeof(addr.sun_path) - 1); + + if (bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) { + fprintf(stderr, "FAILED TO BIND SOCKET TO ADDRESS\n"); + goto error; + } + + if (listen(sfd, 128) == -1) { + fprintf(stderr, "FAILED TO LISTEN ON ADDRESS\n"); + goto error; + } + + for (;;) { + fprintf(stdout, "ATTEMPTING TO WAIT FOR REQUEST ON %s\n", SOCKNAME); + if (accept(sfd, NULL, NULL) == -1) { + fprintf(stderr, "FAILED TO ACCEPT REQUEST ON ADDRESS\n"); + goto error; + } + } + + return 0; + +error: + return 1; + +}