理解 malloc 参数大小的问题

Problems to understand the size of malloc parameter

谁能解释一下下面的代码是如何工作的?因为我发现自己不知道 |malloc(inputNim+1)andexit(1)` 在下面的代码中代表什么...

buffer = (char*) malloc (inputNum+1);
if (buffer==NULL) exit (1);

此行尝试分配 inputNum + 1 字节的内存:

buffer = (char*) malloc (inputNum+1);

下面一行检查上面的分配是否成功。如果 malloc 失败,它会 returns nullptr (NULL) 然后决定以 return 值 1 退出程序。一个常见的约定是在成功时以 0 退出,在失败时以其他方式退出。

if (buffer==NULL) exit (1); // if allocation failed, end program