Advertisement
Kaelygon

memory_DEBUG.c

Dec 20th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. /**
  2.  * @brief Interesting concepts, though likely rarely worth the complecity.
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.    
  10.    const char* stanza[3] = {
  11.    "    Never freed, I will leak.\n",
  12.    "     Am I forever forsaken?\n",
  13.    " Made to be forgotten is so rotten...\n\n"
  14.    };
  15.  
  16. #define stackSize 16U
  17. #define minSize sizeof(void *)
  18. void **allocStack;
  19. size_t freeTop=0;
  20.  
  21. typedef struct {
  22.    int* index;
  23. }Object;
  24. void allocStack_init(){
  25.    allocStack = calloc(stackSize, sizeof(void *));
  26.    if(allocStack==NULL){return;}
  27.    memset(allocStack,0,stackSize*sizeof(void *));
  28. }
  29.  
  30. void *customMalloc(size_t size){
  31.    size = size<minSize ? minSize : size ; //minimum size that few first byte can be safely compared
  32.    if(freeTop>=(int)stackSize-1){
  33.       printf("Full stack\n");
  34.       return NULL;
  35.    }
  36.    void * ptr = calloc(size+1,1);
  37.    if(!ptr){
  38.       return NULL;
  39.    }
  40.    allocStack[freeTop++] = ptr;
  41.    return ptr;
  42. }
  43.  
  44. int custom_free(){
  45.    if(allocStack==NULL){return 1;}
  46.    do{
  47.       free(allocStack[freeTop]);
  48.       allocStack[freeTop]=NULL;
  49.    }while(freeTop--);
  50.    free(allocStack);
  51.    allocStack=NULL;
  52.    return 0;
  53. }
  54.  
  55. Object *createObjects(int count){ //fill stack with varying sizes and types
  56.    Object *obj = customMalloc(3*sizeof(Object *));
  57.    for(int i=0; i<count; i++){
  58.       obj[i].index = customMalloc(sizeof(int));
  59.       if(obj[i].index==NULL){
  60.          printf("Error\n");
  61.          return NULL;
  62.       }
  63.       *obj[i].index=i;
  64.    }
  65.    return obj;
  66. }
  67.  
  68. //addresses only stored in allocstack
  69. char *loneFunction(){
  70.    char* loneString = customMalloc(3*sizeof(char *));
  71.  
  72.    loneString = customMalloc( ( 3 + strlen(stanza[0]) + strlen(stanza[1]) + strlen(stanza[2]) ) * sizeof(char)  );
  73.    if(!loneString){
  74.       return NULL;
  75.    }
  76.    sprintf(loneString, "%s%s%s",stanza[0],stanza[1],stanza[2]);
  77.    return loneString;
  78. }
  79.  
  80. int searchLoneString(){
  81.    int found=0;
  82.  
  83.    for(size_t i=1; i<freeTop; i++){
  84.       if(allocStack[i]!=NULL){
  85.          const char *compare = (char *)allocStack[i];
  86.          const char *verse = stanza[0];
  87.          size_t pos=0;
  88.          while(pos<minSize && verse[pos]){
  89.                if( compare[pos] != verse[pos] ){
  90.                   break;
  91.                }
  92.                pos++;
  93.          }
  94.          if(pos>=8){
  95.             found=1;
  96.          }
  97.       }
  98.    }
  99.    return found;
  100. }
  101.  
  102. int main() {
  103.    allocStack_init();
  104.  
  105.    Object *objList = NULL;
  106.    objList = createObjects(3);
  107.  
  108.    char* loneString = loneFunction();
  109.    if(loneString){
  110.       printf(" LOG: Lone Object has been made.\n\n");
  111.       printf(loneString);
  112.    }
  113.  
  114.    loneString = NULL;
  115.  
  116.    int foundLoneString = searchLoneString()*0;
  117.  
  118.    if(foundLoneString){
  119.       printf("LOG: String match has been found.\n\n");
  120.    }
  121.  
  122.    if(foundLoneString && objList){
  123.       printf("   Worry not I hear your meek.\n");
  124.       printf("   No exception will be raised.\n");
  125.       printf("We'll be free at once without a leak!\n\n");
  126.    }else{
  127.       printf(" I wouldn't know if I will reset.\n");
  128.       printf("  Keeping me you shall regret.\n"); allocStack = NULL;
  129.       printf("  Nevertheless I won't forget.\n\n");
  130.    }
  131.  
  132.    if(custom_free()==0){
  133.       printf(" LOG: All memory has been freed.\n\n");
  134.    }else{
  135.       printf("ERR: memory has not been freed.\n\n");
  136.    }
  137.  
  138.    return 0;
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement