小编mar*_*arr的帖子

在malloc()之后写入指针越界,不会导致错误

当我尝试下面的代码时它工作正常.我错过了什么吗?

main()
{
    int *p;
    p=malloc(sizeof(int));
    printf("size of p=%d\n",sizeof(p));
    p[500]=999999;
    printf("p[0]=%d",p[500]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用malloc(0*sizeof(int))或其他任何东西,但它工作得很好.当我根本不使用malloc时,程序只会崩溃.因此,即使我为数组p分配了0个内存,它仍然可以正确地存储值.那么为什么我甚至不打扰malloc呢?

c malloc memory-corruption

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

如何从文件中读取特定格式的数据?

我应该从类似于这种格式的文件中读取输入和参数:

Add  id:324  name:"john" name2:"doe" num1:2009 num2:5 num2:20
Run Code Online (Sandbox Code Playgroud)

问题是我不允许使用fgets.我尝试使用fscanf,但不知道如何忽略":"并分隔字符串'name:"john"'.

c scanf

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×2

malloc ×1

memory-corruption ×1

scanf ×1