const and pointers
const int * a // a是一个指针,指向一个const int类型的内存, a本身可以修改指向别的变量,但是a所指向的内存中的数据不能修改。
int const * a // 和第一种相同
int * const a // const修饰的是a, a是一个int *型的变量。也就是说a本身不能修改,即指向确定的内存,但所指向的内存中的数据可以修改。
const int * const a // a以及a所指向的内存中的值都不能被修改。
Mainly to see const
what is behind the modification that,
- The first
const
modification isint * a
, that*a
can not be changed, buta
can be changed. - The second
const
modification is* a
, the same*a
can not be changed, that value points to the memory can not be changed. - The third
const
modification isa
, thata
in itself can not be modified, it points to the memory space is determined, and*a
the value in the memory may be changed.
Memory structure alignment
#include <iostream>
#include <cstddef>
using namespace std;
struct a{
char a;
int b;
short c;
double d;
};
struct b{
char a;
short b;
int c;
float d;
};
int main()
{
cout<<"sizeof(struct a) : "<<sizeof(struct a)<<" sizeof(struct b) : "<<sizeof(struct b)<<endl;
cout<<"sizeof(short) : "<<sizeof(short)<<endl;
cout<<"sizeof(int) : "<<sizeof(int)<<endl;
cout<<"sizeof(long int) : "<<sizeof(long int)<<endl;
cout<<"sizeof(long long int) : "<<sizeof(long long int)<<endl;
cout<<"sizeof(flaot) : "<<sizeof(float)<<endl;
cout<<"sizeof(double) : "<<sizeof(double)<<endl;
printf("offset of b in struct a : %lun", offsetof(struct a, b));
printf("offset of d in struct a : %lun", offsetof(struct a, d));
printf("offset of b in struct b : %lun", offsetof(struct b, b));
return 0;
}
程序在64位机器上的输出结果为
sizeof(struct a) : 24 sizeof(struct b) : 12
sizeof(short) : 2
sizeof(int) : 4
sizeof(long int) : 8
sizeof(long long int) : 8
sizeof(flaot) : 4
sizeof(double) : 8
offset of b in struct a : 4
offset of d in struct a : 16
offset of b in struct b : 2
Align the memory has the following structure
- Structure variable of the start address can be a member of their broadest size evenly divisible
- Each member of the structure relative to the offset of the start address can be its size divisible by itself , if not then behind the front member of a complementary byte
- The overall size of the structure capable of being a member of the widest size divisible, if not then later added byte
Take struct a
it,
- The first member is a
char
type, one byte at offset 0. - The second member is the
int
type, itself occupies 4 bytes, an original offset, but one can not be divisible by 4, so thea
back need to add 3 bytes, thisb
start address offset becomes 4, size itself can be divisible, sob
the start address offset of 4. - The third member is the
short
type, itself occupies 2 bytes, the offset is 8, the size of which can be divisible by itself. - The fourth member is the
double
type, itself occupies 8 bytes, the offset 10, the size itself can not be divisible, a frontc
behind need to add 6 bytes, i.e.double d
the start address offset of 16. - Finally, the entire structure occupies 24 bytes, the widest members of the large column developed post written summary of basic questions is
double d
8 bytes, 24 divisible by 8, sosizeof (struct a) = 24
.
Used in the definition of stddef.h macro offsetof (type, member), a member can view the type of structure and relative offset value
A question about memory computing
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int array[2019]={0};
array[19] = 2019;
int offset = (unsigned long)((short *)array+2019) - (unsigned long)array + *(unsigned char *)(array+19);
cout<<offset<<endl;
printf("%pn", 2019);
printf("%pn", array);
cout<<(unsigned long)(array)<<endl;
cout<<(unsigned long)((short *)array+2019)<<endl;
cout<<(unsigned long)array+*(unsigned char *)(array+19)<<endl;
cout<<(int)*(unsigned char *)(array+19)<<endl;
return 0;
}
输出结果如下
4265
0x7e3 // 2019的十六进制,小端存储模式,所以在内存中应为 e3 07, 故*(unsigned char *)(array+19) = 0xe3 = 227
0x7ffeec55a800 // 64位机器输出的地址是64位的
140732863457280 // array的起始地址
140732863461318 // short占用两个字节 (unsigned long)((short *)array+2019)相当于array向后偏移了2*2019=4038个字节,因此地址值为array+4038
140732863457507 //
227
C ++ object storage
#include <iostream>
#include <string>
using namespace std;
class A{
public:
A(){}
~A(){}
void p(const string & s)
{
cout<<s<<endl;
}
};
int main()
{
A * p = NULL;
p->p("hello worldn");
}
This program can compile and run properly output hello world
.
c ++ class method stored in the code segment, so common with all methods of a class code segment, p-> p ( "hello worldn"); the code segment is accessed, so no mistakes or core dump.
Nature of the red-black tree
Each node is a red-black tree with a color attribute binary search tree . In a binary search tree mandatory general requirements, any valid red-black tree for additional requirements.
- Node is red or black;
- The root is black;
- All leaf nodes are black (leaf node is NIL);
- Each node must have a red black two child nodes (or: each leaf node can not have the two successive red nodes on all paths to the root);
- All simple path from any node to each leaf node contains the same number of black nodes.
These constraints ensure a red-black tree key features: from the root to the leaves of the longest possible path more than twice as long as the shortest possible path. Guarantee red-black tree is roughly balanced. Guarantee red-black tree in your crotch is the worst efficient.
Binary search tree, also known as binary search tree, ordered binary tree or binary tree sort, is an empty tree or binary tree with the following properties:
- If any of the left subtree of a node is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root node;
- If any of the right subtree of a node is not empty, then the right subtree are greater than values of all the nodes of the root node and its value;
- Left and right subtrees of any node are also binary search tree;
- There is no equivalent key node.
TCP three-way handshake and four wave
Deadlock
Deadlock: the case of multiple processes Since no one waiting for a resource held by the other party caused each other can not be performed is called a deadlock.
Four necessary conditions for Deadlock
- Exclusive use
- Not seize
- Request and hold
- Circular wait
Deadlock treatment
- Deadlock prevention: Deadlock failure condition;
- One-time application of all resources, not to apply for a share of resources but also other resources
- One disadvantage: the need to predict the future, programming difficulties
- 2 disadvantages: Many resource allocation after a long time after use, resource utilization is low
- Order resources, resource requests must be in order, no loops waiting
- Cons: still a waste of resources
- One-time application of all resources, not to apply for a share of resources but also other resources
- Deadlock Avoidance: detecting a request for each resource, a deadlock will result if the respective resource allocation refusal;
- Banker's algorithm
- When the request occurs: First pretend to assign, and then call the banker's algorithm, if the process sequence can not run forever refused.
- Time complexity is , m is the number of resources, n is the number of processes.
- Banker's algorithm
- + Restore deadlock detection: detects when a deadlock occurs, so that some rollback process so that the resources;
- Bankers algorithm for each application needs to perform low efficiency: identify problems and then deal
- When the detection timing detection or discovery resource utilization is low.
- Which processes choose to roll back?
- How to achieve rollback?
- Bankers algorithm for each application needs to perform low efficiency: identify problems and then deal
- Deadlock ignored (windows, linux adopt the way)
- Minimum Cost
- PC reboot little impact
- Small probability of deadlock