当前位置:网站首页>New关键字的学习和总结
New关键字的学习和总结
2022-04-23 13:59:00 【面试被拒1万次】
new运算符
在复习c和复习c++中,总是把new关键字做简单的分配内存理解,显然是不够的。
首先我看借鉴了两篇博客.
new
《c++ primer plus》
- 指针的真正的用武之地在于动态内存分配,在运行期间分配未命名的内存空间
- typeName* pointer_name = new typeName;
typeName是告诉编译器,用来指定需要什么样(多大的)的内存空间和声明什么样的指针- new 和 Delete 必须配套使用,释放堆或者自由存储区申请的内存
静态联编 和 动态联编
定义静态联编
: 在大型数据中,比如数组,如果在声明的时候创建内存,不管用不用,它一直占用内存,像这种在编译期间分配内存的称为静态链编
> int tacos[10];
定义动态联编
:使用new时,如果在运行阶段需要数组,指定数组长度来分配内存,这种在运行期间确定内存空间的叫做动态联编
int* pz = new int[10];
delete [] pz;
在实际项目中,一些频繁使用的内存空间,并不会释放,在使用完毕之后初始化,等待下次激活赋值之后使用
new 和 malloc的区别
- malloc和free 是c语言的标准库函数,new 和 delete 是运算符
- 为什么要增加new和delete?
在非内置数据类型(class 类)的对象而言 c++内置类型,需要调用构造函数和析构函数。
new 创建内存的对象的同时调用对象的构造函数。
delete 在清理内存的时候调用内存的析构函数。(这非常重要) - 默认使用new[]分配的内存,要交给 delete[] 去释放
- 默认operator new()分配的内存,要交给operator delete去释放
new 创建对象
class Person {
int age ;
public:
Person(/* args */){
age = 0;
cout << " 默认构造函數 " << endl;
} ;
Person(int val):age(val) { cout << " 有参构造函數 " << endl; };
int getPerson() const { return age; };
~Person(){};
};
int main()
{
int ret = 0;
Person tep = Person(); // 栈区,默认构造
Person* pa = new Person; //不带括号,堆区 ,默认构造
ret = pa->getPerson();
delete pa;
Person* pc = new Person(); // 带括号,堆区,默认构造 ,并不是带括号就默认有参构造
ret = pc->getPerson();
delete pa;
Person* pb = new Person(100); // 有参构造
ret = pb->getPerson();
delete pb;
}
new 创建对象的指针数组
在看项目代码的时候,创建了一个TcpScoket[m_nSocketNum]的对象数组,每个元素都是TcpScoke对象,
所以如果创建一个指针数组,就可以在堆区创建这些节点(在项目使用的时候,这些空间都不会释放,区别一
个在栈上,一个在堆上而已),用new 创建指针数组应该怎么创建呢?
class Person{ };
int main()
{
int nSize = 10;
Person* pa = new Person[nSize]; // 开辟一块数组元素为Person的内存空间,
// 开辟Person 的指针数组,怎么开辟呢
// 这里不能写成 new (Person*)[nSize] ,也不能 写成new Person(*[nSize])
Person** pb = new Person*[nSize]; // 开辟一个数组元素都是 (Person*)的内存空间
for( int i=0; i<nSize ;i++)
{
pb[i] = new Person(); // 给Person 开辟空间
}
//释放空间
for( int i=0; i<nSize ;i++)
{
delete pb[i];
}
delete[] pb;
}
版权声明
本文为[面试被拒1万次]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_38023160/article/details/121707395
边栏推荐
- 3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
- [code analysis (4)] communication efficient learning of deep networks from decentralized data
- Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
- Express②(路由)
- Apache Atlas Compilation and installation records
- scikit-learn构建模型的万能模板
- SSM project deployed in Alibaba cloud
- 第十五章 软件工程新技术
- Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
- Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
猜你喜欢
JS 烧脑面试题大赏
蓝绿发布、滚动发布、灰度发布,有什么区别?
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
【项目】小帽外卖(八)
SQL learning | set operation
Leetcode? The first common node of two linked lists
Express中间件③(自定义中间件)
33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Express ② (routage)
随机推荐
Force deduction brush question 101 Symmetric binary tree
leetcode--357. 统计各位数字都不同的数字个数
Es introduction learning notes
Solution of discarding evaluate function in surprise Library
Wechat applet
L2-024 部落 (25 分)
聯想拯救者Y9000X 2020
Apache seatunnel 2.1.0 deployment and stepping on the pit
Troubleshooting of expdp export error when Oracle table has logical bad blocks
初探 Lambda Powertools TypeScript
The latest development of fed digital currency
freeCodeCamp----arithmetic_ Arranger exercise
Leetcode brush question 897 incremental sequential search tree
10g database cannot be started when using large memory host
[code analysis (1)] communication efficient learning of deep networks from decentralized data
Dolphin scheduler source package Src tar. GZ decompression problem
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
Tensorflow Download
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table