转到正文

存档

分类: 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];

静态存储区(全局变量)

继续阅读

"++i" return a reference of the "i" object, it's a left-hand value.
It's more effective than "i++".

int i = 0;
++i  =4;
std::cout << i << std::enld; // 4

Reference:

http://knightkid.blog.35.cn/2009/11/14/zhuanguanyuiheizaiczhongdexiaolvwenti/

In this example, I show some simple random generator examples using C++.

It simulates the generator of INT and FLOAT. The generator format is [a,b] or [a,b).

//////////////////////////////////////////////////////////////////////////
//
// Test the random generator of INT and float
//

#include <iostream>
#include <time.h>

void main()
{
	TestRandomIntFloatACM();
}

static void TestRandomIntClose(int ,int );
static void TestRandomFloatClose(float ,float);
static void TestRandomInt(int ,int );
static void TestRandomFloat(float ,float);

继续阅读

when you use C-sytle scanf function, you should notice the usage of data typpe in "scanf". Everyone knows that "int" data type is an integer, its data format in "scanf" is "%d". So, you may think that "short" type is the same as "int", and you may write codes like this:

void main()
{
	const int num = 3;
	short *a = new short[num];

	scanf("%d %d %d",&a[0],&a[1],&a[2]); // error!

	for(int i=0;i < num;++i)
		printf("%d ",a[i]);

	delete []a;

	system("pause");
}

The above code is not right. The "short" type is not the same as "int" type. If you use "%d" to get "short" type data, the memory heap may be crashed because "int" type data is larger than 继续阅读

If you meet this error in VS, it's because you are mixing code that was compiled with /MD (use DLL version of CRT) with code that was compiled with /MT (use static CRT library). That cannot work, all source code files must be compiled with the same setting. Given that you use libraries that were pre-compiled with /MD, almost always the correct setting, you must compile your own code with this setting as well.

Reference:

[1]. http://stackoverflow.com/questions/2728649/error-lnk2005-xxx-already-defined-in-msvcrt-libmsvcr100-dllc-something-libcm

The memory management in C++ is really a headache.

This evening I run my program, and get error: "Memory crash _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)" at runtime.  This error may be caused by several reasons:

1.  Write buffer overflow[1].

// allocate 10 bytes
char* ptr = new char[10];

// write to the 11th. byte
ptr[10] = 0;

You wrote outside of the allocated range, and the app will most likely crash.

2. You delete a memory block more than once[2].

You may new one memory segment, and you have one pointer pointed to this memory segment. And you assign another pointer to this pointer. So, the tragedy may happen when you 继续阅读

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