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){}

}  //与放在里面相比,实现赋值的顺序会早于构造函数被执行

尽量用初始化不用赋值

原文地址:https://www.cnblogs.com/jinjin-2018/p/9343955.html

时间: 2024-11-05 15:47:04

C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)的相关文章

Cpp Chapter 12: Classes and Dynamic Memory Allocation Part1

12.1 Dynamic memory and classes 12.1.1 A review example and static class members Now try implement a String class(a flawed one): // strngbad.h -- flawed string class definition #include <iostream> #ifndef STRNGBAD_H_INCLUDED #define STRNGBAD_H_INCLU

Cpp Chapter 12: Classes and Dynamic Memory Allocation Part2

12.3 Things to remember when using new in constructors ) If you use new in constructors, use delete in destructor. Their use should be compatible, pair new with delete and new [] with delete [] ) Multiple constructors should share the same way of new

[Paper翻译]Scalable Lock-Free Dynamic Memory Allocation

原文: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.3870&rep=rep1&type=pdf Abstract 动态内存分配器(malloc/free)在多线程环境下依靠互斥锁来保护共享数据的一致性.使用锁在性能,可用性,健壮性,程序灵活性方面有很多缺点.Lock-free的内存分配器能消除线程延迟或被杀死以及CPU的调度策略对程序的性能影响.这篇paper呈上了一个完整的无锁内存分配器.它的实现只使用被广泛支

A Reusable Aspect for Memory Allocation Checking

The checking logic would be refactored into an aspect file, as follows: after(void * s) : (call($ malloc(...)) || call($ calloc(...)) || call($ realloc(...))) && result(s) { char * result = (char *)(s); if (result == NULL) { /* routine to handle t

Safe and efficient allocation of memory

Aspects of the present invention are directed at centrally managing the allocation of memory to executable images in a way that inhibits malware from identifying the location of the executable image. Moreover, performance improvements are implemented

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

May 01, 2003  By Gianluca Insolvibile in Embedded Software Call some useful fuctions of the GNU C library to save precious memory and to find nasty bugs. Dealing with dynamic memory traditionally has been one of the most awkward issues of C and C++ p

PatentTips - Modified buddy system memory allocation

BACKGROUND Memory allocation systems assign blocks of memory on request. A memory allocation system employs an allocator to receive relatively large blocks of memory from an operating system and allocate that memory to satisfy memory requests. Upon r

C++ TUTORIAL - MEMORY ALLOCATION - 2016

http://www.bogotobogo.com/cplusplus/memoryallocation.php Variables and Memory Variables represent storage space in the computer's memory. Each variable presents a convenient names like number or result in the source code. Behind the scenes at runtime

Native memory allocation (mmap) failed to map 142606336 bytes for committing reserved memory.

这里写链接内容 问题描述 Java程序运行过程中抛出java.lang.OutOfMemoryError: unable to create new native thread,如下所示: [java] view plain copy java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(T