How do you know how much space to allocate with malloc()?

前端 未结 6 824
南旧
南旧 2021-02-14 06:31

I\'m a total C newbie, I come from C#. I\'ve been learning about memory management and the malloc() function. I\'ve also came across this code:

char         


        
6条回答
  •  忘了有多久
    2021-02-14 07:04

    Your call to malloc will allocate 3 bytes of memory. sizeof(char) is 1 byte and 2 bytes are indicated explicitly. This gives you enough space for a string of size 2 (along with the termination character)

提交回复
热议问题