[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 |
---|
| 13 | // Last Modified On : Wed Feb 3 16:32:04 2016 |
---|
| 14 | // Update Count : 38 |
---|
| 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 | ofstream * sout = ofstream_stdout(); |
---|
[097e2b0] | 30 | |
---|
[bd85400] | 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 ); |
---|
[097e2b0] | 64 | #if 0 |
---|
[bd85400] | 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 | |
---|
[097e2b0] | 73 | int *p; |
---|
| 74 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
| 75 | free( p ); |
---|
| 76 | |
---|
| 77 | struct St2 { int x; double y; }; |
---|
| 78 | struct St2 * st2; |
---|
| 79 | |
---|
| 80 | y = malloc(); |
---|
| 81 | st1 = malloc(); |
---|
| 82 | // st1 = realloc( st2, 10, st1 ); |
---|
| 83 | |
---|
| 84 | *y = 1.0; |
---|
| 85 | printf("%f\n", *y); |
---|
| 86 | |
---|
| 87 | st1->x = *x + 1; |
---|
| 88 | st1->y = *y *1.5; |
---|
| 89 | printf("{ %d, %f }\n", st1->x, st1->y); |
---|
| 90 | |
---|
| 91 | free( y ); |
---|
| 92 | |
---|
| 93 | x = malloc( 10 ); |
---|
| 94 | for ( int i = 0; i < 10; i += 1 ) { |
---|
| 95 | x[i] = i * 10; |
---|
| 96 | } |
---|
| 97 | for ( int j = 0; j < 10; j += 1 ) { |
---|
| 98 | printf( "x[%d] = %d\n", j, x[j] ); |
---|
| 99 | } |
---|
| 100 | free( x ); |
---|
| 101 | #endif |
---|
[bd85400] | 102 | free( sout ); |
---|
[097e2b0] | 103 | } |
---|
[bd85400] | 104 | |
---|
| 105 | // Local Variables: // |
---|
| 106 | // tab-width: 4 // |
---|
| 107 | // compile-command: "cfa alloc.c" // |
---|
| 108 | // End: // |
---|