nvmd
nvmd

Reputation: 51

Is my program leaking memory?

I'm learning C and to to get a better grasp of some topics in a fun way, I wrote something that does successive allocations/deallocations. Here's the code

#include <stdlib.h>
#include <stdint.h>

int main( void )
{
    uint16_t i, j, k, l, m, n = 10, z = 25;
    int64_t ******v;

    while ( n-- )
    {
        v = malloc( sizeof( int64_t ***** ) * z );

        for ( i = 0; i < z; ++i )
        {
            v[ i ] = malloc( sizeof( int64_t **** ) * z );

            for ( j = 0; j < z; ++j )
            {
                v[ i ][ j ] = malloc( sizeof( int64_t *** ) * z );

                for ( k = 0; k < z; ++k )
                {
                    v[ i ][ j ][ k ] = malloc( sizeof( int64_t ** ) * z );

                    for ( l = 0; l < z; ++l )
                    {
                        v[ i ][ j ][ k ][ l ] = malloc( sizeof( int64_t * ) * z );

                        for ( m = 0; m < z; ++m )
                            v[ i ][ j ][ k ][ l ][ m ] = malloc( sizeof( int64_t ) * z );
                    }
                }
            }
        }

        for ( i = 0; i < z; ++i )
        {
            for ( j = 0; j < z; ++j )
            {
                for ( k = 0; k < z; ++k )
                {
                    for ( l = 0; l < z; ++l )
                    {
                        for ( m = 0; m < z; ++m )
                            free( v[ i ][ j ][ k ][ l ][ m ] );

                        free( v[ i ][ j ][ k ][ l ] );
                    }
                    free( v[ i ][ j ][ k ] );
                }
                free( v[ i ][ j ] );
            }
            free( v[ i ] );
        }
        free( v );
    }

    return 0;
}

I'm on Windows x64, so pointer size is 8 bytes. So if my math is right, the program should allocate ~2.03GB of RAM.
But in reality, it allocates ~2.7GB.
Also, the memory consumption increases by ~2MB after each iteration of the while loop.
And lastly, after the 8th iteration of the while loop, after all free()s, taskmgr still shows the program holding ~1.4GB of RAM, even though next allocations still top at ~2.7GB normally.

The questions are: is my math wrong? What about the additional ~2MB at each iteration? And the unfree()d ~1.4GB of RAM? What's happening?

Upvotes: 0

Views: 117

Answers (2)

zzxyz
zzxyz

Reputation: 2981

Answer: no. I ran your code under linux (with n set to 2 so it wouldn't take forever, admittedly):

£ valgrind --leak-check=full --track-origins=yes ./CMakeHelloWorld
==4213== Memcheck, a memory error detector
==4213== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4213== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4213== Command: ./CMakeHelloWorld
==4213==
==4213==
==4213== HEAP SUMMARY:
==4213==     in use at exit: 0 bytes in 0 blocks
==4213==   total heap usage: 10,172,526 allocs, 10,172,526 frees, 2,034,505,200 bytes allocated
==4213==
==4213== All heap blocks were freed -- no leaks are possible
==4213==
==4213== For counts of detected and suppressed errors, rerun with: -v
==4213== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

What you are doing should never ever happen btw. Matrixes are typically allocated in a block int *pMap = (malloc(x*y*sizeof(int)) and for anything else there are better tools for managing references. Anything past ** is almost certainly complete foolishness.

Upvotes: 2

Sean F
Sean F

Reputation: 4615

No it is not leaking memory.

When you request x bytes with malloc, more than x bytes are used up, in part to create space for memory headers, as well as any other bookkeeping. See this answer for more details Malloc header contents

Upvotes: 4

Related Questions