当前位置:网站首页>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
边栏推荐
- Jenkins construction and use
- 联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
- Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
- 19c environment ora-01035 login error handling
- Special test 05 · double integral [Li Yanfang's whole class]
- ACFs file system creation, expansion, reduction and other configuration steps
- Analysis of unused index columns caused by implicit conversion of timestamp
- [code analysis (1)] communication efficient learning of deep networks from decentralized data
- Tensorflow & pytorch common error reporting
- Get the attribute value difference between two different objects with reflection and annotation
猜你喜欢

Express ② (routage)

Handling of high usage of Oracle undo

About note 1

Dynamic subset division problem

Leetcode brush question 𞓜 13 Roman numeral to integer

Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计

The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table

SQL learning window function

JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes

UML Unified Modeling Language
随机推荐
Express ② (routage)
【vmware】vmware tools 地址
Apache Atlas Compilation and installation records
freeCodeCamp----arithmetic_ Arranger exercise
Atcoder beginer contest 248c dice sum (generating function)
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
Chapter 15 new technologies of software engineering
Choreographer full resolution
China creates vast research infrastructure to support ambitious climate goals
Dolphin scheduler configuring dataX pit records
Express middleware ③ (custom Middleware)
Dolphin scheduler scheduling spark task stepping record
Express中间件③(自定义中间件)
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
Leetcode | 38 appearance array
第一章 电商秒杀商品回顾
[code analysis (1)] communication efficient learning of deep networks from decentralized data
Introduction to spark basic operation
Oracle RAC database instance startup exception analysis IPC send timeout
Record a strange bug: component copy after cache component jump