0

I've a 2-d array declared as such

lower_tri = (float **)malloc(sizeof(float *)*(dimention));
for(i =0; i < dimention; ++i)
	lower_tri[i] = (float *)malloc(sizeof(float)*(i+1));

So that it looks like...
[]
[][]
[][][]
[][][][]
...

However, it causes a segmentation fault when I try to de-allocate the array with

free(lower_tri);

(only in unix, Windows works fine)

I've tried using a for loop to deallocate the array row by row, but just come up with more errors.

Any idea how to deallocate this array?

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

the array has to be deallocated in the reverse order that it was allocated

for(i =0; i < dimention; ++i)
	free(lower_tri[i]);
free(lower_tri);

If you get seg fault with that then it means the program has been corrupted somewhere.

0

I'm sure it has. You're having an out-of-bounds array access somewhere.
Have you run this with valgrind yet? It can discover invalid memory accesses in some cases.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.