Leaky Code…

I had heard lots and lots about leaky code so far, but never actually learned about it or read about it until tonight… so here it is below: a sample of LEAKY CODE (source: Wikipedia)

#include <stdlib.h>
 
void function_which_allocates(void) {
    /* allocate an array of 45 floats */
    float * a = malloc(sizeof(float) * 45);
 
    /* additional code making use of 'a' */
 
    /* return to main, having forgotten to free the memory we malloc'd */
}
 
int main(void) {
    function_which_allocates();
 
    /* the pointer 'a' no longer exists, and therefore cannot be freed,
     but the memory is still allocated. a leak has occurred. */
}

via Wikipedia Memory Leak Article

Published by

Zaid Pirwani

Co-Founder and Design Engineer at EjaadTech + MS Student (Applied Power and Control) + Graduate Teaching Fellow at PAF KIET + Teaching C/C++ at PAF KIET + Industrial Electronics Engineer from IIEE-PCSIR + all round Computer/Electronics Geek

comment bhi kar hi dain.. :P