source: src/examples/alloc.c@ 98735ef

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 98735ef was d3b7937, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

building runtime library (first attempt)

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[d3b7937]1#if 0
[097e2b0]2extern "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}
11
12forall( type T ) T * malloc( void ) {
13 return (T *)malloc( sizeof(T) );
14}
15forall( type T ) T * calloc( size_t size ) {
16 return (T *)calloc( size, sizeof(T) );
17}
[d3b7937]18#endif
19typedef int size_t;
[097e2b0]20forall( type T ) T * realloc( T *ptr, size_t n ) {
[d3b7937]21// return (T *)(void *)realloc( ptr, sizeof(T) );
[097e2b0]22}
[d3b7937]23//forall( type T ) T * realloc( T *ptr, size_t n, T c ) {
24// return (T *)realloc( ptr, n );
25//}
[097e2b0]26
[d3b7937]27//int *foo( int *p, int c );
28//int *bar( int *p, int c );
29//int *baz( int *p, int c );
[097e2b0]30
[784deab]31int main( void ) {
[d3b7937]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;
[097e2b0]41
42 struct St1 { int x; double y; };
43 struct St1 * st1;
[d3b7937]44// double *y;
[097e2b0]45 x = realloc( st1, 10 ); // SHOULD FAIL!!
46#if 0
47 int *p;
48 p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
49 free( p );
50
51 struct St2 { int x; double y; };
52 struct St2 * st2;
53
54 y = malloc();
55 st1 = malloc();
56// st1 = realloc( st2, 10, st1 );
57
58 *y = 1.0;
59 printf("%f\n", *y);
60
61 st1->x = *x + 1;
62 st1->y = *y *1.5;
63 printf("{ %d, %f }\n", st1->x, st1->y);
64
65 free( y );
66
67 x = malloc( 10 );
68 for ( int i = 0; i < 10; i += 1 ) {
69 x[i] = i * 10;
70 }
71 for ( int j = 0; j < 10; j += 1 ) {
72 printf( "x[%d] = %d\n", j, x[j] );
73 }
74 free( x );
75#endif
76}
Note: See TracBrowser for help on using the repository browser.