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 ...
随机推荐
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
- 玩转Libmodbus(二) 写代码体验
libmodbus在线文档 https://www.jianshu.com/p/d93c17485c0a 原创篇 参考上一篇转载的博客,我的ubuntu上的最终生成的动态库的路径,下图所示 我的lin ...
- [VBA原创源代码] excelhome 汇总多工作表花名册
生病了,一点一滴的积累,慢慢康复,今年十月,我就 2 周岁了. 以下代码完成了excelhome中留的作业 http://club.excelhome.net/forum.php?mod=viewth ...
- 06 解决Sublime Text3输入法不跟随的问题
安装原生的Sublime, 输入法是不会跟随Sublime的编译文件页面的,会失去焦点,这样写代码写文档时看起来会十分不方便,参考了一些资料,下载插件做了配置,已经在自己机器上用百度输入法测试成功,记 ...
- C++ “string”: 未声明的标识符
转载:https://blog.csdn.net/kkkmmmjjjj/article/details/53780549 解决方案: 要添加using namespace std;语句在宏定义后面. ...
- 什么是ICD文件
ICD就是IED Capability Description的简称,中文为IED能力描述文件.其中 IED是Intelligent Electronic Device的简称,是智能电子设备 智能电子 ...
- 多测师讲解 自动化测试理论(1)_高级讲师肖sir
自动化测试理论什么是自动化测试?广义的:通过工具或程序替代或辅助人工测试的行为叫自动化测试狭义的:通过工具录制或编写脚本模拟手工测试的过程,通过回放或运行脚本执行测试用例,从而代替人工对系统的功能验证 ...
- Java代码实现计算器加减乘除简易功能
package test; import javax.swing.; import java.awt.; import java.awt.event.KeyAdapter; import java.a ...
- C语言实现表达式求值,支持+、-、*、/四则运算,并且支持多级括号,自定义了栈的操作。
以下是代码的实现使用gcc已经成功运行了,下面是效果图 #include <stdio.h> #include <stdlib.h> #define OPT_ADD 43 /* ...
- cmake引入三方库
目录结构 . |-- cmake | |-- CompilerSettings.cmake | |-- Options.cmake | `-- ProjectJsonCpp.cmake |-- CMa ...