The only way that code segment would be likely to cause a problem is if the malloc
actually failed (returning NULL
).
Other than that, there should be no issue. Any call that can fail should be checked if it's possible that failure would cause issues later on, and memory allocation followed by de-referencing that memory definitely falls within that description.
A better solution would be something like:
T->oa[i].key = malloc(5);
if (T->oa[i].key == NULL)
doSomethingIntelligent();
else
strcpy(T->oa[i].key, "abc");
You'll notice I've also changed the malloc
call. In C, it's rarely a good idea to cast the return value from malloc
since:
- the
void *
returned can be implicitly cast to any other pointer; and
- it can cause certain subtle errors.
In addition, you never need to multiply by sizeof(char)
since that is always, by definition, one.