C++之new再探究
相关博文:C++之new和delete探究
一.C和C语言申请内存
例1
附例1代码:
//小问学编程
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{//一.申请一个变量内存//1.1 C语言中int* pCNum=(int*)malloc(sizeof(int));free(pCNum);//1.2 C++中int* pCppNum=new int;delete pCppNum;//二.申请一段内存//2.1 C语言中int* pcArray=(int*)malloc(sizeof(int)*3);free(pcArray);//2.2 C++中int* pcppArry=new int[3];delete[] pcppArry;//三.申请并初始化//3.1 C语言中int* pAlloc=(int*)calloc(sizeof(int),3);if(pAlloc==nullptr)return 0;cout<<pAlloc[0]<<pAlloc[1]<<pAlloc[2]<<endl;free(pAlloc);//3.2 C++中——单个数据初始化()int* pNum=new int(100);cout<<*pNum<<endl;delete pNum;pNum=nullptr;//3.2 C++中——多个数据初始化{}int* pArray=new int[3]{1,2,3};cout<<pArray[0]<<pArray[1]<<pArray[2]<<endl;delete[] pArray;pArray=nullptr;return 0;
}
二.memory操作
例2
附例2代码
//小问学编程
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<assert.h>
#include<cstring>
using namespace std;
int main()
{//四.内存池:允许我们在申请的内存上再做申请操作char* Memory = new char[1024];assert(Memory);int* pInt = new(Memory + 0)int[3]{1,2,3};//12字节存储整数assert(pInt);char* pChar = new(Memory + 12)char[20]{ "白日依山尽!" };assert(pChar);cout << pInt[0] << pInt[1] << pInt[2] << endl;cout << ((int*)Memory)[0] << ((int*)Memory)[1] << ((int*)Memory)[2] << endl;cout << pChar << endl;cout << Memory + 12 << endl;delete[] Memory;Memory = nullptr;return 0;
}
三.在栈上和堆上返回值区别
例3
附例3代码:
#include<iostream>
using namespace std;int fun1()
{int* x=new int(3);return *x;
}int fun2()
{int x=3;return x;
}int main()
{cout<<fun1()<<endl;cout<<fun2()<<endl;
}
C++之new再探究相关推荐
- C++新特性探究(13.6):右值引用再探究
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
- C++新特性探究(八):初始化列表(Initialization List)再探究
相关博文:C++新特性探究(七):初始化列表(Initialization List) 相关博文:C++新特性探究(八):初始化列表(Initialization List)再探究 相关博文:C++之 ...
- C++之string字符串类再探究
前文:C++之char和string字符串类探究 string是C++中处理字符串的类,是对C语言中字符串的数据和行为的包装.使字符串的处理,更简单易用. 一. string的初始化方式 例1: ...
- 数字滤波器的实现——低通滤波器再探究
在探究完滤波器原理之后,又面临一个问题就是数字滤波器如何实现的问题,因为在实际应用过程中,如果不接触硬件的话,低通滤波器一般都是通过编程实现的,具体代码应该怎么编写,在应用过程中又应该注意什么问题呢? ...
- [iOS - Block基础再探究]
文章目录 前言 1. Block简介 2. 语法 初始化和声明 3. Block类型变量 typedef 截获自动变量 --block 截获的自动变量 4.block的实现 4.1 Block的存储域 ...
- Vue Tab 组件再探究
初学 Vue 的时候,发现用 Vue 来写 Tab 组件是如此简单,利用"数据驱动"的思路还真和 js 控制 dom 不一样.请见下面第一版的代码,没有 js dom 那样 for ...
- C++之new和delete探究
相关博文:C++之new再探究 一.C语言中分配堆上的空间,不会调用构造和析构: 上例代码编译会报错! 附上例代码: //小问学编程 #include<stdlib.h> #inclu ...
- C++新特性探究(13.5):右值引用
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
- C++新特性探究(十六):move constructor移动构造
相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...
最新文章
- 【CentOS 7】关于php留言本网站的搭建
- Spring boot程序入口
- delphi下的MVC架构-eMVC
- vue-router 快速入门
- 《Python Cookbook 3rd》笔记(3.3):数字的格式化输出
- Raspbian 中国软件源
- 开发者的福利,报名即可领取代金券,赢运动手环
- cpu压力测试 Android,Android App 压力测试方法(Monkey)
- JavaScript中实现深浅拷贝
- 配置 mysql/mariadb sphinxSE 错误解决 1st column must be bigint to accept 64-bit DOCID
- WGS84 UTM 坐标系及UTM投影分带标准
- jquery,Ajax判断验证码是否正确,不正确阻止提交
- 已知等价关系求商集_等价关系、商集和集合的划分.pdf
- Web 前端本地化(一)
- 把读书当做信仰的民族:犹太民族
- activiti-邮件任务
- 一文读懂keepalive的工作原理
- 你的安卓手机为什么会卡-Android混乱生态的缺位者:推送
- 视频编码之I帧、P帧、B帧
- C语言指针基础与深入