免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3611 | 回复: 17
打印 上一主题 下一主题

请教关于calloc()和malloc() [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-28 17:30 |只看该作者 |倒序浏览
我在书上看到一个例子,运作时觉得有点奇怪
程序如下:
#include <stdio.h>;
#include <stdlib.h>;

void main()
{
        int *a,i,n,sum = 0;
        scanf("%d\n",&n);
        a = calloc(n,sizeof(int));
        for(i = 0;i<n;++i)
                scanf("%d\n",&a);
        for(i = 0;i<n;++i)
                sum+=a;
        free(a);
        printf("%d\n",sum);
}
我要计算三个数和时却要我打入4个数,而计算则只算了前面三个,第四个数为什么要打阿?请指教。
另外calloc()和malloc()什么时候会自动释放内存空间,而什么时候又不会阿?这段书上没有讲明,有点奇怪。

论坛徽章:
0
2 [报告]
发表于 2005-04-28 20:48 |只看该作者

请教关于calloc()和malloc()

关注ing ,好象第一个数据在循环以前就读入了
不懂

论坛徽章:
0
3 [报告]
发表于 2005-04-28 21:04 |只看该作者

请教关于calloc()和malloc()

  1. #include <stdio.h>;
  2. #include <stdlib.h>;

  3. void main()
  4. {
  5. int *a,i,n,sum = 0;
  6. scanf("%d\n",&n);                    /*最好将'\n'去掉,第1个输入*/
  7. a = calloc(n,sizeof(int));
  8. for(i = 0;i<n;++i)
  9. scanf("%d\n",&a[i]);                /*最好将'\n'去掉,若n=3则此处有3个输入一共有四个输入,且计算结果为后三个值和*/
  10. for(i = 0;i<n;++i)
  11. sum+=a[i];
  12. free(a);
  13. printf("%d\n",sum);
  14. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2005-04-29 10:20 |只看该作者

请教关于calloc()和malloc()

谢谢楼上的
'\n'去掉后果然问题解决了
不过为什么会这样阿?

论坛徽章:
0
5 [报告]
发表于 2005-05-01 22:06 |只看该作者

请教关于calloc()和malloc()

malloc() takes a size and returns a pointer to a chunk of memory at least that big:
void *malloc( size_t size );
         calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all:
void *calloc( size_t numElements, size_t sizeOfElement );
          There are one major difference and one minor difference between the two functions.
          The major difference is that malloc() doesn't initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you are going to use as a char or an int of any  length, signed or unsigned, is guaranteed to be zero.Anything you are going to use as a pointer is set to all zero bits.That is usually a null pointer, but it is not guaranteed.Anything you are going to use as a float or double is set to all zero bits; that is a floating-point zero on some types of machines, but not on all.
           The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.

论坛徽章:
0
6 [报告]
发表于 2005-05-02 08:40 |只看该作者

请教关于calloc()和malloc()

因为\n是转义字符串
是换行的意思
通常是在printf()里面用

论坛徽章:
0
7 [报告]
发表于 2008-08-11 23:06 |只看该作者
calloc分配在堆上的内存是要初始化为0的

论坛徽章:
0
8 [报告]
发表于 2008-08-11 23:42 |只看该作者
i = 0;i<n;++i

后面的条件变成i++就可以了

论坛徽章:
0
9 [报告]
发表于 2008-08-11 23:50 |只看该作者
自动释放?
程序退出就自动释放了

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
10 [报告]
发表于 2008-08-12 09:37 |只看该作者
man calloc
man malloc
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP