calloc和malloc

Here in this tutorial you will learn about difference between malloc() and calloc() in C.

在本教程的这里,您将学习C语言中malloc()和calloc()之间的区别。

We all know that memory is available in limited size only, so it becomes important for us to use it efficiently. If we are writing a program that uses too much of memory resulting in wastage of memory or conversely if we allocate too little memory so that we can’t store enough information, that means we are not sure of the amount of data to be stored. In such cases, dynamic memory allocation comes to the rescue, where our program is capable to allocate whatever amount of memory it needs during the run-time of programs.

我们都知道,内存仅在有限的大小内可用,因此有效使用内存对我们来说很重要。 如果编写的程序使用过多的内存导致内存浪费,或者相反,如果分配的内存太少以致于我们无法存储足够的信息,则意味着我们不确定要存储的数据量。 在这种情况下, 动态内存分配便可以解决,我们的程序能够在程序运行时分配所需数量的内存。

Now the question arises is that how can we achieve dynamic memory allocation in our programs?

现在出现的问题是,如何在程序中实现动态内存分配?

In the C Language, we have predefined functions like calloc() and malloc() defined under the stdlib.h header file that can be used to allocate memory during the runtime of a program.

在C语言中,我们在stdlib.h头文件下定义了预定义的函数,如calloc()和malloc(),可在程序运行时用于分配内存。

Let’s understand this awesomely useful function with simple examples:

让我们通过简单的示例来了解这个非常有用的功能:

malloc()函数 (malloc() Function)

malloc() is an abbreviation for memory-allocation. It is a predefined function defined in the stdlib.h header file. It is used to allocate memory during the runtime of a program. The memory allocated is uninitialized that means it has garbage values.

malloc()函数是用于 emory- 的alloc通货膨胀的缩写。 它是在stdlib.h头文件中定义的预定义函数。 它用于在程序运行时分配内存。 分配的内存是未初始化的,这意味着它具有垃圾值。

Syntax:

句法:

ptr = (type*) malloc (size in bytes to be allocated)

The malloc() function takes size in bytes to be allocated as argument. If the memory allocation is successful, it returns a void pointer to the beginning of the allocated memory. This void pointer can be type-casted to any type of pointer. If the memory allocation fails due to reasons like insufficient memory, the malloc() function returns a NULL pointer.

malloc()函数采用字节大小作为参数分配。 如果内存分配成功,它将返回一个空指针 , 指向已分配内存的开头。 可以将这个void指针类型转换为任何类型的指针。 如果由于内存不足等原因导致内存分配失败,则malloc()函数将返回NULL指针 。

It is the responsibility of the programmer to deallocate the allocated memory before program ends using free() or realloc() functions.

程序员有责任在程序结束之前使用free()或realloc()函数释放已分配的内存。

C Program Implementation of malloc() Function

malloc()函数的C程序实现

#include <stdio.h>
#include <stdlib.h>int main()
{/* The memory allocated by malloc contains garbage value */   /* allocates 10*sizeof(int) = 40 bytes of memory */  int *ptr1 = (int*)malloc( 10 * sizeof(int));      /* allocates 8*sizeof(char) = 8 bytes of memory  */  char *ptr2 = (char*)malloc( 8 * sizeof(char)); if(ptr1)   { printf("Memory allocation successful !!");  }  else  {  printf("Memory allocation failed !!");  }  free(ptr1);  free(ptr2);  return 0;
}

Output

输出量

Memory allocation successful !!

内存分配成功!

calloc()函数 (calloc() Function)

calloc() is an abbreviation for c-contiguous a-allocation. It is an advancement over the malloc() function. It is used to allocate multiple blocks of memory of the same size dynamically. The memory is initialized with zero.

calloc()是c-连续a-分配的缩写。 它是对malloc()函数的改进。 它用于动态分配相同大小的多个内存块。 内存初始化为零。

Syntax:

句法:

ptr = (type*) calloc (number of blocks , the size of blocks in bytes to be allocated)

The calloc() function takes two arguments. First argument is the number of blocks of memory to be allocated and the second argument is used to define the size of blocks in terms of bytes. If the memory allocation is successful, it returns a void pointer to the beginning of the allocated memory. This void pointer can be type-casted to any type of pointer. If the memory allocation fails due to reasons like insufficient memory, the calloc() function returns a NULL pointer.

calloc()函数采用两个参数。 第一个参数是要分配的内存块数,第二个参数用于以字节为单位定义块的大小。 如果内存分配成功,它将返回一个空指针,指向已分配内存的开头。 可以将这个void指针类型转换为任何类型的指针。 如果由于内存不足等原因而导致内存分配失败,则calloc()函数将返回NULL指针。

It is the responsibility of the programmer to deallocate the allocated memory before program ends using free() or realloc() functions.

程序员有责任在程序结束之前使用free()或realloc()函数释放已分配的内存。

C Program Implementation of calloc() Function

C语言实现calloc()函数

#include <stdio.h>
#include <stdlib.h>  int main()
{  /* The calloc function initialize the memory with 0 */   /* allocates 4 blocks of memory with size of int */  int *ptr1 = (int*)calloc(4, sizeof(int));      /* allocates 8 blocks of memory with size of float */  float *ptr2 = (float*)calloc(8, sizeof(float));   if(ptr1)   {  printf("The values are:\n");  for(int i=0;i<4;i++)  {  /* printing the data of memory-allocated */  printf("Memory block %d : %d \n",i+1,*(ptr1+i));   }  }  else  {  printf("Memory allocation failed !!");  } free(ptr1);  free(ptr2);  return 0;
}

Output

输出量

The values are: Memory block 1 : 0 Memory block 2 : 0 Memory block 3 : 0 Memory block 4 : 0

值为: 内存块1:0 内存块2:0 内存块3:0 内存块4:0

malloc()vs calloc()– malloc()和calloc()之间的区别 (malloc() vs calloc() – Difference between malloc() and calloc())

Parameters malloc() calloc()
Abbreviated for m-memory,  alloc-allocation c-contiguous,  alloc-allocation
Syntax: (void*) malloc (n * size in bytes) (void*) calloc (n , size in bytes)
Definition It is a predefined function defined in stdlib.h header file used to allocate memory dynamically in terms of bytes. It is predefined function present in stdlib.h header file used to allocate memory dynamically in terms of number of blocks of memory with given size in bytes.
No.  of Arguments It takes single argument. It takes two arguments.
No.  of memory blocks Allocates a single block of memory with given byte-size. Allocates multiple blocks of memory that are contiguous in memory with given byte-size.
Initialization It does not initialize the allocated memory. It initializes the allocated memory blocks with 0.
Garbage value Present Absent
Speed It is fast. It takes time to allocate multiple blocks of memory.
Used for Creating structures Creating dynamic Arrays
参量 malloc() calloc()
缩写为 存储器,alloc-分配 C-邻接的,alloc-分配
句法: (void *)malloc(n *字节大小) (void *)calloc(n,大小以字节为单位)
定义 它是stdlib.h头文件中定义的预定义函数,用于按字节动态分配内存。 它是stdlib.h头文件中存在的预定义函数,用于根据给定大小(以字节为单位)的内存块数动态分配内存。
参数个数 它需要一个参数。 它有两个参数。
内存块数 用给定的字节大小分配一个内存块。 以给定的字节大小分配在内存中连续的多个内存块。
初始化 它不会初始化分配的内存。 它将分配的内存块初始化为0。
垃圾价值 当下 缺席
速度 很快 分配多个内存块需要花费时间。
用于 建立结构 创建动态数组

Comment down below if you have any queries related to malloc() and calloc() in C.

如果您对C中的malloc()和calloc()有任何疑问,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2019/09/malloc-vs-calloc.html

calloc和malloc

calloc和malloc_malloc()vs calloc()– C中的malloc()和calloc()之间的区别相关推荐

  1. Spring MVC和REST中@RestController和@Controller注释之间的区别

    Spring MVC中的@RestController注释不过是@Controller和@ResponseBody注释的组合. 它已添加到Spring 4.0中,以简化在Spring框架中RESTfu ...

  2. C#中的结构和类之间的区别

    C#类和结构 (C# class and structure) In C# and other programming languages, structure and classes are use ...

  3. SQL 中的=,in,like之间的区别

    SQL中的=,in,like之间的区别: 三者都可以用来进行数据匹配 .但三者并不相同. 等号是用来查找与单个值匹配的所有数据: IN 是 用来查找与多个值匹配的所有数据: 而 LIKE用来查找与一个 ...

  4. mysql insert into values select_mysql中insert语句中,value与values之间的区别?

    你的位置: 问答吧 -> JAVA -> 问题详情 mysql中insert语句中,value与values之间的区别? mysql> select * from tt; +---- ...

  5. c语言中的typedef struct相当于java的一个类?,C ++中'struct'和'typedef struct'之间的区别?...

    在C ++中,之间有什么区别: struct Foo { ... }; 和 typedef struct { ... } Foo; #1楼 您不能对typedef结构使用forward声明. stru ...

  6. Spring MVC中@RequestParam和@PathVariable批注之间的区别?

    Spring MVC框架是在Java世界中开发Web应用程序最流行的框架之一,它还提供了一些有用的注释,可以从传入的请求中提取数据并将请求映射到控制器,例如@ RequestMapping,@ Req ...

  7. 微内核和宏内核的区别_8086微处理器中的过程和宏之间的区别

    微内核和宏内核的区别 Prerequisite 先决条件 Procedure in 8086 Microprocessor 8086微处理器中的过程 Macros in 8086 Microproce ...

  8. uint16 int c#_C#中的Int16和UInt16之间的区别

    uint16 int c# C#Int16和C#UInt16 (C# Int16 and C# UInt16) In C#, Int16 known as a signed integer of 2 ...

  9. nginx中301和302重定向之间的区别

    首先客户端浏览器的URL都会改变: 302重定向是暂时的重定向,搜索引擎会抓取新的内容而保留旧的地址.因为服务器返回302,所以搜索引擎会认为新的网址是暂时的: 301重定向是永久的重定向,搜索引擎会 ...

  10. java 方法 函数 区别_Java中的构造函数和方法之间的区别

    Java方法一种方法用于探索对象的行为. 我们可以在方法的前面加上访问修饰符. 方法必须具有返回类型,例如void,任何原始类型(int,char,float等),任何Object类型(Integer ...

最新文章

  1. JavaScript 兼容新旧版chrome和firefox的桌面通知
  2. python基础 协程
  3. PyTorch-模型
  4. Fiori My Task App Performance Analysis
  5. jquery ajax xml attribute,获得jQuery ajax和asp.net webmethod xml响应工作
  6. 初窥wordcloud之老司机带你定制词云图片
  7. Android两次按返回键退出应用程序
  8. 程序包OE_Order_PUB过程 Process_.Order 中出现错误ORA-20001
  9. python windows 远程执行bat
  10. [转]关于java的动态代理
  11. ppt学习02——字体
  12. 机器人学回炉重造(3):matlab复现最基本的六自由度机械臂逆运动学推导
  13. 华氏温度和摄氏温度的转换-C语言
  14. Redis集群启动时发生如下错误:Node hadoop103:7002 replied with error: ERR Invalid node address specified: hadoop1
  15. Django6:应用及分布式路由
  16. 实现一个行内三个div等分_一个div,包含三个小的div,平均分布的样式
  17. 【JDK7】新特性(1) 概述
  18. HanLP中人名识别分析
  19. 网络基础(网络相关命令)
  20. 刚从培训机构出来的Java程序员且无工作经历该如何找工作?

热门文章

  1. 太原市建筑物矢量数据(Shp格式+带高度)
  2. VTS环境搭建以及debug vts_ltp_test_arm
  3. WorldWind源码剖析系列:BMNG类构造函数深入分析
  4. 头歌Educoder云计算与大数据——实验二 Hadoop单机部署
  5. kibana 修改Ico图标
  6. jaxb xsd java_jaxb 这个资料,可以吧后缀为.xsd的文件,转化成java类使用, 方法在 里面有详细的说 Develop 238万源代码下载- www.pudn.com...
  7. 读《ACM图灵奖》有感
  8. ethos从入门到精通-4.1映泰主板bios设置
  9. PTA(每日一题)7-61 简单的归并
  10. 希捷存储服务器型号,希捷SATA硬盘 打造高效存储服务器平台