PointerList

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <stdlib.h> #include <assert.h> #define PointerListCreate(Name, ListSize) \ static size_t Name##Size = ListSize; \ static void *Name##List[ListSize] = {}; \ static size_t Name##Stack = 0; \ static void *Name##Allocate(size_t TypeSize, size_t Size) \ { \ void *Result; \ assert(Name## Stack < Name##Size && "PointerList is to small"); \ Result = calloc(Size, sizeof(TypeSize)); \ assert(Result && "calloc allocation has failed"); \ Name##List[Name##Stack++] = Result; \ return Result; \ } \ static void Name##Free() \ { \ for(size_t i = 0; i < Name##Stack; i++) \ { \ free(Name##List[i]); \ } \ } PointerListCreate(MainSystem, 64*1024) typedef struct{ size_t size; int *item; }apple; apple apple_init(int size) { apple result; result.size = size; result.item = MainSystemAllocate(sizeof(result.item[0]), size); return result; } int main() { apple har[1024]; for(int i = 0; i < 1024; i++) { har[i] = apple_init(20); } har[739].item[13] = 5563; printf("%i\n", har[739].item[13]); MainSystemFree(); // Free the system, No more manual pointer handling. return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines