Allocating memory to char* C language

后端 未结 7 1585
执笔经年
执笔经年 2021-01-05 06:47

Is it the correct way of allocating memory to a char*.

char* sides =\"5\";

char* tempSides;

tempSides = (char*)malloc(strlen(inSides) * sizeof(char));


        
相关标签:
7条回答
  • 2021-01-05 07:39

    There's a problem with that. tempSides will point to an uninitialized block of memory of size 1. If you intend to copy the sides string into tempSides, then you will need to allocate a size one byte longer, in order to hold the zero terminator for the string. The value returned by strlen() does not include the zero terminator at the end of the string.

    0 讨论(0)
提交回复
热议问题