1. 对象的空间在括号开始就已经分配,但是构造在定义对象的时候才会实现,若跳过(譬如goto),到括号结束析构会发生错误,编译会通不过。

2.初始化

  1. 1 struct X { int i ; float f; char c;};
  2. 2
  3. 3 - X x1 = { 1,2.2,'c'};
  4. 4 X x2[3] = { {1,1.1,'a'},{2,2.2,'b'} };
  5. 5
  6. 6
  7. 7 struct Y {float f; int i; Y(int a);} ;
  8. 8
  9. 9 Y y1[] = {Y(1),Y(2),Y(3)};

3.  default constructor  == 无参数构造函数

  1. Y y2[2] = {Y(4);} //it is wrong

4. new / delete

new :  分配空间+调用构造函数

delete(delete[]): 先析构,后回收空间

  1. int *psome = new int [10];
  2.  
  3. delete [] psome; //不能失去[]

不要用delete去free不是new的空间

不要对同一个内存空间delete两次

当用new[]时,必须用delete[]

new不带[],delete也不带

对一个空指针delete是安全的(nothing will happen):不确定是否用new时使用

没delete。内存泄漏。

  1. int *p = new int;
  2. int *a = new int[10];
  3. Student *q = new Student();
  4. Student *r = new Student[10];
  5.  
  6. delete p; //p的地址与大小
  7. a++;delete[] a; //运行错误,找不到new时a的地址
  8. delete q; //回收Student
  9. delete r; //空间收回,析构只做了一个
  10. delete[] r; //析构所有对象

仅在编译时刻

--public:公用的

--private:自己--类的成员函数,同类的两个对象可以互相访问私有变量。

--protected:

Friend:  别的类,别的类里面的函数。

  1. struct X; //前项声明
  2.  
  3. struct Y{
  4. void f(X*);
  5. };
  6.  
  7. struct X{
  8. private:
  9. int i;
  10. public:
  11. void initialize();
  12. friend void g(X*,int); //Global friend
  13. friend void Y::f(X*); //Struct member friend
  14. friend void Z; //Entire struct is a friend
  15. friend void h();
  16. };

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)的更多相关文章

  1. 动态内存分配(Dynamic memory allocation)

    下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题.    尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...

  2. [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 ...

  3. Memory Allocation with COBOL

    Generally, the use of a table/array (Static Memory) is most common in COBOL modules in an applicatio ...

  4. 内存管理(memory allocation内存分配)

    Memory management is the act of managing computer memory. The essential requirement of memory manage ...

  5. A Reusable Aspect for Memory Allocation Checking

    The checking logic would be refactored into an aspect file, as follows: after(void * s) : (call($ ma ...

  6. (转) Dynamic memory

      In the programs seen in previous chapters, all memory needs were determined before program executi ...

  7. Advanced Memory Allocation 内存分配进阶[转]

    May 01, 2003  By Gianluca Insolvibile  in Embedded Software Call some useful fuctions of the GNU C l ...

  8. Safe and efficient allocation of memory

    Aspects of the present invention are directed at centrally managing the allocation of memory to exec ...

  9. 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 ...

随机推荐

  1. C++系列教程

    C++系列教程: 本人是一个高二狗C++小白,之前徘徊在Python和易语言等一些语言之间,这是我几天学习收获的结果,该教程是我自己搜集整理,再加上自己对C++的理解编写的,也是一个偏经验类型的,希望 ...

  2. std(标准库)和STL(标准模板库)的关系

    C++标准库的内容分为10类: C1.语言支持 C2.输入/输出 C3.诊断 C4.一般工具 C5.字符串 C6.容器 C7.迭代器支持 C8.算法 C9.数值操作 C10.本地化: 下面分类详解: ...

  3. 一种基于均值不等式的Listwise损失函数

    一种基于均值不等式的Listwise损失函数 1 前言 1.1 Learning to Rank 简介 Learning to Rank (LTR) , 也被叫做排序学习, 是搜索中的重要技术, 其目 ...

  4. 【题解】 [GZOI2017]小z玩游戏

    题目戳我 \(\text{Solution:}\) 考虑建图.操作可以看作对\(1\)进行的操作,于是有以下运行过程: \(1\to w[i]\to e[i]\to...\) 考虑倍数,一个数可以走到 ...

  5. vue超出8个字符,显示省略号

    显示的数据

  6. linux 路径结构

    /bin /boot /data /dev /etc /home /lib /lib64 /lost+found /media /mnt /opt /proc /root /run /sbin /sr ...

  7. C# excel文件导入导出

    欢迎关注微信公众号 C#编程大全 这里有更多入门级实例帮你快速成长 在C#交流群里,看到很多小伙伴在excel数据导入导出到C#界面上存在疑惑,所以今天专门做了这个主题,希望大家有所收获! 环境:wi ...

  8. object-fit 详解

    contain 被替换的内容将被缩放,以在填充元素的内容框时保持其宽高比. 整个对象在填充盒子的同时保留其长宽比,因此如果宽高比与框的宽高比不匹配,该对象将被添加"黑边". cov ...

  9. git 查看本地分支和切换本地分支的命令

    查看本地分支,和当前所在的分支 git branch -vv git checkout developer 切换到developer分支

  10. 多测师接口测试 --常见的接口面试题目002---高级讲师肖sir

      1.postman接口测试,它有一个功能可以设置参数化,你有用过吗 2.你测试过哪些接口 3.有做过接口测试吗?接口测试你们是怎么测的 4.多接口怎么测(postman里面有一个批量处理) 5.g ...