My program crashes on following piece od code:
newElement->name = (char*) malloc((strlen(name) + 1) * sizeof(char));
By using debugger I get SIGABRT error and I don't know why because it stops during second iteration (first iteration goes without any problem).
I checked if
name
has a treminating null character and it has.
Here is whole code:
element* createElement(const char* name, const char* text) {
if (name == NULL) return NULL;
element* newElement = malloc(sizeof(element));
if (newElement == NULL) return NULL;
newElement->name = (char*) malloc((strlen(name) + 1) * sizeof(char));
if (newElement->name == NULL) return NULL;
strcpy(newElement->name, name);
if (text == NULL) newElement->text = NULL;
else
{
newElement->text = malloc((strlen(text) + 1) * sizeof(char));
if (newElement->text == NULL) return NULL;
strcpy(newElement->text, text);
}
newElement->parentNode = NULL;
newElement->previousSibling = NULL;
newElement->nextSibling = NULL;
newElement->firstChild = NULL;
newElement->lastChild = NULL;
return newElement;
}