C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)
1. 对象的空间在括号开始就已经分配,但是构造在定义对象的时候才会实现,若跳过(譬如goto),到括号结束析构会发生错误,编译会通不过。
2.初始化
1 struct X { int i ; float f; char c;};
2
3 - X x1 = { 1,2.2,'c'};
4 X x2[3] = { {1,1.1,'a'},{2,2.2,'b'} };
5
6
7 struct Y {float f; int i; Y(int a);} ;
8
9 Y y1[] = {Y(1),Y(2),Y(3)};
3. default constructor == 无参数构造函数
Y y2[2] = {Y(4);} //it is wrong
4. new / delete
new : 分配空间+调用构造函数
delete(delete[]): 先析构,后回收空间
int *psome = new int [10]; delete [] psome; //不能失去[]
不要用delete去free不是new的空间
不要对同一个内存空间delete两次
当用new[]时,必须用delete[]
new不带[],delete也不带
对一个空指针delete是安全的(nothing will happen):不确定是否用new时使用
没delete。内存泄漏。
int *p = new int;
int *a = new int[10];
Student *q = new Student();
Student *r = new Student[10]; delete p; //p的地址与大小
a++;delete[] a; //运行错误,找不到new时a的地址
delete q; //回收Student
delete r; //空间收回,析构只做了一个
delete[] r; //析构所有对象
仅在编译时刻
--public:公用的
--private:自己--类的成员函数,同类的两个对象可以互相访问私有变量。
--protected:
Friend: 别的类,别的类里面的函数。
struct X; //前项声明
struct Y{
void f(X*);
};
struct X{
private:
int i;
public:
void initialize();
friend void g(X*,int); //Global friend
friend void Y::f(X*); //Struct member friend
friend void Z; //Entire struct is a friend
friend void h();
};
class vs. struct
class 缺省的是private
struct 缺省的是public
首选class
struct A{
private:
int i;
public:
A:i(0){}
} //与放在里面相比,实现赋值的顺序会早于构造函数被执行
尽量用初始化不用赋值
C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)的更多相关文章
- 动态内存分配(Dynamic memory allocation)
下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题. 尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...
- [Paper翻译]Scalable Lock-Free Dynamic Memory Allocation
原文: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.3870&rep=rep1&type=pdf Abstr ...
- Memory Allocation with COBOL
Generally, the use of a table/array (Static Memory) is most common in COBOL modules in an applicatio ...
- 内存管理(memory allocation内存分配)
Memory management is the act of managing computer memory. The essential requirement of memory manage ...
- A Reusable Aspect for Memory Allocation Checking
The checking logic would be refactored into an aspect file, as follows: after(void * s) : (call($ ma ...
- (转) Dynamic memory
In the programs seen in previous chapters, all memory needs were determined before program executi ...
- Advanced Memory Allocation 内存分配进阶[转]
May 01, 2003 By Gianluca Insolvibile in Embedded Software Call some useful fuctions of the GNU C l ...
- Safe and efficient allocation of memory
Aspects of the present invention are directed at centrally managing the allocation of memory to exec ...
- linux 下tomcat出现 Native memory allocation (malloc) failed to allocate 1915224064 bytes for committing reserved memory问题
## There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocat ...
随机推荐
- Java学习day04
day04-课堂笔记 1.运算符[剩下的运算符] * 赋值运算符 两种类型: 基本赋值运算符:= 扩展的赋值运算符: += -= *= /= %= 它们都是先执行等号右边的表达式,最后再经过运算赋值给 ...
- MySQL计算月份间隔的函数
要求忽视具体日期,即 2020-01-31 与 2020-02-01 的月份间隔为:1 -- 格式必须为: '%Y%m' SELECT PERIOD_DIFF("202008" , ...
- Code Forces 1030E
题目大意: 给你n个数,你可以交换一个数的任意二进制位,问你可以选出多少区间经过操作后异或和是0. 思路分析: 根据题目,很容易知道,对于每个数,我们可以无视它的1在那些位置,只要关注它有几个1即可, ...
- 多测师讲解requests __中_高级讲师肖sir
(1)生成报告 import unittest #导入单元测试框架 import requests #导入接口库 import time # #时间戳,导入time模块 from api.HTMLTe ...
- Java 10 种常用第三方服务
严格意义上说,所有软件的第三方服务都可以自己开发,不过从零到一是需要时间和金钱成本的.就像我们研发芯片,投入了巨大的成本,但仍然没有取得理想的成绩,有些事情并不是一朝一夕,投机取巧就能完成的. Jav ...
- C++虚函数与多继承
虚函数 C++用虚函数实现运行时多态,虚函数的实现是由两个部分组成的,虚函数指针与虚函数表. 虚函数指针(vptr)是指向虚函数表的指针,在一个被实例化的对象中,它总是被存放在该对象的地址首位.而虚函 ...
- bash 括号使用
Bash 括号多种使用方式 ${} 变量初始化 ${param:-string} 若变量param为空或者未定义,则用在命令行中用string来替换${param:-string} 否则变量param ...
- 编程福利:50本C语言电子书,你还怕没书看吗!
推荐适合编程新手入门的几本经典的C语言书籍. 1.<C程序设计语言> C语言之父的著作,被称为C语言的的圣经.全球最经典的C语言教程.这本书最大的特点是精炼.读起来不会觉得"啰嗦 ...
- kafka生产者 消费者
publisher.php <?php $rk = new RdKafka\Producer(); $rk->addBrokers("192.168.33.50"); ...
- selenium 设置代理ip
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("--prox ...