<!-- [if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!-- [if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!-- [if !mso]>
<object
classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui>
</object>
<mce:style><!--
st1/:*{behavior:url(#ieooui) }
-->
<!-- [endif]-->
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"Comic Sans MS";
panose-1:3 15 7 2 3 3 2 2 2 4;
mso-font-charset:0;
mso-generic-font-family:script;
mso-font-pitch:variable;
mso-font-signature:647 0 0 0 159 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:新宋体;
panose-1:2 1 6 9 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:modern;
mso-font-pitch:fixed;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@新宋体";
panose-1:2 1 6 9 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:modern;
mso-font-pitch:fixed;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;
text-underline:single;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;
text-underline:single;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
<!-- [if gte mso 10]>
<mce:style><!--
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid windowtext;
mso-border-insidev:.5pt solid windowtext;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
-->
<!-- [endif]-->
<!-- [if supportFields]><span
lang=EN-US><span style="mso-element:field-begin" mce_style="mso-element:field-begin"></span><span
style='mso-spacerun:yes'> </span>= 3 /* GB2 <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]-->⑶
<!-- [if supportFields]><span
lang=EN-US><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->继续不简单的数据类型。
昨天没有继续“动笔”,倒是萌发了阅读《东周列国志》的念头,并且看了两章
Joel
写的《软件随想录》,颇有几分感触。今天再次回到巩固所学的路途上。
在操作系统中,有一种思想或者技术叫“覆盖”,利用的是某个运行时间段内函数(或其它占据内存的载体)不共存的现象。比如在下表中,
main
可以调用
l-fun()
或者
r-fun()
,但不会同时调用二者;同样地,
ll-fun()
和
rr-fun()
也不会共存在同一时间段内。也就是说
l-fun()
和
ll-fun()
是
main
的左子孙,而
r-fun()
和
rr-fun()
是
main
的右子孙,形成了一棵活动树,就有了一种结点间消亡顺序关系:某结点的左兄弟(如果有的话)总是先于该结点消亡,对应于活动记录先出栈(见篇一
http://blog.csdn.net/jasonblog/archive/2010/01/27/5261634.aspx
)。
主函数
main
|
l-fun();
|
r-fun();
|
ll-fun();
|
rr-fun();
|
既然
l-fun
和
r-fun
不共存,那么取
memSize = max(l-fun.memSize, r-fun.memSize)
,即取二者中占内存更大者的内存所需大小,就可以使得二者共用一片内存;
ll-fun
和
rr-fun
也同理。
这种思想让我不由自主地联想起了共用体和共享内存。
共用体与结构体类似,但是结构体内的成员变量都有独立的内存空间,而共用体是所有成员变量公用一片内存空间,其大小与内存需求最大的成员变量所需相等,并且每次对共用体成员进行访问,都是取其首地址。二者区别见下表:
结构体:
共用体:
也可以换种方式,用代码来看:
#include
<stdio.h>
struct
sData {
int
num;
float
score;
char
str[20];
};
union
uData {
int
num;
float
score;
char
str[20];
};
int
main(int
argc, char
*argv[]){
struct
sData sVar1;
union
uData uVar1;
printf("%d,%d/n"
,
sizeof
(sVar1) ,sizeof
(uVar1));
printf("%02x/t"
,
&sVar1.num);
printf("%02x/t"
,
&sVar1.score);
printf("%02x/n"
,
sVar1.str);
printf("%02x/t"
,
&uVar1.num);
printf("%02x/t"
,
&uVar1.score);
printf("%02x/n"
,
uVar1.str);
return
0;
}
上述代码的输出如下:
28,20
12f
3a
4
12f3a8
12f3ac
12f
3c
0
12f3c0
12f3c0
从输出可以看出,一是在多成员变量的情况下,共用体所需的内存空间大小相对结构体来讲要小;二是结构体每个成员都有独立的内存空间(即有独立的首地址),而共用体所有成员变量的首地址是一样的,如在上例中都为
12f
3c
0
。
共用体的一个应用让我印象十分深刻,那就是
PHP
弱类型的实现。以下两段代码形成了一定的联系和对比:
代码一
:
#include
<stdio.h>
#include
<string.h>
union
uData {
int
num;
float
score;
char
str[20];
};
int
main(int
argc, char
*argv[]){
union
uData var;
var.num = 1;
var.score = 1.1;
strcpy(var.str, "hello"
);
return
0;
}
代码二
:
<?php
$var = 1;
$var = 1.1;
$var
= "hello";
我是通过这样的比较来初步在自己的脑海里建立起如何用强类型的
C
语言来实现弱类型的
PHP
语言。若要继续深入一点,则要揭开一点点
zend.h
的幕纱。
在
php
中,变量的信息统一用一个结构体
_zval_struct
来保存,以下是摘自
5.3.1
源码的代码片段:
struct
_zval_struct {
/* Variable
information */
zvalue_value value;
/* value */
zend_uint refcount__gc;
zend_uchar type;
/* active type */
zend_uchar is_ref__gc;
};
而由注释可以知道保存变量值的是
value
,它是一个
zvalue_value
类型的变量。该类型的定义在
zend.h
中的位置刚好是在
_zval_struct
上方:
typedef
union
_zvalue_value {
long
lval;
/* long value */
double
dval;
/* double value */
struct
{
char
*val;
int
len;
} str;
HashTable *ht;
/* hash table
value */
zend_object_value obj;
}
zvalue_value;
显然可以知道
zvalue_value
是一个共用体类型的变量。这就是关于强类型
C
语言如何实现弱类型
PHP
语言的一点点粗浅知识,但是就是这点粗浅的知识给我的印象却是十分深刻,让我觉得设计思想十分重要,也是我十分欠缺的。
共用体就暂时告一段落,转向
C
语言中经典类型:指针。
在我的理解中,内存就像一栋楼房,每间楼房就好比内存单元,门牌号好比内存地址,楼房内的事物就是内存单元存储的内容,而指针,便如门牌号。举个例子,比如我们有这么一句声明:
int *p;
这就表示我们向物业要了一间专门存放门牌号的房间,并且这个房间里面存放的门牌号都必须指向存放
int
类型数据的房间。
以下是几种指针类型:
int *p;
表示p
是指针,该指针指向int
类型的变量。
int * *p;
表示p
是指针,该指针指向指针变量,且后者为指向int
类型变量的指针。
int *p[5];
表示数组p
中的元素为指针。
int (*p)[5];
表示p
为指针,指向类似a[][5]
这样的二维数组。
int *f();
表示f
是一个函数,该函数返回一个指针类型的值。
int (*f)();
表示f
是一个指针,该指针指向一个函数入口。
相对于共用体给我那个印象深刻的应用,指针也留下了一个不浅的足迹,那就是一堆括号、
void
、函数以及指针的结合:
(*
(void(*)()
)0
)()
。第一次见到它那种被震撼到的心情此时仍记忆犹新,真是令人头皮发麻啊!在此不妨先引入一个法则:
The right-left rule:
Start reading the declaration
from the innermost parentheses, go right, and then go left. When you encounter
parentheses, the direction should be reversed. Once everything in the
parentheses has been parsed, jump out of it. Continue till the whole
declaration has been parsed.
以上是判断指针类型的有效手段,大致意思就是从最里的括号开始,先右后左开始解析,遇到括号则转向。我们以上述例子来进行分析。
首先,往最里面的括号看可以看到“(*)
”,我们可以初步知道这个括号表示一个指针;接着跳出去,先往右再往左瞧可以分析出“(*)()
”,对照上面几种指针类型,我们可以进一步知道这个指针指向一个函数入口;继续right-left
,可以匹配出“(void (*)())
”来,可以了解到指针指向的函数返回值为void
类型;接下来的一步我们小心点地向右看,可以看到数字0
,这意味着将数字0
强制转换为一个指向返回值为void
类型的函数入口的指针(有点拗口^_^
),遇到括号后我们再返回来往左看,可以看到星号“*
”,而星号放在指针前面是表示取指针变量指向的内容,于是到这个时候我们获得了函数入口地址(这个入口地址为0
);继而在最后,我们可以知道整个表达式的功能是调用入口地址为0
的函数!为了更深刻地理解,依旧以代码形式进行了解。
首先,看如下代码:
#include
<stdio.h>
void
greet(){
printf("hello/n"
);
}
int
main(int
argc, char
*argv[]){
void
(*fp)();
fp = greet;
(*fp)();
//
缩写为:
fp();
printf("%02x/n"
,
*fp);
//
同上,这里也可以缩写为:
printf("%02x/n", fp);
return
0;
}
上述fp
是一个指向函数入口的指针,存放的是函数的入口地址,所以*fp
为函数的入口地址。接着模仿那个令人发麻的表达式修改一下代码:
#include
<stdio.h>
void
greet(){
printf("hello/n"
);
}
int
main(int
argc, char
*argv[]){
void
(*fp)();
fp = greet;
(*fp)();
//
缩写为:
fp();
printf("%02x/n"
,
*fp);
//
这里输出函数的入口地址,即
*fp
的内容
(*(void
(*)())(*fp))();
//
!!注意这里
return
0;
}
运行的结果如下:
hello
401050
hello
可见程序调用了两次greet
函数,一次是通过fp
指针获取入口地址进而调用,另一次则是仿造那个“神奇”的表达式。^_^
2010-1-29
晚
----------------------------------------cutting
line----------------------------------------
分享到:
相关推荐
books, this title concentrates on modern, best-practice techniques from the very beginning, which means you’ll get it right the first time. The web sites you’ll build will: Look good on a PC, Mac ...
books, this title concentrates on modern, best-practice techniques from the very beginning, which means you’ll get it right the first time. The web sites you’ll build will: Look good on a PC, Mac ...
Like any human language, C++ provides a way to express concepts. If successful, this medium of expression is significantly easier and more flexible than the alternatives as problems grow larger and ...
Managed VCL is the best components suite to work with .Net framework from Delphi and C++ Builder - feature named .Net interop. It is designed to provide a way to interact with applications written in ...
Learn how to use C++ from first principles in a practical hands-on way. Understand how to use Custom types, virtual functions and objects to structure your code Build your own function templates, ...
Templates: The Hard Way Templates: The C++ Way Function Specialization Class Templates Class Specialization Implementation Details Advanced Features Summary Programming Exercises 25. Standard ...
江苏省铜山区清华中学七年级英语下册 Unit 4 Finding your way Comic Strip&Welcome to the Unit学案(无答案)(新版)牛津版
Complete text of nine essential books ... 因为上传附件大小受限,故将本资源分为两部分,第二部分是Essential Books on Algorithms and Data Structures(part2)(chm version),请注意两个一起下载,否则内容不全。
江苏省铜山区清华中学七年级英语下册 Unit 4 Finding your way Study skills&Task学案(无答案)(新版)牛津版
WAYON维安一级代理分销KOYUELEC光与电子WAYON OVP IC INTRODUCTION过压保护方案与应用
扫描工具——X-SCAN,NMAP和X-WAY的视频教程以及工具下载
Unfortunately, there are very few books on the market about Borland C++or C++Builder, compared to other products like Visual Basic or VisualC++. Since different authors have different viewpoints of a ...
The author also endeavors to organize the C code for those who are going on to study C++ and the accompanying object-oriented approach to programming. In addition, he emphasizes how to write C code so...
Series: A Smarter Way to Learn Paperback: 258 pages Publisher: CreateSpace Independent Publishing Platform; 1 edition (March 13, 2015) Language: English ISBN-10: 150867387X ISBN-13: 978-1508673873 ...
In a very similar way, C++/CLI is layered on top of C++. C++/CLI provides a high degree of source code compatibility with C++. As a consequence, the following code is valid if you build the program ...
I learned C++11 the hard way. Because I didn’t follow the standardization as it was happening I started to look at C++11 about two years ago. I really had trouble understanding it. But the people on ...
Although it is not as big a step as C++11, it contains a large number of small and valuable language and library features, which again will change the way we program in C++. This applies to both ...
teams a practical way to ensure that their C and C++ code works as expected. C++test can be used both on the desktop under leading IDEs as well as in batch processes via command line interface for ...
Essential Books on Algorithms and Data ... 因为上传附件大小受限,故将本资源分为两部分,第一部分是Essential Books on Algorithms and Data Structures(part1)(chm version),请注意两个一起下载,否则内容不全。