source: src/examples/alloc.c @ d67a9a1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d67a9a1 was 784deab, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

fix recursive include bug in shadow includes, major clean of examples, add several long long routines to prelude

  • Property mode set to 100644
File size: 1.6 KB
Line 
1extern "C" {
2    typedef long unsigned int size_t;
3    void *malloc( size_t size );
4    void *calloc( size_t nmemb, size_t size );
5    void *realloc( void *ptr, size_t size );
6    void *memset( void *s, int c, size_t n );
7    void free( void * ptr );
8    int printf( const char *, ... );
9}
10
11forall( type T ) T * malloc( void ) {
12    return (T *)malloc( sizeof(T) );
13}
14forall( type T ) T * calloc( size_t size ) {
15    return (T *)calloc( size, sizeof(T) );
16}
17forall( type T ) T * realloc( T *ptr, size_t n ) {
18    return (T *)(void *)realloc( ptr, sizeof(T) );
19}
20forall( type T ) T * realloc( T *ptr, size_t n, T c ) {
21    return (T *)realloc( ptr, n );
22}
23
24int *foo( int *p, int c );
25int *bar( int *p, int c );
26int *baz( int *p, int c );
27
28int main( void ) {
29    size_t size = 10;
30    int * x = malloc();
31    x = malloc();
32    x = calloc( 10 );                                   // calloc: array set to 0
33    x = realloc( x, 10 );
34    x = realloc( x, 10, '\0' );
35    x = malloc( 5 );
36    float *fp = malloc() + 1;
37
38    struct St1 { int x; double y; };
39    struct St1 * st1;
40    double *y;
41    x = realloc( st1, 10 );                             // SHOULD FAIL!!
42#if 0
43    int *p;
44    p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
45    free( p );
46
47    struct St2 { int x; double y; };
48    struct St2 * st2;
49
50    y = malloc();
51    st1 = malloc();
52//    st1 = realloc( st2, 10, st1 );
53 
54    *y = 1.0;
55    printf("%f\n", *y);
56
57    st1->x = *x + 1;
58    st1->y = *y *1.5;
59    printf("{ %d, %f }\n", st1->x, st1->y);
60
61    free( y );
62 
63    x = malloc( 10 );
64    for ( int i = 0; i < 10; i += 1 ) {
65        x[i] = i * 10;
66    }
67    for ( int j = 0; j < 10; j += 1 ) {
68        printf( "x[%d] = %d\n", j, x[j] );
69    }
70    free( x );
71#endif
72}
Note: See TracBrowser for help on using the repository browser.