1 | #if 0
|
---|
2 | 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 | }
|
---|
11 |
|
---|
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 );
|
---|
30 |
|
---|
31 | 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;
|
---|
41 |
|
---|
42 | struct St1 { int x; double y; };
|
---|
43 | struct St1 * st1;
|
---|
44 | // double *y;
|
---|
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 | }
|
---|