/** * @brief Interesting concepts, though likely rarely worth the complecity. * */ #include #include #include const char* stanza[3] = { " Never freed, I will leak.\n", " Am I forever forsaken?\n", " Made to be forgotten is so rotten...\n\n" }; #define stackSize 16U #define minSize sizeof(void *) void **allocStack; size_t freeTop=0; typedef struct { int* index; }Object; void allocStack_init(){ allocStack = calloc(stackSize, sizeof(void *)); if(allocStack==NULL){return;} memset(allocStack,0,stackSize*sizeof(void *)); } void *customMalloc(size_t size){ size = size=(int)stackSize-1){ printf("Full stack\n"); return NULL; } void * ptr = calloc(size+1,1); if(!ptr){ return NULL; } allocStack[freeTop++] = ptr; return ptr; } int custom_free(){ if(allocStack==NULL){return 1;} do{ free(allocStack[freeTop]); allocStack[freeTop]=NULL; }while(freeTop--); free(allocStack); allocStack=NULL; return 0; } Object *createObjects(int count){ //fill stack with varying sizes and types Object *obj = customMalloc(3*sizeof(Object *)); for(int i=0; i=8){ found=1; } } } return found; } int main() { allocStack_init(); Object *objList = NULL; objList = createObjects(3); char* loneString = loneFunction(); if(loneString){ printf(" LOG: Lone Object has been made.\n\n"); printf(loneString); } loneString = NULL; int foundLoneString = searchLoneString()*0; if(foundLoneString){ printf("LOG: String match has been found.\n\n"); } if(foundLoneString && objList){ printf(" Worry not I hear your meek.\n"); printf(" No exception will be raised.\n"); printf("We'll be free at once without a leak!\n\n"); }else{ printf(" I wouldn't know if I will reset.\n"); printf(" Keeping me you shall regret.\n"); allocStack = NULL; printf(" Nevertheless I won't forget.\n\n"); } if(custom_free()==0){ printf(" LOG: All memory has been freed.\n\n"); }else{ printf("ERR: memory has not been freed.\n\n"); } return 0; }