What is malloc doing in this code?

后端 未结 11 936
眼角桃花
眼角桃花 2021-01-01 04:59

Could you explain following code?

str = (char *) malloc (sizeof(char) * (num+1));
  1. What is malloc doing here?
11条回答
  •  情话喂你
    2021-01-01 05:14

    Malloc is a call to allocate memory.

    The above code is going to allocate space for num + 1 characters. Likely there is a string with num characters in, and the author of the code has added space for the null terminator.

    After the call str will point to the start of that block of memory which has been allocated.

提交回复
热议问题