Changeset bd85400 for src/examples
- Timestamp:
- Feb 5, 2016, 5:09:04 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 52f85e0, 6ed1d4b, d41280e, d63eeb0
- Parents:
- 4789f44
- Location:
- src/examples
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/abs.c
r4789f44 rbd85400 10 10 // Created On : Thu Jan 28 18:26:16 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 31 09:24:45201613 // Update Count : 4 112 // Last Modified On : Wed Feb 3 11:14:58 2016 13 // Update Count : 43 14 14 // 15 15 16 16 #include <fstream> 17 #include < algorithm>17 #include <stdlib> // abs 18 18 19 19 int main( void ) { -
src/examples/alloc.c
r4789f44 rbd85400 1 #if 0 1 // -*- Mode: C -*- 2 // 3 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 4 // 5 // The contents of this file are covered under the licence agreement in the 6 // file "LICENCE" distributed with Cforall. 7 // 8 // alloc.c -- 9 // 10 // Author : Peter A. Buhr 11 // Created On : Wed Feb 3 07:56:22 2016 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Wed Feb 3 16:32:04 2016 14 // Update Count : 38 15 // 16 17 #include <fstream> 18 #include <stdlib> 2 19 extern "C" { 3 typedef long unsigned int size_t; 4 void *malloc( size_t size ); 5 void *calloc( size_t nmemb, size_t size ); 6 void *realloc( void *ptr, size_t size ); 7 void *memset( void *s, int c, size_t n ); 8 void free( void * ptr ); 9 int printf( const char *, ... ); 10 } 20 #include <stdlib.h> // access C malloc, realloc 21 #include <stdio.h> 22 } // exten "C" 11 23 12 forall( type T ) T * malloc( void ) { 13 return (T *)malloc( sizeof(T) ); 14 } 15 forall( type T ) T * calloc( size_t size ) { 16 return (T *)calloc( size, sizeof(T) ); 17 } 18 #endif 19 typedef int size_t; 20 forall( type T ) T * realloc( T *ptr, size_t n ) { 21 // return (T *)(void *)realloc( ptr, sizeof(T) ); 22 } 23 //forall( type T ) T * realloc( T *ptr, size_t n, T c ) { 24 // return (T *)realloc( ptr, n ); 25 //} 26 27 //int *foo( int *p, int c ); 28 //int *bar( int *p, int c ); 29 //int *baz( int *p, int c ); 24 int * foo( int * p, int c ) { return p; } 25 int * bar( int * p, int c ) { return p; } 26 int * baz( int * p, int c ) { return p; } 30 27 31 28 int main( void ) { 32 // size_t size = 10; 33 //int * x = malloc(); 34 int * x; 35 // x = malloc(); 36 // x = calloc( 10 ); // calloc: array set to 0 37 // x = realloc( x, 10 ); 38 // x = realloc( x, 10, '\0' ); 39 // x = malloc( 5 ); 40 // float *fp = malloc() + 1; 29 ofstream * sout = ofstream_stdout(); 41 30 42 struct St1 { int x; double y; }; 43 struct St1 * st1; 44 // double *y; 45 x = realloc( st1, 10 ); // SHOULD FAIL!! 31 size_t size = 10; 32 int * p; 33 struct S { int x; double y; } * s; 34 35 p = malloc( sizeof(*p) ); // C malloc, type unsafe 36 printf( "here1\n" ); 37 free( p ); 38 p = malloc(); // CFA malloc, type safe 39 printf( "here2\n" ); 40 free( p ); 41 p = malloc( (char)'\0' ); // CFA malloc, type safe 42 printf( "here3\n" ); 43 p = malloc( p, 1000 ); // CFA remalloc, type safe 44 printf( "here4\n" ); 45 free( p ); 46 p = calloc( size, sizeof(*p) ); // C calloc, type unsafe 47 printf( "here5\n" ); 48 free( p ); 49 p = calloc( size ); // CFA calloc, type safe 50 printf( "here6\n" ); 51 free( p ); 52 p = calloc( size ); // CFA calloc, type safe 53 p = realloc( p, 1000 ); // C realloc, type unsafe 54 p = realloc( p, 1000, '\0' ); // CFA realloc, type unsafe 55 p = memset( p ); // CFA memset, type unsafe 56 printf( "here7\n" ); 57 free( p ); 58 p = memalign( 16 ); 59 printf( "here8\n" ); 60 free( p ); 61 posix_memalign( &p, 16 ); 62 printf( "here9\n" ); 63 free( p ); 46 64 #if 0 65 float * fp = malloc() + 1; 66 fprintf( stderr, "%p %p\n", fp, fp - 1 ); 67 free( fp - 1 ); 68 p = realloc( st1, size, '\0' ); // C realloc, type unsafe 69 70 double *y; 71 x = memset( st1, '\0' ); // SHOULD FAIL!! 72 47 73 int *p; 48 74 p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); … … 74 100 free( x ); 75 101 #endif 102 free( sout ); 76 103 } 104 105 // Local Variables: // 106 // tab-width: 4 // 107 // compile-command: "cfa alloc.c" // 108 // End: // -
src/examples/minmax.c
r4789f44 rbd85400 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Feb 1 11:35:53201613 // Update Count : 4 412 // Last Modified On : Wed Feb 3 11:14:49 2016 13 // Update Count : 46 14 14 // 15 15 16 16 #include <fstream> 17 #include < algorithm>17 #include <stdlib> // min, max 18 18 19 19 int main( void ) { -
src/examples/random.c
r4789f44 rbd85400 4 4 5 5 #include <fstream> 6 #include < algorithm>// random6 #include <stdlib> // random 7 7 8 8 int main() { -
src/examples/sum.c
r4789f44 rbd85400 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Jan 28 17:05:53201613 // Update Count : 13 012 // Last Modified On : Fri Feb 5 16:47:44 2016 13 // Update Count : 139 14 14 // 15 15 … … 33 33 34 34 // Required to satisfy sumable as char does not have addition. 35 const char 0;36 char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion37 char ++?( char *op ) { *op += 1; return *op; }38 char ?++( char *op ) { char temp = *op; *op += 1; return temp; }35 // const char 0; 36 // char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion 37 // char ++?( char *op ) { *op += 1; return *op; } 38 // char ?++( char *op ) { char temp = *op; *op += 1; return temp; } 39 39 40 40 int main( void ) { 41 41 const int low = 5, High = 15, size = High - low; 42 43 42 ofstream *sout = ofstream_stdout(); 43 #if 0 44 44 45 45 char s = 0, a[size]; … … 69 69 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " 70 70 | sum( size, (float *)a ) | ", check " | (float)s | endl; 71 #endif 72 double s = 0, a[size]; 73 double v = low / 10.0; 71 74 72 double s = 0.0, a[size];73 double v = low / 10.0;74 75 for ( int i = 0; i < size; i += 1, v += 0.1 ) { 75 76 s += (double)v; … … 78 79 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " 79 80 | sum( size, (double *)a ) | ", check " | (double)s | endl; 81 82 // struct S { int i, j; } sarr[size]; 83 // struct S 0 = { 0, 0 }; 84 // struct S 1 = { 1, 1 }; 85 // S ?+?( S t1, S t2 ) { S s = { t1.i + t1.j, t2.i + t2.j }; return s; } 86 // S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; } 87 // S ++?( S *t ) { *t += 1; return *t; } 88 // S ?++( S *t ) { S temp = *t; *t += 1; return temp; } 89 // sum( size, sarr ); 80 90 } // main 81 91 -
src/examples/swap.c
r4789f44 rbd85400 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Feb 1 12:30:15201613 // Update Count : 6 112 // Last Modified On : Wed Feb 3 11:14:04 2016 13 // Update Count : 63 14 14 // 15 15 16 16 #include <fstream> 17 #include < algorithm>17 #include <stdlib> // swap 18 18 19 19 int main( void ) {
Note:
See TracChangeset
for help on using the changeset viewer.