Is it the correct way of allocating memory to a char*.
char* sides =\"5\";
char* tempSides;
tempSides = (char*)malloc(strlen(inSides) * sizeof(char));
The correct way of allocation dynamic memory to tempSides
is as shown below:
char* sides ="5";
char* tempSides;
tempSides = (char*)malloc((strlen(sides) + 1) * sizeof(char));
char*
stores a string data, similar to char[]
. Strings are null (\0)
terminated. So extra one byte should be allocated for null
character storage.
Dynamically allocated memory block must be freed using free()
after it's use is over. If not freed, memory leak would happen.
free(tempSides);
One the memory is freed, NULL
must be assigned to prevent it from being a dangling pointer.
tempSides = NULL;