How to deallocate 3D arrays (array of pointers to arrays)



  • I allocated memory within an DLL using malloc:

    I = (float **)malloc(zmax * sizeof(float *)); 
    
    	for( i = 0; i <= zmax; ++i) 
    	{
         	I[i] = (float *)malloc(xmax*ymax * sizeof(float));
    	}
    

    I deallocated using free:

    for( i = 1; i <= zmax; ++i) 
    	{
         	free(I[i]);
    	}
    	free(I);
    	free(*I);
    

    When I call this DLL from Labview multiple times the program is running out of memory. I want to make sure that this behavior is not due to wrong deallocation inside the DLL.
    Can anyone help me?
    Thanks in advance.



  • You allocate using this loop with i set to 0:

    for( i = 0; i <= zmax; ++i) 
    {
    	I[i] = (float *)malloc(xmax*ymax * sizeof(float));
    }
    

    But you deallocate with i set to 1:

    for( i = 1; i <= zmax; ++i) 
    {
    	free(I[i]);
    }
    

    This looks wrong to me. I guess this is the fault. Just set i to 0 and check the result.

    Besides, why do you note an additional free?

    free(*I);
    

    I think this is not necessary.



  • I = (float **)malloc(zmax * sizeof(float *));

    this is not three d


Anmelden zum Antworten