Is it the correct way of allocating memory to a char*.
char* sides =\"5\";
char* tempSides;
tempSides = (char*)malloc(strlen(inSides) * sizeof(char));
Multiplying the element count by sizeof(char)
is a matter of personal preference, since sizeof(char)
is always 1. However, if you do this for consistency, better use the recipient pointer type to determine the element size, instead of specifying type explicitly. And don't cast the result of malloc
tempSides = malloc(strlen(inSides) * sizeof *tempSides);
Of course, when working with zero-terminated strings you have to remember to allocate extra space for the terminating zero character. There's no way to say whether it is your intent to make tempSides
a zero-terminated string in this case, so I can't say whether you need it.