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 ...
随机推荐
- selenium学习之基本操作(一)
通过selenium的使用可以驱动浏览器来模拟加载网页,简单定位元素和获取对应的数据:# find_elements_by_id #(根据id属性值获取元素列表)# find_elements_by_ ...
- RTKLIB的主要功能
RTKLIB是全球导航卫星系统GNSS(global navigation satellite system)的标准&精密定位开源程序包,RTKLIB由日本东京海洋大学(Tokyo Unive ...
- 【题解】[USACO13FEB]Tractor S
题目戳我 \(\text{Solution:}\) 好久没写啥\(dfs\)了,借这个题整理下细节. 观察到答案具有二分性,所以先求出其差的最大最小值,\(\log val\)的复杂度不成问题. 考虑 ...
- ESP8266 玩板记
一.前言 esp8266的玩板记,后面应该会去更一些其他东西,这一块内容,这算是收官之战了. IoT,江湖有缘再相会 二.ESP8266实现WiFi杀手/钓鱼 这次的博客做的是一个娱乐性较强的项目. ...
- SQL数据库删除和还原的时候提示 被占用 记录一下
设置离线 1)ALTER DATABASE [datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE设置在线 2)ALTER DATABASE [datebase] ...
- Tomcat配置Gizp 客户端使用okHttp3
找到tomcat 在 server.xml 新增如下配置 <Connector connectionTimeout="20000" port="8088" ...
- Linux最常用的命令大全
Linux最常用的命令大全 按功能索引 目录处理命令 ls mkdir pwd cd rmdir cp mv rm 文件处理命令 touch cat tac more less head tail l ...
- MarkDown语法记录,还在用word,txt编写项目文档吗?
开始之前 是不是在github上看项目的时候第一眼就要看项目介绍? 是不是经常在某些项目的代码里面看到一个README.MD文档 却不知道怎么写? 你是不是不知道,反正我是的. 作为一个程序员,可能写 ...
- 使用 volatile 关键字保证变量可见性和禁止指令重排序
volatile 概述 volatile 是 Java 提供的一种轻量级的同步机制.相比于传统的 synchronize,虽然 volatile 能实现的同步性要差一些,但开销更低,因为它不会引起频繁 ...
- 很多人都搞不清楚C语言和C++的关系!今天我们来一探究竟为大家解惑~
最近,身边有许多小伙伴已经开始学习编程了,但是呢,学习又会碰到许多的问题,其中作为新手小白提到最多的问题就是编程语言的选择. 每次遇到这种问题,看起来很简单,但是又有很多小伙伴搞不清编程语言之间的关系 ...