C中的malloc:C中的动态内存分配

本文介绍了C语言中的动态内存分配函数malloc(),它允许从堆中分配内存。malloc()是stdlib.h的一部分,使用时需要包含头文件。通过malloc()可以为未知大小的数据类型分配内存,但需要注意使用sizeof()确保正确计算内存大小。分配的内存需要使用free()释放,否则可能导致内存泄漏。文章还强调了malloc()在处理超出当前作用域的对象时的效率优势。
摘要由CSDN通过智能技术生成

什么是C中的malloc()? (What is malloc() in C?)

malloc() is a library function that allows C to allocate memory dynamically from the heap. The heap is an area of memory where something is stored.

malloc()是一个库函数,它允许C从堆动态分配内存。 堆是存储内容的内存区域。

malloc() is part of stdlib.h and to be able to use it you need to use #include <stdlib.h>.

malloc()是stdlib.h的一部分,要使用它,您需要使用#include <stdlib.h>

如何使用Malloc (How to Use Malloc)

malloc() allocates memory of a requested size and returns a pointer to the beginning of the allocated block. To hold this returned pointer, we must create a variable. The pointer should be of same type used in the malloc statement.Here we’ll make a pointer to a soon-to-be array of ints

malloc()分配请求大小的内存,并返回指向已分配块开头的指针。 要保留此返回的指针,我们必须创建一个变量。 该指针应与malloc语句中使用的类型相同。在这里,我们将创建一个指向即将成为整数的数组的指针。

int* arrayPtr;

Unlike other languages, C does not know the data type it is allocating memory for; it needs to be told. Luckily, C has a function called sizeof() that we can use.

与其他语言不同,C不知道它为其分配内存的数据类型。 它需要被告知。 幸运的是,C有一个我们可以使用的名为sizeof()的函数。

arrayPtr = (int *)malloc(10 * sizeof(int));

This statement used malloc to set aside memory for an array of 10 integers. As sizes can change between computers, it’s important to use the sizeof() function to calculate the size on the current computer.

该语句使用malloc为10个整数的数组留出内存。 由于大小可以在计算机之间改变,因此使用sizeof()函数计算当前计算机上的大小非常重要。

Any memory allocated during the program’s execution will need to be freed before the program closes. To free memory, we can use the free() function

在程序关闭之前,必须先释放程序执行期间分配的所有内存。 要free内存,我们可以使用free()函数

free( arrayPtr );

This statement will deallocate the memory previously allocated. C does not come with a garbage collector like some other languages, such as Java. As a result, memory not properly freed will continue to be allocated after the program is closed.

该语句将取消分配先前分配的内存。 C没有像Java之类的其他语言那样带有garbage collector 。 因此,关闭程序后,将继续分配未正确释放的内存。

在继续之前... (Before you go on…)

回顾 (A Review)

  • Malloc is used for dynamic memory allocation and is useful when you don’t know the amount of memory needed during compile time.

    Malloc用于动态内存分配,当您在编译时不知道所需的内存量时很有用。
  • Allocating memory allows objects to exist beyond the scope of the current block.

    分配内存允许对象存在于当前块的范围之外。
  • C passes by value instead of reference. Using malloc to assign memory, and then pass the pointer to another function, is more efficient than having the function recreate the structure.

    C通过值而不是引用传递。 使用malloc分配内存,然后将指针传递给另一个函数,比让函数重新创建结构更有效。

有关C编程的更多信息: (More info on C Programming:)

翻译自: https://www.freecodecamp.org/news/malloc-in-c-dynamic-memory-allocation-in-c-explained/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值