2

I've got a problem. I am trying to free an array of pointers using a loop, but it does not work. Could somebody help me with that?

This is the allocation code:

void create1(A*** a, int size)
{
    *a = (A**) malloc(size* sizeof(A*));
    for (int i = 0; i < size; i++)
    {
        a[0][i] = (A*)malloc(size * sizeof(A));
    }
}
4
  • A pointer to a pointer is not a two-dimensional array. Additionally, I do not see any attempt to free anything (you perform allocations at line 3 and 6). Commented Oct 14, 2018 at 18:07
  • Had you had a look at this answer to your previous question? Commented Oct 14, 2018 at 18:07
  • ok, now I see answer to my last question. Thank you for helping me Commented Oct 14, 2018 at 18:10
  • @user76234 If the question is superfluous or obsolete, then please delete it. Commented Oct 14, 2018 at 18:12

1 Answer 1

5

You have to do the reverse of what you have done when you have allocated the memory.

Free the element pointers in a loop and finally free the pointer to the array:

 void del(A** a, int size)
 {
    for (int i = 0; i < size; i++)
    {
        free(a[i]);
    }
    free(a);
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.