forked from jerrypnz/moonranger.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-and-cpp.html
147 lines (134 loc) · 5.59 KB
/
c-and-cpp.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="Stylesheet" type="text/css" href="css/style.css" />
<title>Jerry的个人维基</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="navbar">
<ul>
<li> <a href="http://jerrypeng.me">Blog</a> </li>
<li> <a class="activated" href="index.html">WIKI</a> </li>
<li> <a href="https://github.com/moonranger">Github</a> </li>
<li> <a href="about.html">About</a> </li>
<li class="search">
<form action="http://www.google.com/search" method="get">
<input name="sitesearch" value="http://wiki.jerrypeng.me/" type="hidden" />
<input name="hl" value="zh-CN" type="hidden" />
<input name="ie" value="UTF-8" type="hidden" />
<input size="25" name="q" id="query" value="Search..." type="text"
onfocus="if( this.value=='Search...') {this.value='' };"
onblur="if( this.value=='') {this.value='Search...' };" />
</form>
</li>
</ul>
</div>
<div id="content">
<h1 id="toc_1"> C & C++</h1>
<div class="toc">
<ul>
<li><a href="#toc_1"> C & C++</a></li>
<ul>
<li><a href="#toc_1.1">1. Traps and Pitfalls</a></li>
<li><a href="#toc_1.2">2. UNIX C</a></li>
<ul>
<li><a href="#toc_1.2.1">2.1. fork, waitpid and exit</a></li>
<li><a href="#toc_1.2.2">2.2. pthread</a></li>
</ul>
</ul>
</ul>
</div>
<h2 id="toc_1.1">1. Traps and Pitfalls</h2>
<ul>
<li>
一个布尔表达式里如果既有无符号整数,又有有符号整数,并且没有强制类型转换的话,C会将这些整数当作无符处理,从而得出很多非正常的结果。
</li>
</ul>
<p>
以下程序:
</p>
<pre class="brush: c;">
#include <stdio.h>
int main(int argc, const char *argv[])
{
unsigned x = 10;
printf ("x(10U) > -1 ? %d\n", (x > -1));
printf ("10 > -1 ? %d\n", (10 > -1));
printf ("10u > -1 ? %d\n", (10u > -1));
return 0;
}
</pre>
<p>
输出:
</p>
<pre class="brush: plain;">
x(10U) > -1 ? 0
10 > -1 ? 1
10u > -1 ? 0
</pre>
<h2 id="toc_1.2">2. UNIX C</h2>
<h3 id="toc_1.2.1">2.1. fork, waitpid and exit</h3>
<ul>
<li>
fork()在父进程里返回子进程的pid,在子进程里返回0,出错返回负值。
</li>
<li>
exit()传入的状态值会和0377做与运算以后(status & 0377)再返回给父进程。为什么?
</li>
<li>
通过waitpid()得到的子进程status需要通过一系列的宏做处理后才能进一步使用。
</li>
</ul>
<pre class="brush: c;">
waitpid(pid, &status, 0);
status = WEXITSTATUS(status);
printf("Sub process finished: %u, exit status: %d\n", pid, status);
</pre>
<h3 id="toc_1.2.2">2.2. pthread</h3>
<ul>
<li>
一个进程的多个线程完全共享了同样的地址空间,一个线程甚至能访问其他线程的堆栈(虽然这样做十分容易带来各种bug)
</li>
</ul>
</div>
<!--
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'moonranger'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
-->
</body>
<script type="text/javascript" src="scripts/shCore.js"></script>
<script type="text/javascript" src="scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="scripts/shBrushBash.js"></script>
<script type="text/javascript" src="scripts/shBrushCpp.js"></script>
<script type="text/javascript" src="scripts/shBrushCSharp.js"></script>
<script type="text/javascript" src="scripts/shBrushCss.js"></script>
<script type="text/javascript" src="scripts/shBrushDiff.js"></script>
<script type="text/javascript" src="scripts/shBrushErlang.js"></script>
<script type="text/javascript" src="scripts/shBrushGroovy.js"></script>
<script type="text/javascript" src="scripts/shBrushJava.js"></script>
<script type="text/javascript" src="scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="scripts/shBrushPerl.js"></script>
<script type="text/javascript" src="scripts/shBrushPhp.js"></script>
<script type="text/javascript" src="scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="scripts/shBrushPython.js"></script>
<script type="text/javascript" src="scripts/shBrushRuby.js"></script>
<script type="text/javascript" src="scripts/shBrushScala.js"></script>
<script type="text/javascript" src="scripts/shBrushSql.js"></script>
<script type="text/javascript" src="scripts/shBrushXml.js"></script>
<script type="text/javascript" src="scripts/shBrushClojure.js"></script>
<link type="text/css" rel="stylesheet" href="styles/shCoreDefault.css"/>
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<script type="text/javascript">SyntaxHighlighter.all();</script>
</html>