小编use*_*520的帖子

如果NULL和大小0传递给realloc()怎么办?

是否定义了行为实现?如果将NULL和size == 0传递给realloc():

int main(void)
{
    int *ptr = NULL;

    ptr = realloc(ptr, 0);

    if(ptr == NULL)
    {
        printf("realloc fails.\n");
        goto Exit;
    }

    printf("Happy Scenario.\n");

Exit:
    printf("Inside goto.\n");

return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码应该打印"realloc failed",对吧?但事实并非如此?我已经读过某个地方,这个调用也realloc可能返回NULL.什么时候发生?

c memory-management realloc

6
推荐指数
3
解决办法
4166
查看次数

sscanf的行为是不同的

#include <stdio.h>

int main()
{
   char * msg = "Internal power 10. power sufficient. total count 10";
   char * temp = "Internal power %d. power %s. total count %d";
   int v1, v2, ret;
   char str1[64];
   ret = sscanf(msg, temp, &v1, str1, &v2);

   printf("%d\n", ret);
   printf("%d %s %d ", v1, str1 , v2);

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想了解为什么sscanf失败以及为什么它无法检索最后一个变量?

c scanf

-2
推荐指数
1
解决办法
96
查看次数

标签 统计

c ×2

memory-management ×1

realloc ×1

scanf ×1