c语言,动态存储calloc函数

这段代码代表什么意思啊,怎么和我看的例子不一样
p=(int*)calloc(n,2);
我一般看到的是
p=(int*)calloc(3,sizeof(int));

calloc是一个IOS C函数
  函数名: calloc
  函数原型:void *calloc(size_t n, size_t size);
  功 能: 在内存的动态存储区中分配n个长度为size的连续空间,函数返回一个指向分配起始地址的指针;如果分配不成功,返回NULL。

  用 法:
  void *calloc(size_t n, size_t size);
  一般使用后要使用 free(起始地址的指针) 对内存进行释放,不然内存申请过多会影响计算机的性能,以至于得重启电脑。如果使用过后不清零,还可以使用指针对该块内存进行访问。
  头文件:stdlib.h或malloc.h
  相关函数:malloc、realloc、free _alloca
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-02
void *calloc( size_t num, size_t size );

Return Value

calloc returns a pointer to the allocated space. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

Parameters num--------Number of elements

size----------Length in bytes of each element
p=(int*)calloc(n,2);
指有是有n个元素,每个元素占2个字节。这个地方要是16位机的话int是2个字节,现在的一般是32位机所以int是4个字节,p=(int*)calloc(n,2);改为p=(int*)calloc(n,4);

p=(int*)calloc(3,sizeof(int));
指有是有3个元素,每个元素占sizeof(int)=4个字节。本回答被提问者采纳
相似回答