In the course of the laboratory, I came across a task where you just had to write a program to count the product of matrices, while allocating memory dynamically. Sounds pretty simple, but in the end I found it.

Create definitions of macros that calculate how many times the program has been using dynamic allocation and freeing functions, as well as the amount of allocated and freed memory.

In the manual, nothing is written except for explanations about #define, #if, etc. I would like to have at least some idea of ​​what it is about)

    1 answer 1

    It looks like they want something like

    int total = 0; int mallocs = 0; int frees = 0; #define malloc(s) (mallocs++, total += (s), malloc((s))) #define free(s) (frees++, free((s))) int main(int argc, const char * argv[]) { char * c = malloc(200); char * v = malloc(2000); free(c); printf("Alloc %d bytes in %d mallocs; frees: %d times\n", total, mallocs,frees); } 
    • Yes, it seems to me exactly that, I understood everything. Thank you very much) - dhvcc