0

I've got a little bug with this function:

char **addtotab(char **tab, char *newline) {
  int u;
  char **new;

  u = 0;
  while (tab[u])
    u++;

  if ((new = (char **)malloc(sizeof(char *) * (u + 2))) == NULL)
    return (NULL);

  u = 0;
  while (tab[u]) {
    new[u] = strdup(tab[u]);
    u++;
  }
  new[u] = strdup(newline);
  new[u + 1] = NULL;

  u = 0;
  while (tab[u]) {
    free(tab[u]);
    u++;
  }
  free(tab);
  return (new);
}

It's called from main() like this:

tab = addtotab(tab, line)

My problem is that tab has the wrong data at the end of the program. tab[0] is ALWAYS empty.

For example, I initialize tab with this data:

Alii summum decus in carruchis solito altioribus
Et quia Montius inter dilancinantium manus
Restabat ut Caesar post haec properaret accitus et
Siquis enim militarium vel honoratorum aut nobilis
Et olim licet otiosae sint tribus pacataeque
Ut enim quisque sibi plurimum confidit et ut
Sed laeditur hic coetuum magnificus splendor
Etenim si attendere diligenter, existimare vere de
Denique Antiochensis ordinis vertices sub uno
Post quorum necem nihilo lenius ferociens Gallus

but I got this result

Empty
Et quia Montius inter dilancinantium manus
Restabat ut Caesar post haec properaret accitus et
Siquis enim militarium vel honoratorum aut nobilis
Et olim licet otiosae sint tribus pacataeque
Ut enim quisque sibi plurimum confidit et ut
Sed laeditur hic coetuum magnificus splendor
Etenim si attendere diligenter, existimare vere de
Denique Antiochensis ordinis vertices sub uno
Post quorum necem nihilo lenius ferociens Gallus

Can you help me? Many thanks.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
oxmolol
  • 125
  • 1
  • 1
  • 10
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). This is not the cause of your problem(s), but a friendly hint. – unwind Feb 19 '14 at 14:48
  • hehe, school reflex ( a "norme" to respect... ) – oxmolol Feb 19 '14 at 14:53
  • I rolled back your recent edit. The way to indicate that the problem is solved is to accept an answer (which you've done), not to add "SOLVED" to the title. – Keith Thompson Feb 19 '14 at 16:32
  • ok sry, its done! and thx for the tips and the edit! – oxmolol Feb 19 '14 at 16:41

2 Answers2

0

You have to allocate space foru + 2, not u + 1 elements.

For example if u is 2, your original tab had 3 elements (first, second, NULL), and now you need space for 4 elements.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

In addition to allocating memory for u+2 items, the problem is the line

free(*tab);

You didn't show us how you created tab before passing it to the function, but there are 2 possibilities:

  • If you created it the same way you create new[], then you need to free() it by following the same steps in reverse:
    int u = 0;
    while (tab[u]) {
      free(tab[u]);
      tab[u] = NULL;  // Not necessary, but makes debugging easier.
    }
    free(tab);
    tab = NULL;
  • If you didn't create it with malloc(), then `free()'ing it is an error.
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • strdup uses malloc, so i need to free it, a friend helped me, and now i've some new line for the free ( look the EDIT ) the free isnt a problem now. I lose the first line only now. – oxmolol Feb 19 '14 at 15:18
  • Please update your post with the problem and the solution, so the next person will benefit from it. Thank you! – Adam Liss Feb 19 '14 at 16:14
  • 1
    done! in fact I didnt alloc u + 2 but u + 1, my free wasbad and I tried to force dup the first line, and on the first call of the function tab[0] turn empty because of my brute force, so all time i called my fonction, the tab[0] stay empty. i just delete my bruteforce and now it's ok! – oxmolol Feb 19 '14 at 16:22