-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiary.txt
479 lines (418 loc) · 14.1 KB
/
Diary.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
【2017-09-18】C++内存优化技巧
union
{
// 定义多个互斥struct
struct A
{
};
struct B
{
};
//......
};
【2017-09-19】【1】C++11 新for
int ary[] = {1,3,1,4,};
for(int nData : ary)
{
printf("%d\n", nData);
}
for(int &nData : ary)
{
nData = 0;
printf("%d\n", nData);
}
// 注意auto类型不是int
std::vector<int> vec;
vec.push_back(1); vec.push_back(3); vec.push_back(1); vec.push_back(4);
for(auto nData : vec)
{
printf("%d\n", nData);
}
for(int nData : vec)
{
printf("%d\n", nData);
}
for(auto &it : vec)
{
it = 0;
printf("%d\n", it);
}
std::map<int, int> map;
map.insert(std::pair<int, int>(10, 10));
// 注意auto类型std::map<int, int>::value_type即std::pair<int, int>
for(auto pair : map)
{
printf("%d %d\n", pair.first, pair.second);
}
for(std::map<int, int>::value_type pair : map)
{
printf("%d %d\n", pair.first, pair.second);
}
//所以for(auto it : container) 中 auto类型为container元素类型而非迭代器
【2017-09-19】【2】umdh查内存泄漏
1.首先安装好WinDbg
2.然后用任务管理员权限打开cmd
3.cd到WinDbg目录
4.执行 set _NT_SYMBOL_PATH="%windir%\symbols";AppPath目标程序符号文件位置;
5.打开App.exe
6.执行 gflags -i App.exe +ust (App为已经开启的进程 成功执行后屏幕会输出一些东西)
7.执行 umdh.exe -pn:App.exe -f:D:/Output/FirstDump.txt
8.继续运行App.exe执行一些操作
9.执行 umdh.exe -pn:App.exe -f:D:/Output/SecondDump.txt
10.执行 umdh.exe D:/Output/FirstDump.txt D:/Output/SecondDump.txt -f:D:/Output/Result.txt
11.Profile结果在Result.txt里
【2017-09-20】内存泄漏检查
// http://blog.csdn.net/bluehawksky/article/details/39841975
// http://www.cnblogs.com/findumars/p/5353538.html
// http://www.cnblogs.com/skynet/archive/2011/02/20/1959162.html
#ifdef MEMORY_DUMP_DEBUG
# ifdef _WIN32
# define _CRTDBG_MAP_ALLOC
# include <stdlib.h>
# include <crtdbg.h>
// 开始检测内存泄漏
// DUMP_MEMORY_LEAK_BEGIN开始跟踪内存分配
# define DUMP_MEMORY_LEAK_BEGIN() {_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);}
// 结束检测内存泄漏
// DUMP_MEMORY_LEAK_END是清空内存分配跟踪
# define DUMP_MEMORY_LEAK_END() {_CrtDumpMemoryLeaks(); _CrtSetDbgFlag(0);}
# define DUMP_MEMORY_STATUS() {_CrtMemState s; _CrtMemCheckpoint(&s); _CrtMemDumpStatistics(&s);}
# else
// https://en.wikipedia.org/wiki/Mtrace
# include <stdlib.h>
# include <mcheck.h>
# define DUMP_MEMORY_LEAK_BEGIN() {mtrace();}
# define DUMP_MEMORY_LEAK_END() {muntrace();}
# define DUMP_MEMORY_STATUS()
# endif
#else
# define DUMP_MEMORY_LEAK_BEGIN()
# define DUMP_MEMORY_LEAK_END()
# define DUMP_MEMORY_STATUS()
#endif//MEMORY_DUMP_DEBUG
【2017-09-21】【1】命令队列
如果一个方法要加入命令队列然后轮询
但是方法未执行时调用者不能返回 必须阻塞等待
这时候可以用信号量解决
Sem sem;
CommandList.push([&sem](/*...*/)
{
//DoSomWork
sem.Notify();
}
sem.Wait();
【2017-09-21】【2】状态转移
CAS做状态转移
atomic state;
if(state.cas(exp, tar))
{
//状态转移成功
}
【2017-09-22】【1】回顾operator new
// 覆盖全局operator new
void *operator new(size_t size)
{
return malloc(size);
}
void operator delete(void* p)
{
free(p);
}
// 覆盖全局operator new[]
void *operator new[](size_t size)
{
return malloc(size);
}
void operator delete[](void* p)
{
free(p);
}
// placement new
void *operator new(size_t size, /*用户自定义参数*/void* pMemory, int nLine, const char* pFileName)
{
printf("%d %s\n", nLine, pFileName);
return pMemory;
}
void* p = operator new(sizeof(int));// new int;
int *newPtr = new(p, __LINE__, __FILE__) int;
语法: ptr = new (参数表) 构造函数
【2017-09-22】【2】宏定义# ##
#define TOSTR(x) #x //量化为字符串
#define LINK_X_Y(x, y) x##y //宏连接
puts(TOSTR(100));// 100
puts(LINK_X_Y(TOSTR(12), TOSTR(34))); //1234
puts(TOSTR(LINK_X_Y(56, 78))); // LINK_X_Y(56, 78) 直接把#后面的变成字符串了
【2017-09-25】函数修饰
__stdcall 约定被调用函数负责对函数参数退栈 Windows回调函数只能使用该约定
__cdecl 约定调用者负责函数参数退栈 因此可变函数参数只能使用该约定
C和C++默认约定是__cdecl
可以这样检测默认约定
int __cdecl add(int a, int b);
int add(int a, int b)
{
return a + b;
}
如果编译器提示出错默认约定是__stdcall
由于没有出错提示 因此默认约定是__cdecl
注意如果使用__stdcall要保证声明与实现都有__stdcall标志
还有约定函数指针时也要注意匹配
【2017-09-26】C++ inl文件
http://blog.csdn.net/business122/article/details/9770019
内联函数和模版的声明和定义必须放在一起
但是如果数量太多影响阅读 可以把实现抽出来放到inl文件里
然后头文件在结尾include该inl文件
【2017-09-26】C++ 命名空间
namespace
{
void Test()
{
puts("namespace");
}
}
void Test()
{
puts("global");
}
int main()
{
/*
Test();
error C2668: “Test”: 对重载函数的调用不明确
: 可能是“void Test(void)”
: 或 “void `anonymous-namespace'::Test(void)”
尝试匹配参数列表“(void)”时
*/
::Test();//输出global
}
【2017-09-27】#pragma push_macro #pragma pop_macro
//http://blog.csdn.net/ixsea/article/details/7976627
>1.
#define int double
printf("%d\n", sizeof(int)); // 8
#pragma push_macro("int")// 将int的定义入栈
#undef int // 取消该宏定义
printf("%d\n", sizeof(int)); // 4
#pragma pop_macro("int")// 恢复入栈前int的定义
printf("%d\n", sizeof(int)); // 8
#pragma push_macro("int")
printf("%d\n", sizeof(int)); // 8
#pragma pop_macro("int")
>2.
#define A 1
#define B 2
#pragma push_macro("A")
#pragma push_macro("B")
#pragma pop_macro("A")
#pragma pop_macro("B")
printf("%d %d\n", A, B);// 1 2
所以入栈 出栈是独立的
>3.
在三方库源码中,我们经常看到这样的代码:
#pragma push_macro("new")
#undef new
// do something with new
......
#pragma pop_macro("new")
它的作用就是将宏定义new压入栈并取消它的定义
如此一来new的本来含义便获得了恢复
使用完毕后将宏定义new弹出栈 恢复宏定义
【2017-09-29】CMake
# https://cmake.org/cmake/help/v3.9/
>1.备注
# This is a CMake comment
或者
#[[This is a CMake\
comment]]
>2.输出
# https://cmake.org/cmake/help/v3.9/command/message.html?highlight=message
message(STATUS This is a message)
>3.指定版本
cmake_minimum_required (VERSION 3.9)
>4.添加引用目录
# https://cmake.org/cmake/help/v3.9/command/include_directories.html?highlight=include_directories
include_directories(./)
>5.编译器参数 如果添加预处理宏定义要加上-D
# https://cmake.org/cmake/help/v3.9/command/add_definitions.html?highlight=add_definitions
# 相当于加上MEMORY_DUMP_DEBUG预处理宏
add_definitions(-DMEMORY_DUMP_DEBUG)
>6.收集目录下所有源文件并储存到变量中 注意只有源文件不含头文件
# 相当于收集CMakeLists.txt目录下的所有源文件并储存到变量SRC中
# 注意没有递归到各个子目录收集的功能 而且只有只有源文件不含头文件
# 不好用
aux_source_directory(./ SRC)
>7.收集目录下所有文件并储存到变量中 不递归
# https://cmake.org/cmake/help/v3.9/command/file.html?highlight=file
# 相当于收集Interface下所有头文件和源文件并储存到变量ALL_FILES中
file(GLOB ALL_FILES
Interface/*.h
Interface/*.cpp
)
>8.收集目录下所有文件并储存到变量中 递归到各个子目录
file(GLOB_RECURSE ALL_FILES
Interface/*.h
Interface/*.cpp
)
>9.设置变量 注意没有通配符用法
# https://cmake.org/cmake/help/v3.9/command/set.html?highlight=set
# 设置变量FILETOOL_CPP
set(FILETOOL_CPP Internal/KFileTool.cpp)
>10.指定一个源文件组
# https://cmake.org/cmake/help/v3.9/command/source_group.html?highlight=source_group
# 添加一个源文件组Internal 并且把INTERNAL变量指定的文件添加进去
source_group("Internal" FILES ${INTERNAL})
>11.生成链接库
# https://cmake.org/cmake/help/v3.9/command/add_library.html?highlight=add_library
# 生成一个叫BASE的库 并且把INTERFACE INTERNAL PUBLISH指定的文件添加进去
add_library(BASE ${INTERFACE} ${INTERNAL} ${PUBLISH})
>12.指定生成目标
# https://cmake.org/cmake/help/v3.9/command/add_executable.html?highlight=add_executable
# 用法如同add_library 不过项目生成的是一个可执行文件
add_executable(EXE ${INTERFACE} ${INTERNAL} ${PUBLISH})
>13.添加链接库
# https://cmake.org/cmake/help/v3.9/command/target_link_libraries.html?highlight=target_link_libraries
# 指定对于项目EXE添加依赖库../Base/BASE
# 注意位置要放在EXE对应的add_executable之后
target_link_libraries(EXE ../Base/BASE)
【2017-10-06】C++11 CAS
C++11 CAS
http://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange
注意C++11中atomic的CAS操作中 【Exp】是以【引用】传入而非【常引用】
所以标准就允许CAS操作后【Exp】被修改
VS2012下CAS后【Exp】就有被修改的可能
【2017-10-06】批量修改文件编码
1.安装Python
2.在Notepad++插件管理器里下载Python Script
3.选择Plugins->Python Script->New script把ToUTF8.py添加进去
4.在ToUTF8.py修改转换路径
5.重启Notepad++
6.选择Plugins->Python Script->ToUTF8
ToUTF8.py
import os;
import sys;
filePathSrc="D:\\KApp\\" # Path to the folder with files to convert
for root, dirs, files in os.walk(filePathSrc):
for fn in files:
if fn[-4:] == '.cpp':
notepad.open(root + "\\" + fn)
notepad.runMenuCommand("Encoding", "Convert to UTF-8")
notepad.runMenuCommand("Edit", "EOL Conversion")
notepad.save()
notepad.close()
elif fn[-2:] == '.c':
notepad.open(root + "\\" + fn)
notepad.runMenuCommand("Encoding", "Convert to UTF-8")
notepad.save()
notepad.close()
elif fn[-2:] == '.h':
notepad.open(root + "\\" + fn)
notepad.runMenuCommand("Encoding", "Convert to UTF-8")
notepad.save()
notepad.close()
【2017-11-07】ID3D11DeviceContext::Map method
https://msdn.microsoft.com/en-us/library/windows/desktop/ff476457(v=vs.85).aspx
Don't read from a subresource mapped for writing
When you pass D3D11_MAP_WRITE, D3D11_MAP_WRITE_DISCARD,
or D3D11_MAP_WRITE_NO_OVERWRITE to the MapType parameter,
you must ensure that your app does not read the subresource
data to which the pData member of D3D11_MAPPED_SUBRESOURCE
points because doing so can cause a significant performance penalty.
The memory region to which pData points can be allocated with PAGE_WRITECOMBINE,
and your app must honor all restrictions that are associated with such memory.
【2017-12-07】DX11 Map
1.在AMD RX 390下申请一个2M的Usage为D3D11_USAGE_DYNAMIC Buffer
并且用D3D11_MAP_WRITE_DISCARD 去Map会导致很卡
把Buffer大小改为512K后问题消失
2.Usage为D3D11_USAGE_DEFAULT的Buffer只能用UpdateSubresource
更新数据 背后原理是自动申请一个Buffer然后填充完这个临时的
Buffer后拷贝到被Update的Buffer上
【2017-12-21】【1】字符编码
1.ANSI 是对应当前系统locale的遗留(legacy)编码
(通常使用 0x00~0x7f 1个字节来表示1个英文字符
为使计算机支持更多语言 通常使用 0x80~0xFFFF 范围的2个字节来表示1个字符
不同的国家和地区制定了不同的标准 由此产生了 GB2312 GBK GB18030 Big5
Shift_JIS 等各自的编码标准)
2.Unicode 带有 BOM 的小端序 UTF-16
(Unicode 规范定义 每一个文件的最前面分别加入一个表示编码顺序的字符
这个字符的名字叫做"零宽度非换行空格"(zero width no-break space)用FEFF表示
如果一个文本文件的头两个字节是FE FF 就表示该文件采用大头方式
如果头两个字节是FF FE就表示该文件采用小头方式)
3.UTF-8 Windows下是带BOM的UTF-8
【2017-12-21】【2】Sublime Text 3配置Python2编译环境
1.首先在环境变量里的path加上Python2的安装路径
2.在工具->编译系统->新建编译系统
用以下代码替代默认代码
{
"cmd": ["H:/Python27/python.exe","-u","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
}
"cmd": ["Python安装路径","-u","$file"]
3.保存名字为python2.sublime-build注意扩展名要为.sublime-build才能被
Sublime记住
【2017-12-21】【3】Python迭代技巧
array = [1, 2, 3]
for i in range(len(array)):
print i
# range 返回一个列表 [0, 1, 2]
# http://www.runoob.com/python/python-func-range.html
python range() 函数可创建一个整数列表,一般用在 for 循环中。
函数语法
range(start, stop[, step])
参数说明:
start: 计数从 start 开始 默认是从 0 开始 例如range(5) 等价于range(0, 5)
end: 计数到 end 结束,但不包括 end 例如: range(0, 5) 是[0, 1, 2, 3, 4]没有5
step:步长,默认为1 例如:range(0, 5) 等价于 range(0, 5, 1)
【2017-12-22】【1】Python索引和分片
>例子1: 构造回文数
def func(res, idx, first):
if idx < 0:
# res[::-1]反转字符串 默认指定开始索引为-1
revstr = res[::-1]
# 从索引为1开始截断字符串
# res += revstr[1:len(revstr)]
res += revstr[1:]
print res
return
for k in range(first, 10):
func(res + str(k), idx - 1, 0)
for i in range(1, 3):
func('', i, 1)
>例子2: 列表操作
list = ['0', '1', '2']
# 0为开始索引
print list[0:] # ['0', '1', '2']
# 3为结束索引不包括
print list[0:3] # ['0', '1', '2']
# 1为步长
print list[0:3:1] # ['0', '1', '2']
print list[0:3:2] # ['0', '2']
# -1为开始索引
print list[-1::] # ['2']
# 第二个-1为步长
print list[-1::-1] # ['2', '1', '0']
print list[-1::-2] # ['2', '0']
print list[-2::-1] # ['1', '0']
# TODO: 反转访问无法指定结束索引
【2017-12-22】【2】Python lambda
lambda表达式只能写在同一行
不过可以用\进行分割
SUM = lambda a, b:\
a + b
print SUM(10, 20)
【2017-12-22】【3】Python对象比较
a = 1
b = 2
# a, b 是否同一个对象
print a == b # False
b -= 1
print a == b # True
# 对a, b 进行值比较
print cmp(a, b) # 0
【2017-12-22】【4】Python赋值
# 与C++不同 等号赋值语句右部分只是值的快照
a, b, c = 1, 2, 3
print a, b # 1 2
a, b = b, a
print a, b # 2 1
a, b = c, a
print a, b # 3 2