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?

Recommended Answers

All 3 Replies

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.

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.

Why did you create two threads for the same problem??? See your other thread for solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.