1.malloc(size)、calloc(numElements,sizeOfElement)
calloc会把空间里的值置为0
2.new、delete
int* p1 = new int[10];//这个是分配10个int长度的空间
int* p2 = new int(10);//这个是分配1个int长度的空间并初始化为10.....Orz
3.几种哈希算法(主要用于加密跟检验)
class
GeneralHashFunctionLibrary
{
/*RSHash*/
public
long
RSHash(String str)
{
int
b = 378551;
int
a = 63689;
long
hash = 0;
for
(
int
i = 0; i < str.length(); i++)
{
hash = hash * a + str.charAt(i);
a = a * b;
}
return
hash;
}
/*JSHash*/
public
long
JSHash(String str)
{
long
hash = 1315423911;
for
(
int
i = 0; i < str.length(); i++)
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
return
hash;
}
/*PJWHash*/
public
long
PJWHash(String str)
{
long
BitsInUnsignedInt = (
long
)(4 * 8);
long
ThreeQuarters = (
long
)((BitsInUnsignedInt * 3) / 4);
long
OneEighth = (
long
)(BitsInUnsignedInt / 8);
long
HighBits = (
long
)(0xFFFFFFFF)<<(BitsInUnsignedInt-OneEighth);
long
hash = 0;
long
test = 0;
for
(
int
i = 0; i < str.length(); i++)
{
hash = (hash << OneEighth) + str.charAt(i);
if
((test = hash & HighBits) != 0)
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
return
hash;
}
/*ELFHash*/
public
long
ELFHash(String str)
{
long
hash = 0;
long
x = 0;
for
(
int
i = 0; i < str.length(); i++)
{
hash = (hash << 4) + str.charAt(i);
if
(( x = hash & 0xF0000000L) != 0)
hash ^= ( x >> 24);
hash &= ~x;
}
return
hash;
}
/*BKDRHash*/
public
long
BKDRHash(String str)
{
long
seed = 131;
//31131131313131131313etc..
long
hash = 0;
for
(
int
i = 0; i < str.length(); i++)
hash = (hash * seed) + str.charAt(i);
return
hash;
}
/*SDBMHash*/
public
long
SDBMHash(String str)
{
long
hash = 0;
for
(
int
i = 0; i < str.length(); i++)
hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
return
hash;
}
/*DJBHash*/
public
long
DJBHash(String str)
{
long
hash = 5381;
for
(
int
i = 0; i < str.length(); i++)
hash = ((hash << 5) + hash) + str.charAt(i);
return
hash;
}
/*DEKHash*/
public
long
DEKHash(String str)
{
long
hash = str.length();
for
(
int
i = 0; i < str.length(); i++)
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
return
hash;
}
/*BPHash*/
public
long
BPHash(String str)
{
long
hash=0;
for
(
int
i = 0;i < str.length(); i++)
hash = hash << 7 ^ str.charAt(i);
return
hash;
}
/*FNVHash*/
public
long
FNVHash(String str)
{
long
fnv_prime = 0x811C9DC5;
long
hash = 0;
for
(
int
i = 0; i < str.length(); i++)
{
hash *= fnv_prime;
hash ^= str.charAt(i);
}
return
hash;
}
/*APHash*/
long
APHash(String str)
{
long
hash = 0xAAAAAAAA;
for
(
int
i = 0; i < str.length(); i++)
{
if
((i & 1) == 0)
hash ^=((hash << 7) ^ str.charAt(i) ^ (hash >> 3));
else
hash ^= (~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));
}
return
hash;
}
}
4.传说中的C++内存泄露
常见的情况就是内存在调用完后没有delete,或者delete之前指针离开了内存,如:
int* p = new int(10);
p++;//侧漏了=。=
delete p;
p = NULL; //不用了置空是好习惯
5.for()从小到大循环跟从大到小循环有时候会产生不同的结果,嗯,就是这样。
6.字符数组的实际长度会比理想长度大一,原因是最后一位会自动储存结束标志'\0'。
7.奇葩的字符数组的指针
char* s = "My name";
如果输出s,会得到My name
输出s+1,会得到y name
QAQ
8.尽量显式申请内存空间,锁定指针,嗯。