当前位置: 首页 > news >正文

怎么做好推广深圳网站优化培训

怎么做好推广,深圳网站优化培训,wordpress 调用菜单,wordpress 获取侧边栏他治愈了身边所有人,唯独没有治愈他自己—超脱 csdn上的朋友你们好呀!!今天给大家分享的是动态内存管理 👀为什么存在动态内存分配 我们定义的局部变量在栈区创建 int n 4;//在栈上开辟4个字节大小int arr[10] { 0 };//在栈上开…

在这里插入图片描述
他治愈了身边所有人,唯独没有治愈他自己—超脱

csdn上的朋友你们好呀!!今天给大家分享的是动态内存管理

👀为什么存在动态内存分配

在这里插入图片描述

我们定义的局部变量在栈区创建

    int n = 4;//在栈上开辟4个字节大小int arr[10] = { 0 };//在栈上开辟连续的40个字节大小

上述变量创建的特点
1. 空间开辟大小是固定的。
2. 数组在申明的时候,必须指定数组的长度,它所需要的内存在编译时分配。
但是对于空间的需求,不仅仅是上述的情况。有时候我们需要的空间大小在程序运行的时候才能知道,那数组的编译时开辟空间的方式就不能满足了

int main()
{int n;scanf("%d",&n);int arr[n];}

上述代码只能在C99标准编译器上才行,vs系列编译器均不支持,那我们怎么才能在运行的时候,实现上述变长数组的代码呢??这时候就只能试试动态存开辟了。

👀 动态内存函数的介绍

malloc

在这里插入图片描述

函数功能:开辟内存块
参数size_t:需要申请的字节数
返回值:申请失败返回空指针,申请成功返回指向申请该空间首地址的指针
头文件:stdlib.h
注意返回指针的类型是void*,这时候需要你把该指针强制类型转化为你想要的类型,这样方便访问,以及解引用,malloc申请来的空间是连续的,但是多次malloc来的是不连续的


malloc的使用

int main()
{int*p=(int*) malloc(40);//申请了40个字节,强制转化为int*类型指针if (p == NULL)//如果返回空指针的话,申请失败{perror("malloc:");//打印错误信息return 1;//非正常退出}for (int i = 0; i < 10; i++){*(p + i) = i;//对每一个四个字节大小的元素赋值,这里*(p+i)的本质就是p[i];printf("%d", *(p + i));//打印每个元素}return 0;//程序正常退出}

在这里插入图片描述


free

在这里插入图片描述

功能:释放内存块
参数:指针接收要释放内存块的首地址
头文件:stdlib.h
返回值:无

了解了这些之后,我们试一下释放刚才malloc来的内存块

int main()
{int i = 0;int*p=(int*) malloc(40);if (p == NULL){perror("malloc:");return 1;}for (int i = 0; i < 10; i++){*(p + i) = i;printf("%d", *(p + i));}free(p);//指针接收要释放内存块的首地址p = NULL;//很有必要否则p为野指针return 0;}

在这里插入图片描述
当p所指向的申请的空间释放时,p指针指向随机位置,p变成野指针。
如果我们不释放动态内存申请的内存的时候,程序结束,动态申请内存由操作系统自动回收,如果不用free函数释放申请好的空间,就会在程序运行结束前一直存在于堆中,造成内存泄漏

int main()
{while (1){malloc(1000);}return 0;}

在这里插入图片描述
我是不知天高地厚的年轻人哈哈哈哈哈


calloc

在这里插入图片描述

功能:申请一个数组在内存中,并且初始化为0;
参数:size_t num申请数组元素的个数,size_t size每个元素的字节大小
返回值:申请失败返回空指针,申请成功返回指向申请该空间首地址的指针
头文件:stdlib.h

calloc函数使用

int main()
{int i = 0;int*p=(int*) calloc(10,sizeof(int));//申请10个元素,每个元素字节大小4if (p == NULL)//如果返回空指针的话,申请失败{perror("calloc:");//打印错误信息return 1;//非正常退出}for (int i = 0; i < 10; i++){printf("%d ", *(p + i));//打印初始化的值}free(p);p = NULL;return 0;}

在这里插入图片描述

malloc和calloc的区别:与函数 malloc 的区别只在于 calloc 会在返回地址之前把申请的空间的每个字节初始化为全0
我们可以看看malloc有没有先初始化

int main()
{int* p = (int*)malloc(40);//申请了40个字节,强制转化为int*类型指针if (p == NULL)//如果返回空指针的话,申请失败{perror("malloc:");//打印错误信息return 1;//非正常退出}for (int i = 0; i < 10; i++){printf("%d ", *(p + i));//打印每个元素}free(p);//指针接收要释放内存块的首地址p = NULL;//很有必要否则p为野指针return 0;//程序正常退出}

在这里插入图片描述

可以看到是未初始化的,放的随机值


realloc

在这里插入图片描述

功能:内存块的扩容
参数:第一个参数接收要扩容内存块的首地址,扩容后总字节大小(包括原来的字节大小)
头文件:stdlib.h
返回值:在这里插入图片描述
在这里插入图片描述

realloc函数使用

int main()
{int* p = (int*)malloc(40);//申请了40个字节,强制转化为int*类型指针if (p == NULL)//如果返回空指针的话,申请失败{perror("malloc:");//打印错误信息return 1;//非正常退出}for (int i = 0; i < 10; i++)//循环打印扩容前的元素{*(p + i) = i;printf("%d ", *(p + i));}int* ptr = (int*)realloc(p, 80);//原空间够用ptr==p,不够用的话ptr存放新地址if (ptr != NULL)//扩容成功{p = ptr;//原空间够用ptr==p,不够用的话ptr存放新地址,重新将新地址给p}for (int i = 10; i < 20; i++)//扩容后新空间的{*(p + i) = i;printf("%d ", *(p + i));}free(p);p = NULL;return 0;
}

编译运行
在这里插入图片描述


👀常见的动态内存错误

1.对NULL指针的解引用操作

int main()
{int* p = (int*)malloc(1000);int i = 0;//if (p ==NULL)//{//	return 1;//}for (i = 0; i < 250; i++){*(p + i) = i;}free(p);p = NULL;return 0;
}

在这里插入图片描述

当malloc申请内存失败,p=NULL,i=0,相当于给空指针解引用

解决办法:对malloc函数返回值做出判断

int main()
{int* p = (int*)malloc(1000);int i = 0;if (p ==NULL){return 1;}for (i = 0; i < 250; i++){*(p + i) = i;}free(p);p = NULL;return 0;
}

2. 对动态开辟空间越界访问

int main()
{int* p = (int*)malloc(100);int i = 0;if (p ==NULL){return 1;}for (i = 0; i <=25; i++)//越界访问{*(p + i) = i;}free(p);p = NULL;return 0;
}

编译运行
在这里插入图片描述
解决方法:人为检查是否越界

修改:

int main()
{int* p = (int*)malloc(100);int i = 0;if (p ==NULL){return 1;}for (i = 0; i <25; i++)//=25变成<25{*(p + i) = i;}free(p);p = NULL;return 0;
}

3.对非动态开辟内存进行free

int main()
{int a = 10;int* p = &a;free(p);p = NULL;return 0;
}

编译运行
在这里插入图片描述

解决方案:你别手贱(🙂)


4.使用free释放一块动态开辟内存的一部分

int main()
{int* p = (int*)malloc(100);if (p == NULL){return 1;}int i = 0;for (i = 0; i < 10; i++){*p = i;p++;}free(p);p = NULL;return 0;}

编译运行
在这里插入图片描述

解决方案:别改变p指向的地址,或者用一个指针记录申请内存的首地址

plan1:

int main()
{int* p = (int*)malloc(100);if (p == NULL){return 1;}int i = 0;for (i = 0; i < 10; i++){*(p+i)= i;printf("%d ", *(p + i));}free(p);p = NULL;return 0;}

plan2:

int main()
{int* p = (int*)malloc(100);int* q = p;if (p == NULL){return 1;}int i = 0;for (i = 0; i < 10; i++){*p= i;printf("%d ", *p);p++;}free(q);q = NULL;return 0;}

5.多次free已经释放的内存

int main()
{int* p = malloc(40);if (p == NULL){return 1;}free(p);free(p);p = NULL;return 0;
}

编译运行
在这里插入图片描述
解决方案:别多次free已经释放的内存(滑稽)


6.动态开辟内存忘记释放

见上面


👀几个经典的笔试题

1

char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}

分析:定义一个char*str,让他指向空,str接收GetMemory()函数返回来的地址,进入GetMemory()函数,return p,只能把p[]的首地址传回去,而p[]是局部变量,出GetMemory(),p[]销毁,当你传回去的时候,str接收的是野地址,str为野指针。打印不出来hello world
编译运行:在这里插入图片描述


2.

void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}

分析:在这里插入图片描述

3.

void GetMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}

分析:在这里插入图片描述

但是没有free释放内存

运行编译
在这里插入图片描述


4.

void main(void)
{
char *str = (char *) malloc(100);
strcpy(str, "hello");
free(str);
if(str != NULL)
{
strcpy(str, "world");
printf(str);
}
}

分析:在这里插入图片描述

运行编译
在这里插入图片描述


👀 柔性数组

也许你从来没有听说过柔性数组(flexible array)这个概念,但是它确实是存在的。 C99 中,结构中的最
后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员

柔性数组的特点

结构中的柔性数组成员前面必须至少一个其他成员。sizeof 返回的这种结构大小不包括柔性数组的内存。
包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。

我们可以定义一个结构体

struct pp {int a;int b[];};
int main()
{printf("%d", sizeof(struct pp));}

编译运行
在这里插入图片描述

确实没有包括柔性数组大小

柔性数组的使用

struct pp {int a;int b[];//柔性数组成员
};
int main()
{struct pp* p = (struct pp*)malloc(sizeof(struct pp) + 10 * sizeof(int));//malloc中第一个元素大小+柔性数组字节大小p->a = 4;//赋值for (int i = 0; i < 10; i++){p->b[i] = i;//赋值}for (int i = 0; i < 10; i++){printf("%d ", p->b[i]);//打印柔性数组}printf("%d ", p->a);free(p);//free掉malloc来的空间p = NULL;//p置为空指针}

我们能不能用指针代替那个柔性数组呢,我们可以将指针指向的那个地方malloc来使用
定义一个结构体

struct pp {int a;int* p;
};

int main()
{struct pp* q = (struct pp*)malloc(sizeof(struct pp));//申请结构体大小的内存if (q == NULL)//判断申请是否成功{return 1;//异常退出}q->p = (int*)malloc(10*sizeof(int));if (q->p == NULL)//判断申请是否成功{return 1;//异常退出}q->a = 10;//赋值for (int i = 0; i < 10; i++){q->p[i]= i;赋值}printf("%d ", q->a);for (int i = 0; i < 10; i++){printf("%d ", q->p[i]);}free(q->p);//free掉p指针指向的另一块申请空间的内存q->p = NULL;//指针置空,防止野指针free(q);//free掉q指向的申请的内存q = NULL;}

分析:

malloc过程在这里插入图片描述
释放过程
在这里插入图片描述
编译运行
在这里插入图片描述

上述 代码1 和 代码2 可以完成同样的功能,但是 方法1 的实现有两个好处: 第一个好处是:方便内存释放
如果我们的代码是在一个给别人用的函数中,你在里面做了二次内存分配,并把整个结构体返回给用户。用户调用free可以释放结构体,但是用户并不知道这个结构体内的成员也需要free,所以你不能指望用户来发现这个事。所以,如果我们把结构体的内存以及其成员要的内存一次性分配好了,并返回给用户一个结构体指针,用户做一次free就可以把所有的内存也给释放掉。
第二个好处是:这样有利于访问速度.
连续的内存有益于提高访问速度,也有益于减少内存碎片,根据局部性原理,连续存放的数据,cup从缓冲区读取的快,缓存区从内存中读取的快。

总结

本片分享了四个动态内存函数,以及常见动态内存错误,几个经典的笔试题,以及柔性数组的概念,如果你觉得对你有帮助的话,希望能留下你的点赞,关注加收藏,如果有不对的地方,可以私信我,谢谢各位佬们!!!


文章转载自:
http://dinncorpc.wbqt.cn
http://dinncojoining.wbqt.cn
http://dinncoreafference.wbqt.cn
http://dinncochiliast.wbqt.cn
http://dinncoethereal.wbqt.cn
http://dinncofingerpaint.wbqt.cn
http://dinncoricin.wbqt.cn
http://dinncohydrotropically.wbqt.cn
http://dinncocacogenics.wbqt.cn
http://dinncoslabby.wbqt.cn
http://dinncomithridatise.wbqt.cn
http://dinncohumourist.wbqt.cn
http://dinncorestorative.wbqt.cn
http://dinncocharnel.wbqt.cn
http://dinncobonapartism.wbqt.cn
http://dinncorefugium.wbqt.cn
http://dinncoconfiscator.wbqt.cn
http://dinncoarithmetician.wbqt.cn
http://dinncounbeaten.wbqt.cn
http://dinncoloading.wbqt.cn
http://dinncoresinous.wbqt.cn
http://dinncoplaudit.wbqt.cn
http://dinncovelamen.wbqt.cn
http://dinncoknave.wbqt.cn
http://dinncosunk.wbqt.cn
http://dinncowadable.wbqt.cn
http://dinncosinking.wbqt.cn
http://dinncoresolutely.wbqt.cn
http://dinncomandarin.wbqt.cn
http://dinncoironical.wbqt.cn
http://dinncoconchiolin.wbqt.cn
http://dinncolegume.wbqt.cn
http://dinncomanioc.wbqt.cn
http://dinncopolarise.wbqt.cn
http://dinncolanarkshire.wbqt.cn
http://dinncoattachment.wbqt.cn
http://dinncoverglas.wbqt.cn
http://dinncomishanter.wbqt.cn
http://dinncotylosin.wbqt.cn
http://dinncohear.wbqt.cn
http://dinncoulm.wbqt.cn
http://dinncopsychoanalyst.wbqt.cn
http://dinncobiotype.wbqt.cn
http://dinncomaharashtrian.wbqt.cn
http://dinncorepulsively.wbqt.cn
http://dinncogourde.wbqt.cn
http://dinncomartemper.wbqt.cn
http://dinncorecompose.wbqt.cn
http://dinncougric.wbqt.cn
http://dinncorunback.wbqt.cn
http://dinncoperiscopic.wbqt.cn
http://dinncohindostani.wbqt.cn
http://dinncoharbinger.wbqt.cn
http://dinncoglanduliferous.wbqt.cn
http://dinncofustic.wbqt.cn
http://dinncodinky.wbqt.cn
http://dinncorarified.wbqt.cn
http://dinncohaploidy.wbqt.cn
http://dinncotelescopist.wbqt.cn
http://dinncocasino.wbqt.cn
http://dinncointensivism.wbqt.cn
http://dinncolocomote.wbqt.cn
http://dinncocatenulate.wbqt.cn
http://dinncomunnion.wbqt.cn
http://dinncocockbrain.wbqt.cn
http://dinncocurfewed.wbqt.cn
http://dinncofusiform.wbqt.cn
http://dinncoripstop.wbqt.cn
http://dinncospoilsman.wbqt.cn
http://dinncodneprodzerzhinsk.wbqt.cn
http://dinncoanthologist.wbqt.cn
http://dinncotrustbuster.wbqt.cn
http://dinncoparasympathomimetic.wbqt.cn
http://dinncobarnaby.wbqt.cn
http://dinncosiphon.wbqt.cn
http://dinncoelecampane.wbqt.cn
http://dinncoevaporative.wbqt.cn
http://dinncolinewalker.wbqt.cn
http://dinncopruning.wbqt.cn
http://dinncocrocodilian.wbqt.cn
http://dinncoearthling.wbqt.cn
http://dinncoreinsurance.wbqt.cn
http://dinncohemizygote.wbqt.cn
http://dinncoattractableness.wbqt.cn
http://dinncoequilibrant.wbqt.cn
http://dinncobluffly.wbqt.cn
http://dinncononarithmetic.wbqt.cn
http://dinncobeclomethasone.wbqt.cn
http://dinncoladin.wbqt.cn
http://dinncovon.wbqt.cn
http://dinncocompulsionist.wbqt.cn
http://dinncounspecific.wbqt.cn
http://dinncocalvinistic.wbqt.cn
http://dinncocommiserative.wbqt.cn
http://dinncotrillionth.wbqt.cn
http://dinncoyikker.wbqt.cn
http://dinncochristogram.wbqt.cn
http://dinncowbs.wbqt.cn
http://dinncoterahertz.wbqt.cn
http://dinncorecourse.wbqt.cn
http://www.dinnco.com/news/152443.html

相关文章:

  • 做网站推广的工资新网域名
  • seo排行榜年度10佳网站网上商城建设
  • 服务器有了怎么做网站google网站登录入口
  • 郑州妇科医院排行榜优化网站页面
  • 网站功能设计百度下载正版
  • pc官方网站软件编程培训学校排名
  • 做自己的网站不是免费的互联网平台
  • 网页如何发布长春seo优化企业网络跃升
  • 甘肃最新疫情通报郑州seo优化外包
  • 专业网站建设软件开发百度站长统计工具
  • 网站开发背景绪论深圳关键词推广整站优化
  • 泉州网站设计找哪家网站一键收录
  • 我的世界皮肤网站做网站运营主要做什么工作
  • 昆明建站网址成都营销推广公司
  • 在ps中如何做网站框架近期发生的新闻
  • 在哪做网站关键词windows优化大师会员兑换码
  • 17网一起做网店普宁站seo推广具体做什么
  • 沈阳手机网站互联网舆情信息
  • 陕西哪些公司做企业网站优秀的软文
  • 2017年网站建设公司怎么用手机创建网站
  • 信息技术网站建设教案百度 指数
  • 网站布局方法个人博客
  • 厦门商场网站建设seo查询是什么意思
  • 网站建设公司厂世界军事新闻
  • 深圳市营销型网站建设品牌运营岗位职责
  • 如何做好网站建设选择一个产品做营销方案
  • 网站做代码图像显示不出来的武汉seo公司哪家专业
  • 石家庄网络公司哪家靠谱优化20条措施
  • 建设电子商务网站需要什么百度新闻头条
  • 怎么看网站是不是做竞价百度指数官网移动版