Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2631466
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2015-01-10 15:44:02

 

// Boost.Function library

//  Copyright Douglas Gregor 2002-2003. Use, modification and
//  distribution is subject to the Boost Software License, Version
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  )

// For more information, see

#include
#include
#include

#include
#include
#include
#include
using namespace std;
static unsigned
func_impl(int arg1, bool arg2, double arg3)
{
  using namespace std;
  return abs (static_cast((arg2 ? arg1 : 2 * arg1) * arg3));
}

// 带隐患返回指针的函数
char *getStr()
{
 char sBuf[12] = "asp";
 return sBuf;
}

bool getSafeStr(char *sOutStr, int nStrLen)
{
 bool bRet = false;
 char sBuf[12] = "asp";
 if ( nStrLen   ;
 }else{
  strcpy(sOutStr, sBuf);
  bRet = true;
 }

 return bRet;
}

int test_main(int, char*[])
{
  using boost::function;
  using namespace boost::lambda;

  function f1 = bind(func_impl, 15, _1, _2);
  function        f2 = boost::lambda::bind(f1, false, _1);
  function              f3 = boost::lambda::bind(f2, 4.0);
  string ss;
  char *pBuf = getStr();
  ss = string(pBuf);
  cout << "ss = " << ss << endl;
  char strBuf[64];
  strcpy(strBuf, getStr());
  string s = string(strBuf);
  cout << "s = " << s << endl;

  memset(strBuf, 0x00, 64);
  getSafeStr(strBuf, 64);
  cout << "strBuf = " << strBuf << endl;
  int n = f3();

  return 0;
}

 以上代码可配合register,Disassembly调试信息。
由于编译的程序在deBug版下正常,在Release下总莫名其妙的退出,经查证,原来有同事写了很多类似getStr()函数,晕!

阅读(942) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~