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

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

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