How to free multidimensional arrays

Jan 21, 2010 at 5:44pm
Hi there,

I have created a 3D array A representing 100 4x3 matrices of doubles:
1
2
3
4
5
6
7
8
9
10
        ui xdim = 100;
	ui ydim = 4;
	ui zdim = 3;

	double ***A = (double ***)malloc(xdim * sizeof(double **));
	for(ui i = 0; i < xdim; i++) {
		A[i] = (double **)malloc(ydim * sizeof(double *));
		for(ui j = 0; j < ydim; j++)
			A[i][j] = (double *)malloc(zdim * sizeof(double));
	}


Somehow I can't figure out how to free it up. I tried this code:
1
2
3
4
5
6
	for(ui i = 0; i < xdim; i++){
		for(ui j = 0; j < ydim; j++)
			free((void **)A[i][j]);
		free((void *)A[i]);
	}
	free((void *)A);


But get this error message while running:
Windows has triggered a breakpoint.
This may be due to a corruption of the heap....


Any idea of what could go wrong with the code to free the memory?

Thanks

Jan 21, 2010 at 5:50pm
Line 3 should read:
 
			free((void *)A[i][j]);

Let me know if that doesn't help.
Jan 21, 2010 at 5:57pm
Thanks Duoas,

Just tried it and got the same error message. This line triggers the error obviously.
Jan 21, 2010 at 6:23pm
All right!

Duoas, what you suggested is correct; it works fine. The error was triggered by me using a commercial library (Optivec) which requires some special data alignment to function.
Topic archived. No new replies allowed.