c - Free function not working -


i have following c code.

#include <stdio.h> #include <stdlib.h>  int main() {     while (1) {         int *test = malloc(sizeof(*test));         test = 500;         free(test);     }     return 0; } 

the free function not seem work allocated memory grows 2gb within few seconds. problem?

you can free pointer return of malloc.

in writing test = 500, you've changed memory location pointed test. trying free undefined behaviour.

to assign value allocated integer, derefence it: *test = 500;


Comments

Popular posts from this blog

javascript - How to name a jQuery function to make a browser's back button work? -

python - Django-easy-pdf: xhtml2pdf reporting reportlab 2.2+ is required, but 3.0 installed -

collision detection - C++ library for mesh to mesh intersection: what is available? -