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 ...
 
随机推荐
- 软件定义网络实验记录①--Mininet 源码安装和可视化拓扑工具
			
第一步: 在 Ubuntu 系统的 home 目录下创建一个目录,目录名为自己的标识,包括但 不限于学号.姓名拼音等,目录不要包含中文. 第二步: 在创建的目录下,完成 Mininet 的源码安装. ...
 - JavaScript实时显示当前时间
			
1.HTML部分 <div id="div1">显示当前时间!</div> 2.css部分 #div1 { width: 700px; height: 50 ...
 - leaflet如何加载10万数据
			
作为一名GIS开发者,你工作中一定遇到过这种问题,根据业务设计,需要在地图上添加1万+条数据,数据或是点.或是线.或是面.但不管哪种,当你添加到5000条时,地图操作就会出现明显的卡顿.当你添加超过1 ...
 - 多测师讲解selenium--常用关键字归纳-_高级讲师肖sir
			
常见的定位方式: 1.通过id定位 id=kw 2.通过name定位 name=wd 3.通过xpath相对路径定位:xpath=//*[@id="kw"] 4.通过两个属性值定位 ...
 - CentOS 7系统常见快捷键操作方式
			
快捷键操作方式 Linux系统中一些常见的快捷方式,可有效提高操作效率,在某些时刻也能避免操作失误带来的问题. 最有用的快捷键 序号 快捷键 官方说明 掌握程度 01 Tab 命令或路径等的补全键 移 ...
 - C++里面类和对象是什么意思?
			
本文章向大家介绍C++类和对象到底是什么意思?,主要包括C++类和对象到底是什么意思?使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下. C++ 是一门 ...
 - 洛谷 CF1012C Hills(动态规划)
			
题目大意: 有几座山,如果一座山左右两边的山比它矮,那么可以在这个山上建房子,你有一台挖掘机,每天可以挖一座山一米,问你需要花多少代价可以分别盖1.2.3--座房子.(给出山的数量,以及每座山的高度) ...
 - swoft 切面AOP尝试
			
官网文档 https://www.swoft.org/documents/v2/basic-components/aop/ 视频教程 https://www.bilibili.com/video/BV ...
 - UI设计学习总结
			
UI设计学习总结 平面设计基础 平面构成 三大构成:点线面 重复构成 相同,有规律的重复 近似构成 形状,大小,色彩,肌理相似 渐变构成 色彩逐渐变化 发射构成 通过一点向四周扩散犹如绽放的花朵 密集 ...
 - Django采坑日志(django2.0)
			
使用Mariadb时出现的问题 "Unknown system variable 'transaction_isolation'" 解决办法:修改django/db/backend ...