B
Ben Bacarisse
JC said:JC Â said:[...]
   (*node)->elem = (char*) malloc (strlen (elem) + 1);
   if ( NULL == (*node)->elem ) {
    error = C_ERR_NO_MEMORY;
   }
   strncpy ((*node)->elem, elem, strlen ((*node)->elem) + 1);
(*node)->elem does not point to a string (yet); you cannot pass it
to strlen(). Â Either you want
   strncpy ((*node)->elem, elem, strlen (elem) + 1);
or (simpler)
   strcpy ((*node)->elem, elem);
NO, I don't agree.
Because (*node->elem) have been malloced to char*.
And when we copy elem to (*node->elem), we should make sure copy strlen
((*node)->elem) + 1 length at most.
So, here should be :
strncpy ((*node)->elem, elem, strlen ((*node)->elem) + 1);
Am I correct?
No. Lets leave the fact the *node can be NULL out of this (I pointed
that out in another message). After:
(*node)->elem = (char*) malloc (strlen (elem) + 1);
There are two problems. (1) Even when you see that the allocation
failed all you do is set error. You then go on to use (*node)->elem
even though it might be NULL. (2) In the best outcome, when the
allocation worked, you still can't call strlen((*node)->elem) because
the contents of the newly allocated storage are indeterminate.