Always try to use two pointers before backtracking or dynamic programing:

cause it will avoid reuse of data and reach good performance


how to create a vector

1
2
v = [[[] for _ in range(4)]for _ in range(4)] # init a blank vector 
# basically it is quite good to store and read data via v[i][j]

Directly read/ wirte is quite different from append()

append() will treat elements as data, which is more flexible

dirctly read/ write v[][] will ensure it as an aligned vector

Read more »

德国古典哲学的前提

近代以来西方文明的转型

从博物到系统的科学

牛顿-《自然哲学背后的数学原理》

自然科学繁荣背后的秘密

自然科学基础

假设1:自然界的规律确实存在

假设2:规律可以被人认识

本体论->认识论

唯理论与经验论的争论

唯理派(欧陆):哲学-找到无前提的根据 存在/是Being

经验派(英美)(休谟):质疑因果律,非必然而是习惯

Read more »

今天写作业被这个const搞得很自闭,因此来总结一下,避免以后踩坑(此处不一定是int,可以是任意type)

const这个东西对于一般变量很简单,const int a and const a int are totally same. Both of them tell the compiler that the value stored in a can not be modified. But when it is combined with pointer, things go complicated

const int *

For C++, int* is not viewed as a type. Only *a be viewed as a mark telling the complier a is a pointer. So as we have already known that int const is same with const int, int const *a is also same with const int *a. Both of them means *a is a const, which means the value poined by a can not be modified but a, as a pointer, can point to different places.

int* const

By contrast, the const in int* const is a mark for a, so a, as a pointer, can only point to one specific place(determined when it is initialized), but the value pointed by it can be changed.

Besides, we can combine two usages of consttogether: const int* const a--a, as a pointer, can not be modified and the value pointed by a cannot be modified.

今天写作业的时候要求实现vector,很自然地用 malloc,但是告知不能使用stdlib.h,没有办法就只能使用new了。于是忽然想到newmalloc都是在heap上开辟动态空间,那么有什么异同呢?

malloc()

malloc只负责开辟一个指定大小的空间,类似的calloc则使得在开辟空间的同时初始化为0。 malloc的特点在于将内存空间视为链表,在内存上找到足够长的初始空间返回给目标空间的开头地址,如果失败就会返回NULL(因此使用malloc必须要检验返回指针)。

由于malloc开辟的空间在heap上,这就使得可以在函数内定义的变量在函数被销毁会变量不会被同步销毁。因此释放heap的空间只能通过手动free()

malloc在开辟新空间时往往会多开辟一些空间用于存储空间长度,前后空间指针的信息,由free源码可见,实际上free是将heap上的可用空间定义为指针传入的指针位置减去malloc所要求的大小--所以free本质上是定义了一个指针告诉OS:一个指针到另一个指针之间的空间是可用的free->is_available = 1;。由此free函数对传入的指针要求非常严格,必须是malloc空间的起始指针。

new()

intuitive的区别: 首先new有个好处可以简单地进行初始化,即使用new时会调用该类型的默认构造函数。并且new开辟的空间需要用delete函数删除。

更重要的区别是:

new函数调用的空间在C++被抽象为free store(反向定义--new的空间才被视为free store,由OS特别管理)上。free store一般都是在heap上,但是取决于new自身的实现,也可以在static区域。

new有多种变体,如定位new--直接使用传入的地址而不考虑是否该地址为空可以使用new (address) int[10]

new函数返回的指针类型与开辟空间的数据类型相符合,这就意味着不需要像malloc一样在cast后才能使用,在数据上更加安全。

new如果分配失败,不会返回错误会直接丢出异常,如果想要确认需要使用catch

1
2
3
4
5
6
7
8
try
{
int *a = new int();
}
catch (bad_alloc)
{
...
}

summary:

特征 new/delete malloc/free
分配内存的位置 自由存储区
内存分配成功返回值 完整类型指针 void*
内存分配失败返回值 默认抛出异常 返回NULL
分配内存的大小 由编译器根据类型计算得出 必须显式指定字节数
处理数组 有处理数组的new版本new[] 需要用户计算数组的大小后进行内存分配
已分配内存的扩充 无法直观地处理 使用realloc简单完成
是否相互调用 可以,看具体的operator new/delete实现 不可调用new
分配内存时内存不足 客户能够指定处理函数或重新制定分配器 无法通过用户代码进行处理
函数重载 允许 不允许
构造函数与析构函数 调用 不调用

Just a note for myself !

For C/C++: -fopenmp not -openmp !!!

Actually what I want to mention is the new type, reference, introduced via c++, but here I just use int&as an example.

int

it is apperantly an integer type we are quite familiar with.

int*

This is a type of int pointer. Since it points at int, so it will read 4 byte to represent an integer and return the first byte. (In C, we do not take it as a type, it is something kinda like an address, and it it presented as int *p)

int&

int& is a reference to a pointer. Literally it is just a 'nickname' of an integer. int a; int& p = a;p can be used just like an integer with all operations.

So why reference is introduced?

A: if we do not use reference, there will be something wrong with passing values to function (still we can still solve it via pointer). For example, in a vector class, we have a function that will return a modifiable value of an entry. So we have to introduce reference.

Note: reference is just another name of its origianl type, so it will not allocate a new memory space for it, thus an reference without initilizatin is not permitted, int& p; // error

const iterator

Literally, it is a const iterator, which means: iterator, as a pointer, can not be modified, but the value pointed by this iterator is permitted to be modified.

const_iterator

const_iteratoris a specially defined class. It is defined to go through a const container, which we are not intended to modify.

看到网上有一篇文章用汽车收费站来解释这三个概念,想法很好,但是感觉没说那么清楚,于是我自己来说一遍

0. 一般情况

想象一下你是公路的设计者,现在要对车子进行收费,你怎么办?

最一般的情况就很简单了:让所有车子排一排,依次通过一个收费站,结束。

Read more »
0%