0

I have a few questions about a piece of code that I have found on web, which is located at http://www.c.happycodings.com/Data_Structures/code9.html.

  1. Why is strarray defined as **?
  2. Do we have to first malloc() the array, and then malloc() each element of it?

    strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));

    strarray[count] = (struct node *)malloc(sizeof(struct node));

  3. How to free() this array totally?

Thanks

unwind
  • 391,730
  • 64
  • 469
  • 606
groove
  • 273
  • 2
  • 7
  • 17

2 Answers2

2
  1. The strarray is a pointer to an array of pointers with each pointer pointing to a node struct. This is the basic representation of an array of objects. For basic types you can use only one *, because it's a pointer to an array of basic types. You can do that for structs too, it just depends on what you want to do with the array.

  2. Yes, yes you do.

  3. First iterate over the array, freeing every member, then free the array. The clue to freeing: free everything you have allocated.

Minion91
  • 1,911
  • 12
  • 19
1
  1. strarray is of type struct node ** because it's a dynamically allocated array of struct node * - ie. an array of pointers, where each element points to a struct node.

  2. No, depending on what you're trying to achieve you could simply allocate a block of memory to hold X struct node, and assign that pointer to a struct node *. The person who wrote that code allocated memory for an array of pointers, which is why they then made a call to malloc to allocate memory for each individual struct node.

    A possibility for them using a dynamic array of pointers to dynamically allocated struct node, as opposed to simply allocating a single block of contiguous struct node could have to do with lessening the cost of (if required) copying the whole array when calling realloc.

  3. To free the memory, you do things in reverse. Free each element in the array, and then free the entire array.

Community
  • 1
  • 1
AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • 2) So, can we define `struct node *starray` and allocate memory `strarray = (struct node *)realloc(strarray, 2 * sizeof(struct node ));`, and modify the new 2 elements immediately without any more 'inner' mallocs? And if yes, is there a confliction with the @Minion91's answer (because struct is not a basic type)? – groove Sep 13 '12 at 08:04
  • 1
    Oh but you can make an array like that from structs, but it depends on what you want to do with it. – Minion91 Sep 13 '12 at 08:24
  • @Minion91 So I think, in this case, I just have to use `free(strarray[i].str)` in a loop and `free(strarray)`, because elements are not pointers. But in the original code, I have to use also `free(strarray[i])` in the loop as an addition to the other two `free()`s. – groove Sep 13 '12 at 08:35
  • 1
    @groove: Yes you can allocate and use a dynamically allocated array of 2 `struct node` like that, which is what you'll typically see. If you're not actually reallocating memory though then you could just stick to a `malloc` call. – AusCBloke Sep 13 '12 at 09:57