Comment

Comment by josephcsible

Consider this C code:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void) {
        size_t sz = 0;
        void *unused = NULL;
        for(;;) {
            int n = 0;
            void *tmp = realloc(unused, ++sz);
            if(!tmp) break;
            unused = tmp;
            puts("Enter a number and I'll double it, or enter 0 to end:");
            scanf("%d", &n);
            if(!n) break;
            printf("%d\n", 2*n);
        }
        free(unused);
    }
Do you think it contains a memory leak? If so, note that Valgrind disagrees: "All heap blocks were freed -- no leaks are possible". If not, then can you give a short example of some JavaScript code that does contain a memory leak?

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton