[bd85400] | 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 |
---|
[6ba0659] | 13 | // Last Modified On : Wed Feb 17 11:43:23 2016 |
---|
| 14 | // Update Count : 40 |
---|
[bd85400] | 15 | // |
---|
[097e2b0] | 16 | |
---|
[bd85400] | 17 | #include <fstream> |
---|
| 18 | #include <stdlib> |
---|
| 19 | extern "C" { |
---|
| 20 | #include <stdlib.h> // access C malloc, realloc |
---|
| 21 | #include <stdio.h> |
---|
| 22 | } // exten "C" |
---|
[097e2b0] | 23 | |
---|
[bd85400] | 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; } |
---|
[097e2b0] | 27 | |
---|
[784deab] | 28 | int main( void ) { |
---|
[bd85400] | 29 | size_t size = 10; |
---|
| 30 | int * p; |
---|
| 31 | struct S { int x; double y; } * s; |
---|
| 32 | |
---|
| 33 | p = malloc( sizeof(*p) ); // C malloc, type unsafe |
---|
| 34 | printf( "here1\n" ); |
---|
| 35 | free( p ); |
---|
| 36 | p = malloc(); // CFA malloc, type safe |
---|
| 37 | printf( "here2\n" ); |
---|
| 38 | free( p ); |
---|
| 39 | p = malloc( (char)'\0' ); // CFA malloc, type safe |
---|
| 40 | printf( "here3\n" ); |
---|
| 41 | p = malloc( p, 1000 ); // CFA remalloc, type safe |
---|
| 42 | printf( "here4\n" ); |
---|
| 43 | free( p ); |
---|
| 44 | p = calloc( size, sizeof(*p) ); // C calloc, type unsafe |
---|
| 45 | printf( "here5\n" ); |
---|
| 46 | free( p ); |
---|
| 47 | p = calloc( size ); // CFA calloc, type safe |
---|
| 48 | printf( "here6\n" ); |
---|
| 49 | free( p ); |
---|
| 50 | p = calloc( size ); // CFA calloc, type safe |
---|
| 51 | p = realloc( p, 1000 ); // C realloc, type unsafe |
---|
| 52 | p = realloc( p, 1000, '\0' ); // CFA realloc, type unsafe |
---|
| 53 | p = memset( p ); // CFA memset, type unsafe |
---|
| 54 | printf( "here7\n" ); |
---|
| 55 | free( p ); |
---|
| 56 | p = memalign( 16 ); |
---|
| 57 | printf( "here8\n" ); |
---|
| 58 | free( p ); |
---|
| 59 | posix_memalign( &p, 16 ); |
---|
| 60 | printf( "here9\n" ); |
---|
| 61 | free( p ); |
---|
[097e2b0] | 62 | #if 0 |
---|
[bd85400] | 63 | float * fp = malloc() + 1; |
---|
| 64 | fprintf( stderr, "%p %p\n", fp, fp - 1 ); |
---|
| 65 | free( fp - 1 ); |
---|
| 66 | p = realloc( st1, size, '\0' ); // C realloc, type unsafe |
---|
| 67 | |
---|
| 68 | double *y; |
---|
| 69 | x = memset( st1, '\0' ); // SHOULD FAIL!! |
---|
| 70 | |
---|
[097e2b0] | 71 | int *p; |
---|
| 72 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
| 73 | free( p ); |
---|
| 74 | |
---|
| 75 | struct St2 { int x; double y; }; |
---|
| 76 | struct St2 * st2; |
---|
| 77 | |
---|
| 78 | y = malloc(); |
---|
| 79 | st1 = malloc(); |
---|
| 80 | // st1 = realloc( st2, 10, st1 ); |
---|
| 81 | |
---|
| 82 | *y = 1.0; |
---|
| 83 | printf("%f\n", *y); |
---|
| 84 | |
---|
| 85 | st1->x = *x + 1; |
---|
| 86 | st1->y = *y *1.5; |
---|
| 87 | printf("{ %d, %f }\n", st1->x, st1->y); |
---|
| 88 | |
---|
| 89 | free( y ); |
---|
| 90 | |
---|
| 91 | x = malloc( 10 ); |
---|
| 92 | for ( int i = 0; i < 10; i += 1 ) { |
---|
| 93 | x[i] = i * 10; |
---|
| 94 | } |
---|
| 95 | for ( int j = 0; j < 10; j += 1 ) { |
---|
| 96 | printf( "x[%d] = %d\n", j, x[j] ); |
---|
| 97 | } |
---|
| 98 | free( x ); |
---|
| 99 | #endif |
---|
| 100 | } |
---|
[bd85400] | 101 | |
---|
| 102 | // Local Variables: // |
---|
| 103 | // tab-width: 4 // |
---|
| 104 | // compile-command: "cfa alloc.c" // |
---|
| 105 | // End: // |
---|