首页 > 系统相关 >C标准库 | 内存分配以及释放函数汇总

C标准库 | 内存分配以及释放函数汇总

时间:2023-01-17 18:37:43浏览次数:37  
标签:__ 函数 age 汇总 height person 内存 size


C标准库 | 内存分配以及释放函数汇总_内存释放

在日常C语言使用过程中,不可避免遇到从堆中申请空间给特定的数据结构(结构体指针)!

一、头文件

#include <stdlib.h>

文件所在路径:

$ ls /usr/include/stdlib.h

二、函数声明

/* Allocate SIZE bytes of memory.  */
extern void *malloc (size_t __size) __THROW __attribute_malloc__
__attribute_alloc_size__ ((1)) __wur;
/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
extern void *calloc (size_t __nmemb, size_t __size)
__THROW __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __wur;

/* Re-allocate the previously allocated block
in PTR, making the new block SIZE bytes long. */
/* __attribute_malloc__ is not used, because if realloc returns
the same pointer that was passed to it, aliasing needs to be allowed
between objects pointed by the old and new pointers. */
extern void *realloc (void *__ptr, size_t __size)
__THROW __attribute_warn_unused_result__ __attribute_alloc_size__ ((2));

/* Free a block allocated by `malloc', `realloc' or `calloc'. */
extern void free (void *__ptr) __THROW;

三、函数作用

形参的作用:

  • ​__size​​:要被分配的元素大小(单位是字节)
  • ​__nmemb​​:要被分配的元素个数
  • ​__ptr​​:指向已分配内存块的指针

函数的作用:

函数

参数

返回值

作用

​malloc​

​__size​

​void *​

分配​​__size​​字节大小的内存块,并返回指向该内存块的指针

​calloc​

​__nmemb​​​, ​​__size​

​void​

分配​​__nmemb​​​个​​__size​​字节大小的内存块,并返回指向该内存块的指针

​realloc​

​__ptr​​​, ​​__size​

​long int​

把​​__ptr​​​内存块重新分配为​​__size​​字节大小,并返回指向新内存块的指针

​free​

​__ptr​

​void​

把​​__ptr​​内存块进行释放

注意点:

  • ​malloc​​​ 和 ​​calloc​​​ 函数之间的不同点是,​​malloc​​​ 函数不会设置内存块为零,而 ​​calloc​​ 函数会设置分配的内存块为零
  • ​realloc​​​ 函数的​​__ptr​​​参数内存块之前是通过调用 ​​malloc​​​、​​calloc​​​ 或 ​​realloc​​ 进行分配内存的。如果为空指针,则会分配一个新的内存块,且函数返回一个指向它的指针
  • ​realloc​​​ 函数的​​__size​​​参数为​​0​​​,则相当与​​free​​函数,并返回空指针
  • 没有分配的内存块不能使用​​free​​函数,会造成内存泄露

四、使用

  • 源代码:
#include <stdio.h>
#include <stdlib.h>

struct person {
int age;
int height;
int weight;
};

int main(int argc, char *argv[])
{
struct person *person;

person = (struct person *)malloc(sizeof(struct person));
person->age = 20;
person->height = 175;
person->weight = 130;
printf("persons.age: %d\n", person->age);
printf("persons.height: %d\n", person->height);
printf("persons.weight: %d\n", person->weight);
free(person);

person = (struct person *)calloc(1, sizeof(struct person));
person->age = 30;
person->height = 180;
person->weight = 150;
printf("persons.age: %d\n", person->age);
printf("persons.height: %d\n", person->height);
printf("persons.weight: %d\n", person->weight);

person = (struct person *)realloc(person, 2 * sizeof(struct person));
person->age = 30;
person->height = 180;
person->weight = 150;
printf("persons.age: %d\n", person->age);
printf("persons.height: %d\n", person->height);
printf("persons.weight: %d\n", person->weight);
person++; // 指向第二项结构体
person->age = 40;
person->height = 185;
person->weight = 160;
printf("persons.age: %d\n", person->age);
printf("persons.height: %d\n", person->height);
person--; // 回到指向第一项结构体
free(person); // 必须传入指向第一项结构体的指针,否则会内存泄漏

return 0;
}
  • 打印结果:

C标准库 | 内存分配以及释放函数汇总_malloc_02


标签:__,函数,age,汇总,height,person,内存,size
From: https://blog.51cto.com/u_13726704/6017589

相关文章

  • DataFrame - to_csv()函数乱码
    https://blog.csdn.net/weixin_39559994/article/details/125297263https://blog.csdn.net/qq_40258748/article/details/96306878......
  • 内存结构与垃圾回收
    ......
  • golang: os.Write函数
    函数解析os.Write函数func(f*File)Write(b[]byte)(nint,errerror){//判断是否f为nil iferr:=f.checkValid("write");err!=nil{ return0,e......
  • python内存中生成excel和zip文件
    目录python内存中生成excel和zip文件直接返回Zip文件直接返回Excel文件内存中生成Excel文件再压缩生Zip文件python内存中生成excel和zip文件我们知道pandas和zipfile两个......
  • redis占用内存过高怎么办???
    运维排查篇|Redis占用内存过高怎么办?原创 咸鱼不想秃头 咸鱼爱搞机 2022-09-1921:40 发表于上海前言 我们知道,Redis是一个key-value数据库,它的数据是运行......
  • vue脚手架—render函数
    一、关于不同版本Vue:1.vue.js和vue.runtime..js的区别:(1)vue.js是完整版的Vue,包含核心功能+模板解析器(2)vue.runtime..js是运行版的Vue,只包含核心功能,没有模板解析器......
  • 小满Vue3第三十八章(函数式编程,h函数)
    之前跟大家介绍了两种vue编写风格分别是template模板方式,和JSX方式感觉JSX被大家吐槽的很厉害,其实用习惯还挺好用的今天介绍第三种函数式编程主要会用到h函数​​h​​ 接......
  • 学习Vue3 第二十九章(Vue3定义全局函数和变量)
    globalProperties由于Vue3没有Prototype属性使用app.config.globalProperties代替然后去定义变量和函数Vue2//之前(Vue2.x)Vue.prototype.$http=()=>{}Vue3//......
  • 《SQL基础》06. 函数
    目录函数字符串函数数值函数日期函数流程函数函数函数是指一段可以直接被另一段程序调用的程序或代码。要查看函数操作的结果,可以使用SELECT函数(参数);select代表......
  • socket编程相似对象、函数、概念的区别于联系
    socketaddr、sockaddr_in与addr_insocketaddr与socketaddr_in的关系类似于基类和派生类的关系。addr_in是socketaddr_in中一个成员变量。structso......