Remember that when allocating spacefor a string, the argument to malloc must be one plus thelengthofthestring. This is because a stringis terminated with a null characterthat doesn’t countinthe “length” ofthestringbutdoes need space. For example:
char *ptr;
...
ptr = (char *) malloc (length + 1);