shootingstars

菩提本无树,明镜亦非台。本来无一物,何处惹尘埃。尘埃乃虚幻,亦何惧之来?

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  25 随笔 :: 0 文章 :: 45 评论 :: 0 引用

2008年7月30日 #

1 使用:
一直以来习惯了使用printf函数,但是对于可变参数没有深入研究过,觉得可变参数是一个神奇的技术^0^。。。
工作闲下来的时候,想研究研究看可变参数的使用和原理。
目前C提供的可变参数的申明为
void function(const char *format, ...);
这样就可以在function中使用可变参数
C提供了几个宏用于使用可变参数
va_list
va_start
va_arg
va_end
其中
va_list用于定义一个变量获取可变参数指针
va_start用于将va_list定义的指针进行初始化
va_arg用于获取对应指针的真实类型数据
va_end用于清空va_list定义的指针

好了,光说不练假把式,来一个例子吧。嗯,什么样的例子比较好呢?
对了,在c的printf中不支持c++的std::string,就自己实现一个支持std::string的printf吧。
例子:
Code

调用代码:
Code

输出结果为:
MyPrint Function Version:1, Support C++ std::string abc...version 2

可变参数真是神奇的很啊。。。

2 原理:
我们来看看这几个宏到底干了什么
typedef char *  va_list;  // 这个仅仅是个重定义而已。。。

// 获取v的地址
#define _ADDRESSOF(v)   ( &(v) )  
// n的整数字节的大小,必须是sizeof(int)的整数倍。如sizeof(n)为5的话,_INTSIZEOF(n)为8(假设为32位机器的话)
#define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
// 给v的地址加上v的大小  
#define va_start(ap,v)  ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )

// 给ap自增t的大小,并且获取原有ap的地址的数据,强制转型为t类型
// 这个相当于 ( *(t *)ap )
//            (ap += _INTSIZEOF(t))
// 这一个宏相当于完成两件事情
#define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )

// 给ap置0
#define va_end(ap)      ( ap = (va_list)0 )

我们有必要了解一下C函数的调用规则了,在调用一个函数之前,调用方会将这个函数参数push(修改ESP指针),并且push规则是先push最后一个参数,最后push第一个参数,因此ESP指针最后应该是指向第一个参数。可变参数就是利用了这一点,一旦获取到第一个参数的地址后,就能够通过地址向前查找所有的参数。(注意:x86上的堆栈是反向的,push会使ESP的值减少,而不是增加)
上面的宏就是帮助用户查找所有的可变参数。

问题:
printf以及Printf都不是类型安全的。调用方必须保证参数个数的正确,以及参数类型的正确,否则将会发生不可预期的错误。

3 探索
是否可以写一个没有固定参数的函数,比如
int f (...);
据ANSI C说不行,但是我的vc8可行。
问题是写出这样的函数,va_start就用不上了,因为它需要可变参之前的那个固定参数。
其实我们可以自己从ESP中获取相关参数。
例子:
Code

调用代码:
Code


输出结果:
First=3
Second=5
Third=7

这样完全是可以获取第一个参数的地址的。问题是,由于没有类型信息和类型个数等信息(类似于printf中的%c等信息),所以这样的例子貌似意义不是很大。
posted @ 2008-07-30 14:05 shootingstars 阅读(118) | 评论 (0)编辑

2008年7月22日 #

int _stdcall s_addint(int i, int j)
{
    printf(
"i = %d\n",i);
    printf(
"j = %d\n",j);
    
return i+j;
}


int __cdecl  c_addint(int i, int j)
{
    printf(
"i = %d\n",i);
    printf(
"j = %d\n",j);
    
return i+j;
}


int _fastcall  f_addint(int i, int j, int k)
{
    printf(
"i = %d\n",i);
    printf(
"j = %d\n",j);
    printf(
"k = %d\n",k);
    
return i+j+k;
}


int _tmain(int argc, _TCHAR* argv[])
{
    
int num;

    
// 汇编调用_stdcall函数
    
// 参数由右至左压栈
    
// 调用返回时,堆栈由被调函数调整
    
// 返回值在EAX中
    _asm
    
{
        push 
2
        push 
1
        call s_addint
        mov  num,eax
    }


    printf(
"num = %d\n",num);

    
// 汇编调用__cdecl函数
    
// 参数由右至左压栈
    
// 调用返回时,堆栈由调用者调整
    
// 返回值在EAX中
    _asm
    
{
        push 
4
        push 
3
        call c_addint
        mov  num,eax
        add esp,
4*2
    }


    printf(
"num = %d\n",num);

    
// 汇编调用_fastcall函数
    
// 函数的第一个和第二个DWORD参数(或者尺寸更小的)通过ecx和edx传递,其他参数通过从右向左的顺序压栈
    
// 调用返回时,堆栈由被调函数调整
    
// 返回值在EAX中
    _asm
    
{
        mov ecx,
5
        mov edx,
6
        push 
7
        call f_addint
        mov  num,eax
    }


    printf(
"num = %d\n",num);
    
    
return 0;

}

 

以上代码通过VC8编译

 

posted @ 2008-07-22 13:52 shootingstars 阅读(158) | 评论 (0)编辑

2008年6月2日 #

在Java中,继承类可以使用super访问基类中的数据变量
class A
{
    
int i = -1;
}


class B extends A
{
    
void printSuperI()
    
{
        System.out.println(
super.i);
    }

}

但是在C++中,貌似没有此类访问符。。。
能否在C++中访问基类中的数据呢?当然能够,最简单的方式莫过于在基类中定义Get/Set等属性函数,在继承类中使用即可。
问题是,变量太多,定义n多的Get/Set看着有些烦人。
呵呵,其实可以采用一种不是很正规的方式来直接访问基类中的数据(注意:在真实的项目中不建议使用此类方法,这个方法仅仅用于对C++二进制布局的了解+测试而已。我目前已经用vc8、gcc测试通过)
我们知道,在C++中,基类和继承类对象其实享用的同一块内存空间。即this指针所指位置。我们的所有数据都在this指针所指位置上,所有数据按照内存正向规则向上占用各自的位置。

例子代码:
class A
{
public:
    
int i,j;
    A()
    
{
        i 
= -1;
        j 
= -2;
    }


}
;

class B : public A
{
public:
    
void printSuperI()
    
{
        
// 此this指针及指向A::i的内存位置
        int superI = *(int *)(this);   
        
// 此this+4指针及指向A::j的内存位置(sizeof(int) == 4)
        
// 注意:此时必须知道C++的内存对齐方案,当然你可以通过命令要求按照自己的规则进行内存对齐。
        int superJ = *(int *)((char*)this+4);  
        printf(
"SuperI=%d, superJ=%d\n",superI,superJ);
    }

}
;


问题:一旦在基类中出现了虚拟函数,此时的this指针不再指向数据的第一个变量,而是指向vptr。
在此种情况下需要给this指针加上一个vptr的指针变量位置即可。
class A
{
public:
    
int i;
    A()
    
{
        i 
= -1;
    }

    
virtual void foo()
    
{
        printf(
"Foo\n");
    }


}
;

class B : public A
{
public:
    
void printSuperI()
    
{
        
int superI = *(int *)((char*)this+4);
        printf(
"SuperI=%d\n",superI);
    }

}
;


补充:
既然知道了vptr的位置,那我们可以直接手工打造函数地址来调用它(我只使用vc8调用通过,gcc调用报错。。。)
vc的c++虚拟指针手工调用
class A
{
public:
    
int i;
    
char *p;
    A()
    
{
        p 
= "5678";
        i 
= -1;
    }

    
virtual void foo(const char *s)
    
{
        printf(
"Foo, %s : %s\n",s, p);
    }

    
virtual void foo1(const char *s)
    
{
        printf(
"Foo1, %s\n",s);
    }


}
;

typedef 
void (A::*foo_fun)(const char *p);

class B : public A
{
public:
    
void printSuperI()
    
{
        unsigned 
int *vptr;
        
int superI = *(int *)((char*)this+4);
        printf(
"SuperI=%d\n",superI);
        vptr 
= (unsigned int *)this;
        foo_fun fun_ptr 
= *(foo_fun*)(*vptr);
        (
this->*fun_ptr)("1234");
        foo_fun fun_ptr1 
= *(foo_fun*)((*vptr)+4);
        (
this->*fun_ptr1)("abcd");
    }

}
;

posted @ 2008-06-02 09:23 shootingstars 阅读(169) | 评论 (0)编辑

2008年5月7日 #

编译程序一般划分为
1 词法分析 -- 输入源程序,对源程序的字符串进行扫描和分解,得到一个个单词(token)
2 语法分析 -- 按照语法规则,确定输入串是否符合此规则
3 语义分析及中间代码产生 -- 在语法分析的基础上分析含义,并且进行相关的翻译(中间代码)。如很多编译器采用“四元式”作为中间代码,四元式如下所示:
    算符    左操作数    右操作数    结果
4 优化 -- 优化以上产生的中间代码,产生更加有效的中间代码
5 目标代码生成 -- 按照目标机产生低级语言代码

前端以及后端
前端 -- 与目标机无关的部分(词法分析,语法分析,语义分析,与目标机无关的中间代码优化)
后端 -- 与目标机有关的部分(与目标机相关的中间代码优化以及目标代码生成)

posted @ 2008-05-07 18:43 shootingstars 阅读(85) | 评论 (0)编辑

2007年8月17日 #

以前使用bind1st以及bind2nd很少,后来发现这两个函数还挺好玩的,于是关心上了。
在C++ Primer对于bind函数的描述如下:

绑定器binder通过把二元函数对象的一个实参绑定到一个特殊的值上将其转换成一元函数对象

C++标准库提供了两种预定义的binder 适配器bind1st 和bind2nd 正如你所预料的bind1st 把值绑定到二元函数对象的第一个实参上bind2nd 把值绑定在第二个实参上
例如
为了计数容器中所有小于或等于10 的元素的个数我们可以这样向count_if()传递
count_if( vec.begin(), vec.end(), bind2nd( less_equal<int>(), 10 ));


哦,这倒是挺有意思的。于是依葫芦画瓢:
bool print(int i, int j) 
{
    std::cout
<< i << "---" << j << std::endl; 
    
return i>j;
}

int main(int argc, char *argv[])
{
    (std::bind1st(print, 
2))(1);
    
return 0;

}
满怀希望它能够打印
2---1

只不过。。。编译出错:
1    Error    error C2784: 'std::binder1st<_Fn2> std::bind1st(const _Fn2 &,const _Ty &)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'   
---不能够推断出模板参数for 'overloaded function type' from 'overloaded function type' 。。。。
(还真看不明白。。。)

于是直接看bind1st代码:
template<class _Fn2,
    
class _Ty> inline
    binder1st
<_Fn2> bind1st(const _Fn2& _Func, const _Ty& _Left)
        {    
        typename _Fn2::first_argument_type _Val(_Left);
        
return (std::binder1st<_Fn2>(_Func, _Val));
        }
嗯。。。在代码里

typename _Fn2::first_argument_type _Val(_Left)
说必须定义
first_argument_type类型,可是我一个函数,哪里来的这个类型定义?嗯,STL一定提供了某种东东用来自动定义这个类型。找啊找,于是找到了ptr_fun。
这个函数自动将一个函数指针转换为一个binary_function的继承类pointer_to_binary_function,而在
binary_function中定义了first_argument_type。
于是修改代码:
int main(int argc, char *argv[])
{
    (std::bind1st(std::ptr_fun(print), 
2))(1);
    
return 0;
}
打印结果如下:
2---1
posted @ 2007-08-17 17:35 shootingstars 阅读(767) | 评论 (2)编辑

2007年8月13日 #

最近公司版本管理日显复杂,急需进行每日构建来确保版本质量。于是想到了用python开发一个简单的每日构建工具进行自动代码切出,自动编译以及自动打包。

写了好几天,基本成果出来了。用一个XML文件控制所有的代码切出,编译以及打包过程。
公司的软件涉及到了C++,Java和C#,C++和C#都是用的Vs2005,这个好说,用Vs2005自带的命令行方式编译项目即可。但是Java开发用的是Eclipse,没有听说Eclipse自己带命令行方式的,同事推荐使用ANT,于是研究了以下ANT的配置方式。。。天哪。。。我写的那个每日构建工具基本上用ANT都可以实现。。。
郁闷,早知道,也没必要花好几天时间,直接拿ANT来用就好了。。。

不过好在发现自己写的东东与ANT的很多东西不谋而合,也算心里有点安慰了。

附上一个python的学习文档,俺就是看着它上手的:Python 研究(Dive Into Python)

posted @ 2007-08-13 14:41 shootingstars 阅读(265) | 评论 (0)编辑

2007年8月1日 #

七:regex_replace学习
写了个去除左侧无效字符(空格,回车,TAB)的正则表达式。
std::string testString = "    \r\n Hello        World  !  GoodBye  World\r\n";
std::
string TrimLeft = "([\\s\\r\\n\\t]*)(\\w*.*)";
boost::regex expression(TrimLeft);
testString 
= boost::regex_replace( testString, expression, "$2" );
std::cout
<< "TrimLeft:" << testString <<std::endl;
打印输出:
TrimLeft:Hello          World  !  GoodBye  World

问题是去除右侧无效字符的正则表达式该怎么写?哪位大侠显个灵,帮助写写看,多谢了。
posted @ 2007-08-01 18:45 shootingstars 阅读(510) | 评论 (5)编辑

六:关于重复的贪婪
我们先来一个例子:
std::string regstr = "(.*)(age)(.*)(\\d{2})";
boost::regex expression(regstr);
std::
string testString = "My age is 28 His age is 27";
boost::smatch what;
std::
string::const_iterator start = testString.begin();
std::
string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{

    std::
string name(what[1].first, what[1].second);
    std::
string age(what[4].first, what[4].second);
    std::cout
<< "Name:" << name.c_str() << std::endl;
    std::cout
<< "Age:" <<age.c_str() << std::endl;
    start 
= what[0].second;
}

我们希望得到的是打印人名,然后打印年龄。但是效果令我们大失所望:
Name:My age is 28 His
Age:27

嗯,查找原因:这是由于"+"号或者"*"号等重复符号带来的副作用,这些符号会消耗尽可能多的输入,使之是“贪婪”的。即正则表达式(.*)会匹配最长的串,而不是匹配最短的成功串。
如何使得这些重复的符号不再“贪婪”,我们在重复符号后加上"?"即可。
std::string regstr = "(.*?)(age)(.*?)(\\d{2})";
boost::regex expression(regstr);
std::
string testString = "My age is 28 His age is 27";
boost::smatch what;
std::
string::const_iterator start = testString.begin();
std::
string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{

    std::
string name(what[1].first, what[1].second);
    std::
string age(what[4].first, what[4].second);
    std::cout
<< "Name:" << name.c_str() << std::endl;
    std::cout
<< "Age:" <<age.c_str() << std::endl;
    start 
= what[0].second;
}
打印输出:
Name:My
Age:28
Name: His
Age:27

posted @ 2007-08-01 17:08 shootingstars 阅读(283) | 评论 (0)编辑

五:regex_search学习
regex_search与regex_match基本相同,只不过regex_search不要求全部匹配,即部份匹配(查找)即可。
简单例子:
std::string regstr = "(\\d+)";
boost::regex expression(regstr);
std::
string testString = "192.168.4.1";
boost::smatch what;
if( boost::regex_search(testString, expression) )
{
    std::cout
<< "Have digit" << std::endl; 
}
上面这个例子检测给出的字符串中是否包含数字。

好了,再来一个例子,用于打印出所有的数字
std::string regstr = "(\\d+)";
boost::regex expression(regstr);
std::
string testString = "192.168.4.1";
boost::smatch what;
std::
string::const_iterator start = testString.begin();
std::
string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{
    std::cout
<< "Have digit:" ; 
    std::
string msg(what[1].first, what[1].second);
    std::cout
<< msg.c_str() << std::endl;
    start 
= what[0].second;
}
打印出:
Have digit:192
Have digit:168
Have digit:4
Have digit:1

posted @ 2007-08-01 15:12 shootingstars 阅读(415) | 评论 (0)编辑

四:regex_match例子代码学习
1 我们经常会看一个字符串是不是合法的IP地址,合法的IP地址需要符合以下这个特征:
  xxx.xxx.xxx.xxx 其中xxx是不超过255的整数
正则表达式找到上面的这种形式的字符串相当容易,只是判断xxx是否超过255就比较困难了(因为正则表达式是处理的文本,而非数字)
OK,我们先来处理一个数字,即:xxx。找到一种表达式来处理这个数字,并且保证这个数字不会超过255
第一种情况:x,即只有一个数字,它可以是0~9 ,用\d 表示
第二种情况:xx,即有两个数字,它可以是00~99,用\d\d 表示
第三种情况:xxx,这种情况分为两种,一种是 1xx,可以用 1\d\d 表示
                                   另外一种是 2xx,这又分为两种 2[01234]\d
                                                             和 25[012345]
好了组合起来
1?\d{1,2}|2[01234]\d|25[012345]
既可以标识一个不大于255的数字字符串

嗯,我们现在需要重复这种情况既可:
(1?\d{1,2}|2[01234]\d|25[012345])\.(1?\d{1,2}|2[01234]\d|25[012345])\.(1?\d{1,2}|2[01234]\d|25[012345])\.(1?\d{1,2}|2[01234]\d|25[012345])

呵呵,长是长了点,我试图用boost支持的子表达式缩短,但是没有达到效果,请各位了解boost的正则表达式的达人指点:
(1?\d{1,2}|2[01234]\d|25[012345])\.\1$\.\1$\.\1$
(参看反向索引:http://www.boost.org/libs/regex/doc/syntax_perl.html
似乎反向只能匹配与第一个字符完全一样的字符串,与我们的需求不同)

Example:
std::string regstr = "(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])";
boost::regex expression(regstr);
std::
string testString = "192.168.4.1";
if( boost::regex_match(testString, expression) )
{
    std::cout
<< "This is ip address" << std::endl;
}
else
{
    std::cout
<< "This is not ip address" << std::endl;
}

2 我们来看看regex_match的另外一个函数原型
template <class ST, class SA, class Allocator, class charT, class traits>
    bool regex_match(const basic_string<charT, ST, SA>& s,
    match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
    const basic_regex <charT, traits>& e, match_flag_type flags = match_default);

template <class BidirectionalIterator, class Allocator, class charT, class traits>
bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
match_results<BidirectionalIterator, Allocator>& m,
const basic_regex <charT, traits>& e,
match_flag_type flags = match_default);
 
注意参数m,如果这个函数返回false的话,m无定义。如果返回true的话,m的定义如下

Element

Value

m.size()

e.mark_count()

m.empty()

false

m.prefix().first

first

m.prefix().last

first

m.prefix().matched

false

m.suffix().first

last

m.suffix().last

last

m.suffix().matched

false

m[0].first

first

m[0].second

last

m[0].matched

true if a full match was found, and false if it was a partial match (found as a result of the match_partial flag being set).

m[n].first

For all integers n < m.size(), the start of the sequence that matched sub-expression n. Alternatively, if sub-expression n did not participate in the match, then last.

m[n].second

For all integers n < m.size(), the end of the sequence that matched sub-expression n. Alternatively, if sub-expression n did not participate in the match, then last.

m[n].matched

For all integers n < m.size(), true if sub-expression n participated in the match, false otherwise.

Example:
std::string regstr = "(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])";
boost::regex expression(regstr);
std::
string testString = "192.168.4.1";
boost::smatch what;
if( boost::regex_match(testString, what, expression) )
{
    std::cout
<< "This is ip address" << std::endl;
    
for(int i = 1;i <= 4;i++)
    {
        std::
string msg(what[i].first, what[i].second);
        std::cout
<< i << "" << msg.c_str() << std::endl;
    }
}
else
{
    std::cout
<< "This is not ip address" << std::endl;
}
这个例子会把所有的IP的单个数字答应出来:
This is ip address
1:192
2:168
3:4
4:1
posted @ 2007-08-01 11:23 shootingstars 阅读(430) | 评论 (0)编辑