分类存档: Tech

Wonderland 更新完毕

新主题,支持移动设备20130106-103637.jpg

Test WordPress iOS Client

Just A Test20130105-102945.jpg

Linux C 常用函数

Link

VS bug

// assign_aTest.cpp : Defines the entry point for the console application.
//

#include “stdafx.h”

#include “boost/spirit.hpp”
#include “boost/spirit/actor/ref_actor.hpp”

using namespace boost;
using namespace boost::spirit;

#include

struct MyParser
: public grammar
{
mutable int m_i;

/// Constructor
MyParser()
{
}

/// Grammar definition
template
struct definition
{
definition(MyParser const& self)
{
test = (ch_p(L’@'))
[assign_a(self.m_i, 100)];
}

rule test;

// Start rule
rule const& start() const { return test; }
};
};

struct MyParser2
: public grammar
{
mutable int m_i;

static const int s_i = 100;

/// Constructor
MyParser2()
{
}

/// Grammar definition
template
struct definition
{
definition(MyParser2 const& self)
{
test = (ch_p(L’@'))
[assign_a(self.m_i, s_i)];
}

rule test;

// Start rule
rule const& start() const { return test; }
};
};

int _tmain(int argc, _TCHAR* argv[])
{
struct MyParser pa;
pa.m_i = 0;
parse(L”@”, pa);

struct MyParser2 pa2;
pa2.m_i = 0;
parse(L”@”, pa2);

std::cout<<"Parse1: "<<pa.m_i<<std::endl;
std::cout<<"Parse2: "<<pa2.m_i<<std::endl;

std::cin.get();
return 0;
}

VC在函数中将常数值赋给一个常引用,会导致出函数有效区域后此引用的无效,为一个不确定值。
更一般的测试例程:

// RefTst.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include

using namespace std;

class A
{
private:
const int& m_i;

public:
A(const int& i)
: m_i(i)
{
}

void PrintI()
{
cout<<"I: "<<m_i<<endl;
const int* p = &m_i;
cout<<"P: "<<p<<endl;
}
};

A* GetA()
{
A* pA = new A(123456789);
pA->PrintI();
return pA;
}

void Print(A* pA)
{
int i = 54321;
__int64 j = 13328703583;
pA->PrintI();
}

int _tmain(int argc, _TCHAR* argv[])
{
A* pA = NULL;
pA = GetA();
pA->PrintI();
Print(pA);
delete pA;
cin.get();
return 0;
}

Licensed Memory in 32-Bit Windows Vista

Licensed Memory in 32-Bit Windows Vista

Lambda vs. Function class

#define LAMBDA
#include

#ifdef LAMBDA
int main()
{
int v[10];
for (int i = 0; i < 10; ++i)
v[i] = i;

int cnt = 0;
std::for_each(v, v + 10, [&cnt](int i){
if (i % 2)
{
wprintf(L”even %d\r\n”, i);
++cnt;
}
});

return 0;
}
#else
class Func
{
public:
Func(int& i)
: _i(i)
{}

void operator()(int i)
{
if (i % 2)
{
wprintf(L”even %d\r\n”, i);
++_i;
}
}

private:
int& _i;
};

int main()
{
int v[10];
for (int i = 0; i < 10; ++i)
v[i] = i;

int cnt = 0;
std::for_each(v, v + 10, Func(cnt));

return 0;
}
#endif

详解link

有些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错误信息不能定位到某一行)。或者对语言的一些部分不知道为什么要(或者不要)这样那样设计。了解本文之后,或许会有一些答案。
首先看看我们是如何写一个程序的。如果你在使用某种IDE(Visual Studio,Elicpse,Dev C++等),你可能不会发现程序是如何组织起来的(很多人因此而反对初学者使用IDE)。因为使用IDE,你所做的事情,就是在一个项目里新建一系列的.cpp和.h文件,编写好之后在菜单里点击“编译”,就万事大吉了。但其实以前,程序员写程序不是这样的。他们首先要打开一个编辑器,像编写文本文件一样的写好代码,然后在命令行下敲
cc 1.cpp -o 1.o
cc 2.cpp -o 2.o
cc 3.cpp -o 3.o

图片的宽度对齐问题:Beware : Windows Bitmaps have to have a DWORD alligned width :-(

假设有一张图片宽度为6,因为是Format24bppRgb格式(每像素3字节。在以下的讨论中,除非特别说明,否则Bitmap都被认为是24位RGB)的,显然,每一行需要6*3=18个字节存储。对于Bitmap就是如此。但对于BitmapData,虽然BitmapData.Width还是等于Bitmap.Width,但大概是出于显示性能的考虑,每行的实际的字节数将变成大于等于它的那个离它最近的4的整倍数,此时的实际字节数就是Stride。就此例而言,18不是4的整倍数,而比18大的离18最近的4的倍数是20,所以这个BitmapData.Stride = 20。显然,当宽度本身就是4的倍数时,BitmapData.Stride = Bitmap.Width * 3。

画个图可能更好理解。R、G、B 分别代表3个原色分量字节,BGR就表示一个像素。为了看起来方便我在每个像素之间插了个空格,实际上是没有的。X表示补足4的倍数而自动插入的字节。为了符合人类的阅读习惯我分行了,其实在计算机内存中应该看成连续的一大段。

Scan0

|-------Stride-----------|
|-------Width---------|  |
BGR BGR BGR BGR BGR BGR XX
BGR BGR BGR BGR BGR BGR XX
BGR BGR BGR BGR BGR BGR XX
.
.
.

C# 中的 Bitmap.FromFile 文件锁定问题

以前一直用Bitmap bmp = (Bitmap)Bitmap.FromFile(filename);来读取图片,经常会出现就算bmp.Dispose()后文件还是无法删除的情况;
最好的方法为:
Image img = Image.FromFile(filename);
Bitmap bmp = new Bitmap(img);

C++ 模板

模板函数:

template <class T>

T func(T t1)

{

return t1;

}

模板类:

template <class T>

class CA

{

public:

T func(T t1);

}

template <class T>

T CA<T>::func(T t1)

{

return t1;

}