- 内核态、用户态区别,如何进入内核态
- 为什么使用 B+ 树作为索引的数据结构。
- extern “C”
- 自由存储区和堆的区别
腾讯后台开发面经
请带着批判的眼光看这篇文章。或许我的一些答案是不对的。如果您发现了错误,烦请指出。
C++对象模型
这个要重点掌握, 要明白类里有什么,每个类的对象里又有什么。尤其是对虚函数表的了解。这块知识较多,不在这篇文章中详述。网上有着很多优秀的博客来介绍。
C++多态机制的实现原理
主要就是上面问题中的虚函数表。
如果一个类的指针为
nullptr
,那么调用他的成员函数会不会出错。出错的话是在哪一阶段出错。也是对C++内存模型的一个考察以及动态绑定静态绑定。每个类的成员函数都在
.text
代码段,静态绑定的时候如果成员函数不访问成员变量的话那么这个函数不会出问题。如果说是虚函数或这个函数访问了成员变量,那么就会出错。虚函数出错是因为其动态绑定,因为是nullptr
, 无法确定其虚函数表。
C++动态绑定与静态绑定
今天做题的时候遇到这样一个问题。
以下程序片段输出内容为:
1 | class Demo { |
Have fun with unix
突然想起小伙伴前段时间给我看的一段神仙代码。实在特有诱惑力了。。当时女票喊我吃饭我都在看这段代码,话不多说,放码过来。
1 | printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60); |
要解开这道题,我们首先要知道下面几点。
a[3]
==3[a]
转换为指针角度看,a[3] = *(a+3) = *(3+a) = 3[a]
"abcdefg"[3]
这种表示相当于char str[]="abcdefg"; str[3]
- 知道了上面那点后,我的目光就聚焦在
unix
上,我感觉它应该是一个数字,一开始以为它是个变量,但代码中没有描述,我问小伙伴这是不是全量代码,小伙伴说文章里没有看到其他的。。于是我就开始猜他的值,一开始猜的3,是因为忘记了字符串中“\0xx”的意义,把他们当成了3个字符来看。其实它们是一个字符的,这种格式表示八进制的ASCII码。然后猜unix
值为1,后面才晓得unix
是个宏定义,#define unix 1
,这样,我们可以将上面的题目转换成较易看懂的。
1 | printf( &("\021%six\012\0"[1]), "have"[1]+"fun"-0x60); |
众所周知, printf()
的第一个参数为字符串,或者说字符串的起始地址,我们分析后可知为%six\012\0
, '%s'
表示一个字符串,故我们看第 2 个参数, have[1]
也便是 'a'
,'a'-0x60
= 97 - 96 = 1, "fun"+1
也便是字符串 "un"
的起始地址,\012
= 10, 对应 ASCII
码也便是\n
。
所以这段代码其实是 printf("unix\n");
。
unix
很棒很惊艳的代码。
Linux crontab
***** /usr/local/run.sh 中5个*分别代表什么
答案:分钟 小时 日 月 星期几
牛客用户 进击的渣渣兔 的答案:
吃货速记:分食日月粥
Educational Codeforces Round 68 D 1-2-K Game
Problem
memory limit per test : 256 megabytes
input : standard input
output : standard output
Description
Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).
Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can’t make a move loses the game.
Who wins if both participants play optimally?
Alice and Bob would like to play several games, so you should determine the winner in each game.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of games. Next T lines contain one game per line. All games are independent.
Each of the next T lines contains two integers n and k (0 ≤ n ≤ 109, 3 ≤ k ≤ 109) — the length of the strip and the constant denoting the third move, respectively.
Output
For each game, print Alice if Alice wins this game and Bob otherwise.
Example
input
1 | 4 |
output
1 | Bob |
Solution
Idea
打表找规律,存在循环。
发现若 k 不为 3 的倍数,则循环节长度为3 。若 k 为 3 的倍数, 循环节长度为 k+1, 循环节内也有一定的规律。
Educational Codeforces Round 68 B Yet Another Crosses Problem
Problem
memory limit per test : 256 megabytes
input : standard input
output : standard output
Description
You are given a picture consisting of $nn$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$ , where $1≤x≤n$ and $1≤y≤m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
The fourth picture contains 4 crosses: at $(1,3)$ , (1,5)(1,5), $(3,3)$ and $(3,5)$ .
Following images don’t contain crosses:
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
Linux源码学习——offsetof与container_of宏
Linux内核源码果真精髓,相见恨晚。
offsetof
宏定义如下
1 | #define offsetof(type, member) (size_t)&(((type*)0)->member) |
container_of
宏定义如下
1 | #define container_of(ptr, type, member) ({ \ |