转到正文

存档

标签: C++

When we use C++ pointer, we should save the pointer and the original pointer should not be thrown away in our program.

A bad usage example about C++ pointer is :

	int *a = new int;
	*a = 2;
	int b = *a;
	delete &b;

We have a pointer "a", we assign its value to object "b" and we throw away the pointer "a". And we delete object "b" when we want to release the memory what pointer "a" has allocated.

However, we will meet "Memory Leak" error when we want to release(delete) object "b",( we want to release pointer "a" , but this pointer is lost. We will release object "b" because pointer is assigned to object "b".)

Another wrong usage example is :

继续阅读

when  I read the code ( From book "Beginning Linux Programming 4th Edition" )

int stat(const char *path, struct stat *buf);

I found that this function name "stat" is equal to the struct name "stat". First, I feel a little surprised. How the struct name can be equal to the name of function? Why the G++(GCC) compiler doesn't throw out complains?

Then I tried the following codes which are written by me:

struct statExample
{

};

void statExample(struct statExample *buf=NULL)
{
	printf("Nothing\n");
}

void main()
{
	struct statExample x;
	statExample(&x);
}

继续阅读

中午看《编程之美》的第2.5节中有这么一小段程序:

float array[100 000 000];

这个1亿个float变量,这样就有8亿个字节,转化为二进制单位G,就是小于等于1G。这样大小的数组量只能用堆(动态申请内存变量)或者静态存储区(全局变量)来申请。

好了,转入今天要讲的正题。到底栈、堆、静态存储区能申请的最大分配大小是多少呢?

栈(stack)

栈大小与编译器有关。

默认情况下,visual studio 2010 的栈大小为1M。但在平时应用程序中,由于函数会使用栈结果,所以只能用略小于1M大小的栈。
对于64位和32位程序,结果都是一样的,因为VS2010已经设定好了默认的栈大小了。

	const int nStackSize = 249036; // 这是0.95M
	int b[nStackSize];

	for(int i=0;i< nStackSize;++i)
		b[i] =0;

	std::cout << b[nStackSize-1];

静态存储区(全局变量)

继续阅读

This is a C++ project that describe the usage of Strategy Pattern design
The aim is to implement one task which is in the page 25 of <Head First> ( Chinese version ).

creatd by Knight 2010.07.15
www.liaoqiqi.com/blog

Full Codes:

继续阅读

This is a C++ project that describe the usage of  Builder Pattern design and Decorator Pattern design.
The aim is to implement one task which is in the page 614 of <Head First> ( Chinese version ).

File List:

  • Vocation.h :   Implement Decorator Pattern design
  • Vocation.cpp : Implement Decorator Pattern design
  • Builder.h : Implement Builder Pattern design
  • Builder.cpp : Implement Builder Pattern design
  • Test.cpp : a test driver

There are several important tips:
1. Java have memory garbage, but C++ don't have. So you should be careful when you use Decorator Pattern design.
The trick is that you should write destructor for every decorator class. ( ex, VocationHotel, VocationTicket )

creatd by Knight 2010.07.14
www.liaoqiqi.com/blog

UML Chart:

继续阅读

There are some bad programming habits in my experience which can lead to Memory Leak:

To be continued....

I wrote a single pattern with C++ implementation[1] article the day before yesterday. However, this version has one fatal problem: it has Memory Leak  error! This is because you use "new" to malloc memory explicitly, but you never delete it manually. Although OS will help you free this heap, you program reallly exist memory leak problem.

It's well known that Java has garbage collector, so he never thinks of freeing the memory. But we are C++ programmers, we should free heap memory if we new/malloc them.

Ok, Now I'll introduce my C++ single pattern correct version:
继续阅读

I read <Effective C++> at one time, and I know there is a criterion which is "Never call virtual functions during construction and destruction". But as we all know, genuine knowledge comes from practice. We will never have in-depth understanding of knowledge unless we touch them by ourselves.

Today I want to write a class with Factory patterns in C++.

class DemoModelClass
{
public :
    // common functions
    DemoModelClass()
    {
        CreateDemoModelResourceFactory();   // 1st problem!
        CreateOtherData();
    }    

private:
    virtual void CreateDemoModelResourceFactory()=0{};   // 2nd problem!
    void CreateOtherData(){.....};
}

继续阅读

www.liaoqiqi.com网站PR查询 博客简洁版 博客Google_Site_Map 博客Baidu_Site_Map ?