[MINC-users] dynamic memory allocation for hyperslabs

Thomas Funck thomas.funck at mail.mcgill.ca
Wed Apr 27 15:59:28 EDT 2011


Hi,

I'm trying to dynamically allocate memory for a hyperslab, but this always seems to lead to a seg fault.  I used hyper-test.c to try and figure out the problem. The normal hyper-test.c compiles and runs fine, so I changed "dtemp[CX][CY][CZ];" to "double ***dtemp;" and then allocate the necessary memory by doing:

	dtemp=malloc(CX * sizeof(double**));
	for(i=0; i< CX; i++)
	{
	dtemp[i]=malloc(CY * sizeof(double*));
		for(j=0; j < CY; j++)
		{
		dtemp[i][j]=malloc(CZ * sizeof(double) );
		}
	}

However this resulted in a seg fault on the line:

if (fabs(r - dtemp[i][j][k]) > 1.0e-15)

So even though "miget_real_value_hyperslab" returns 0 and says it gets the hyperslab correctly, there seems to be a problem dereferencing "dtemp" when the memory has been dynamically allocated.

Is there something wrong with my C? Or does "miget_real_value_hyperslab" not work correctly for dynamically allocated memory?

Many thanks for any help!

Best,

Thomas


More information about the MINC-users mailing list