changeset 9:2ce34a5ff69b default tip

add pointer test
author taiki
date Tue, 08 Jan 2013 12:34:40 +0900
parents 1d2839ecabda
children
files cbc_thread/cbc_thread_test.c pointer/pointer.c quick_sort/quick_sort.cbc
diffstat 3 files changed, 68 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/cbc_thread/cbc_thread_test.c	Sat Dec 29 12:02:56 2012 +0900
+++ b/cbc_thread/cbc_thread_test.c	Tue Jan 08 12:34:40 2013 +0900
@@ -2,7 +2,39 @@
 #include <stdlib.h>
 #include <pthread.h>
 
-int main()
+typedef struct _DS {
+    int thread_no;
+    int *data;
+} DS;
+
+__code start1()
+{
+    printf("into cs \n");
+}
+
+void start(void *arg)
 {
+    DS *ds = (DS*)arg;
+    printf("thread%d:\t%d\n", ds->thread_no, ds->data[0]);
+    goto start1();
+}
+
+
+int main(int argc, char* argv[])
+{
+    pthread_t handle; 
+    DS ds;
+
+    int data[2];
+    
+    int i=0;
+    for (; i < 2; i++) data [i] = i;
+
+    ds.thread_no = 1;
+    ds.data = data;
+
+    pthread_create(&handle, NULL, (void*)start, (void*)&ds);
+
+    pthread_join(handle, NULL);
     return 0;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pointer/pointer.c	Tue Jan 08 12:34:40 2013 +0900
@@ -0,0 +1,25 @@
+#include <stdio.h>
+
+__code A()
+{
+    printf("into __code A.\n");
+}
+
+void* get_cs()
+{
+    return A;
+}
+
+void start()
+{
+    printf("start\n");
+    __code (*cs)() = get_cs();
+    goto (*cs)();
+}
+
+int main()
+{
+    printf("main\n");
+    start();
+    return 0;
+}
--- a/quick_sort/quick_sort.cbc	Sat Dec 29 12:02:56 2012 +0900
+++ b/quick_sort/quick_sort.cbc	Tue Jan 08 12:34:40 2013 +0900
@@ -1,10 +1,3 @@
-/* 
- * This program is quick sort.
- * move low value to low subscript, and high value to high subscript.
- *
- *
- *
- */
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
@@ -15,104 +8,22 @@
     COUNT_START_VALUE = 0,
 };
 
-struct array_state {
-    int *int_array_p;
-    int *divide_index;
-    int index_num;
-    int array_length;
-    int upper_count;
-    int lower_count;
-    int divide_count;
-    int base_value;
-    int low_tmp;
-    int up_tmp;
-};
-
-__code search_lower_array(struct array_state *as_p);
-__code manage_array(struct array_state *as_p);
-
-__code change_tmp_num(struct array_state *as_p)
+__code quick_sort(int *int_array)
 {
-    int tmp = *(as_p->int_array_p + as_p->upper_count);
-    *(as_p->int_array_p + as_p->upper_count) = *(as_p->int_array_p + as_p->lower_count);
-    *(as_p->int_array_p + as_p->lower_count) = tmp;
-    if (as_p->upper_count > as_p->lower_count) {
-        goto search_lower_array(as_p);
-    } else {
-        as_p->index_num++;
-        goto manage_array(as_p);
-    }
-}
-
-__code search_upper_array(struct array_state *as_p)
-{
-    if (as_p->base_value < *(as_p->int_array_p + as_p->upper_count)) {
-        as_p->upper_count--;
-        goto search_lower_array(as_p);
-    } else {
-        goto change_tmp_num(as_p);
-    }
+    
 }
 
-
-__code search_lower_array(struct array_state *as_p)
-{
-    if (as_p->base_value < *(as_p->int_array_p + as_p->lower_count)) {
-        as_p->lower_count++;
-        goto search_upper_array(as_p);
-    } else {
-        search_lower_array(as_p);
-    }
-}
-
-__code manage_array(struct array_state *as_p)
-{
-    if (as_p->index_num!=0) {
-        as_p->divide_index[as_p->index_num] = as_p->upper_count; 
-        as_p->base_value = as_p->base_value/2;
-    } 
-    goto search_lower_array(as_p);
-}
-
-__code set_base_value(int *int_array_p)
-{
-    
-    int base_value = *(int_array_p + (ARRAY_MAX / 2));
-    int divide_index[100];
-    struct array_state *as_p = malloc(sizeof(struct array_state));
-    as_p->int_array_p = int_array_p;
-    as_p->index_num = 0;
-    divide_index[as_p->index_num] = 0;
-    as_p->divide_index = divide_index;
-    as_p->upper_count = ARRAY_MAX;
-    as_p->lower_count = COUNT_START_VALUE;
-    as_p->base_value = base_value;
-    as_p->array_length = ARRAY_MAX;
-
-    goto manage_array(as_p);
-}
-
-__code print_value(int *int_array_p, int count)
+// init array by random value.
+__code init_random(int *int_array, int count)
 {
     if (count < ARRAY_MAX) {
+        *(int_array + count) = (int)(rand() * (ARRAY_MAX + 1.0)/(RAND_MAX + 1.0));
+        printf("[%d] %d \n", count, *(int_array + count));
         count++;
-        printf("[%d] %d \n", count, *(int_array_p + count));
-        goto print_value(int_array_p, count);
+        goto init_random(int_array, count); 
     } else {
         printf("\n");
-        goto set_base_value(int_array_p);
-    }
-
-}
-
-__code init_random(int *int_array_p, int count)
-{
-    if (count < ARRAY_MAX) {
-        *(int_array_p + count) = (int)(rand() * (ARRAY_MAX + 1.0)/(RAND_MAX + 1.0));
-        count++;
-        goto init_random(int_array_p, count); 
-    } else {
-        goto print_value(int_array_p, COUNT_START_VALUE);
+        goto quick_sort(int_array);
     }
 }
 
@@ -120,9 +31,9 @@
 void
 make_array()
 {
-    int *int_array_p = malloc(ARRAY_MAX * sizeof(int));
+    int *int_array = malloc(ARRAY_MAX * sizeof(int));
     srand((unsigned int)time(NULL));
-    goto init_random(int_array_p, COUNT_START_VALUE);
+    goto init_random(int_array, COUNT_START_VALUE);
 }
 
 int