Changeset d41280e for src/examples/alloc.c
- Timestamp:
- Feb 8, 2016, 10:07:42 AM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- c44e622
- Parents:
- 00ede9e (diff), bd85400 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/alloc.c
r00ede9e rd41280e 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 // 16 17 #include <fstream> 18 #include <stdlib> 1 19 extern "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 } 20 #include <stdlib.h> // access C malloc, realloc 21 #include <stdio.h> 22 } // exten "C" 10 23 11 forall( type T ) T * malloc( void ) { 12 return (T *)malloc( sizeof(T) ); 13 } 14 forall( type T ) T * calloc( size_t size ) { 15 return (T *)calloc( size, sizeof(T) ); 16 } 17 forall( type T ) T * realloc( T *ptr, size_t n ) { 18 return (T *)(void *)realloc( ptr, sizeof(T) ); 19 } 20 forall( type T ) T * realloc( T *ptr, size_t n, T c ) { 21 return (T *)realloc( ptr, n ); 22 } 23 24 int *foo( int *p, int c ); 25 int *bar( int *p, int c ); 26 int *baz( int *p, int c ); 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; } 27 27 28 28 int main( void ) { 29 ofstream * sout = ofstream_stdout(); 30 29 31 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; 32 int * p; 33 struct S { int x; double y; } * s; 37 34 38 struct St1 { int x; double y; }; 39 struct St1 * st1; 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 ); 64 #if 0 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 40 70 double *y; 41 x = realloc( st1, 10 );// SHOULD FAIL!!42 #if 0 71 x = memset( st1, '\0' ); // SHOULD FAIL!! 72 43 73 int *p; 44 74 p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); … … 70 100 free( x ); 71 101 #endif 102 free( sout ); 72 103 } 104 105 // Local Variables: // 106 // tab-width: 4 // 107 // compile-command: "cfa alloc.c" // 108 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.