Quick actions

cmd+k|ctrl+k

Navigation

Languages

Allocate stuff.

Snippet info

Language

C

Visibility

public

Author

evine

Created

2016-03-03T19:52:07Z

Updated

2016-03-04T11:40:49Z

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define Allocate(VariableOrType, Size) \
(typeof(VariableOrType) *)malloc( sizeof(VariableOrType) * (Size))

#define AllocateZero(Pointer, Size) \
bzero(Pointer, (Size) * sizeof(*Pointer))

#define AllocateFree(Pointer) \
free(Pointer)

typedef struct{
    int size;
    short *array;
}LengthArray;


int main() {
    
    LengthArray foo = {20000, Allocate(*foo.array, foo.size) };
    foo.array[foo.size-1] = 120;
    AllocateZero(foo.array, foo.size);
    
    //printf("Last value in Array: %hd\n", foo.array[foo.size-1] );
    printf("sizeof(dereference) = %lu\n", sizeof( *foo.array ));
    printf("sizeof(type) = %lu\n", sizeof( short ));
    printf("sizeof(typeof(dereference)) = %lu\n", sizeof( typeof(*foo.array) ));
    printf("sizeof(typeof(type)) = %lu\n", sizeof( typeof(short) ));
    printf("sizeof(typeof(dereference) *) = %lu\n", sizeof( typeof(*foo.array) *));
    printf("sizeof(typeof(type) *) = %lu\n", sizeof( typeof(short) *));
    printf("These last two are important for casting, and they aren't possible with decltype().");
    
    AllocateFree(foo.array);
    return 0;
}
INFO