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学习day03
day03 课堂笔记 1.数据类型 2.总结第二章到目前为止所学内容: * 标识符 * 关键字 * 字面值 * 变量 成员变量如果没有赋值,系统会自动赋值,而局部变量不手动赋值,则会编译不通过. * ...
- python单元测试框架pytest
首先祝大家国庆节日快乐,这个假期因为我老婆要考注会,我也跟着天天去图书馆学了几天,学习的感觉还是非常不错的,这是一篇总结. 这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python ...
- SQL学习 小知识点(一)
这是我在做实验的时候总结的,随着学习可能会做更多的小总结 ssms上面把所有代码写下来后一块执行是行不通的(极大可能会报错),推测可能是因为ssms先对代码的语法进行了检查. --My expe ...
- shell-批量修改文件名及扩展名多案例
1. 功能描述如下表: 批量文件改名案例实战: 问题1: 创建测试数据 [root@1-241 tmp]# for i in `seq 6`;do touch stu_161226_${i}_fin ...
- Makefile-3-书写规则
目录 前言 概念 Chapter 3:书写规则 3.3 在规则中使用通配符 3.4 文件搜索 3.8 自动生成依赖性 原理 * 直接解析例子 ** sed 命令 参考 书籍 前言 本笔记主要记录Mak ...
- 多测师讲解requests __中_高级讲师肖sir
(1)生成报告 import unittest #导入单元测试框架 import requests #导入接口库 import time # #时间戳,导入time模块 from api.HTMLTe ...
- 多测师讲解自动化测试 _pybot.bat批处理脚本_高级讲师肖sir
- 多测师讲解性能测试_面试题_001高级讲师肖sir
什么叫做性能测试?1. 软件的性能是软件的一种非功能特性,它关注的不是软件是否能够完成特定的功能,所以一般来说性能测试介入的时机是在功能测试完成之后.另外,由定义中的及时性可知性能也是一种指标,可以 ...
- P2340 [USACO03FALL]Cow Exhibition G题解
新的奇巧淫技 原题传送门 众所周知,模拟退火是一种很强大的算法,DP很强,但我模拟退火也不虚,很多题你如果不会的话基本可以拿来水很多分.比如这道题,我用模拟退火可以轻松水过(虽然我是足足交了两页才过) ...
- 2020年sublime text3可用的注册码
2020年sublime text3可用的注册码 最近终于找到可以用的sublime text3注册码了,仅供各位同学交流学习使用.用完请删除,购买正版! ----- BEGIN LICENSE -- ...