How to simplify the allocation and release of temporary memory

Source: Internet
Author: User
Tags define int size

Description: When compiling a C + + program, one of the most annoying problems is the allocation and release of temporary resources, because the logic of the program is often complex, and there are many possibilities of throwing exceptions, and in order to properly handle run-time exceptions, we have to release a temporary resource that has been allocated successfully wherever possible to throw an exception. , and then return to ensure that the program's robust stability and system clean, so the program may be everywhere Delete, free and other statements, it is very complicated! Is there a way to allocate and use temporary resources regardless of the funeral?

In the Delphi can use try...finally ... The protection of some resources, but in C++builder as if there is no try...finally such a grammatical structure, so in the program in order to properly free memory, such as temporary resources, have to write the release statement delete anywhere possible error to protect the limited resources. There is actually a simpler way to define a common class for temporary resources, to encapsulate the new/delete statements of C + +, and to define the following classes:

TMemory{
public:
void *ptr;
public:
TMemory(int size){ ptr=(void*)new char[size]; _WINAPI_::ZeroMemory(ptr,size);}
~TMemory(void){ if(ptr) delete ptr; }
};
应用示例如下:
void TMainForm::function(int size)
{
TMemory mem(size); //注意捕获异常:EOutOfMemory!
char *str=(char *)mem.ptr; //取得分配的内存指针
some_func(str); //其它操作
return; //直接返回,而不必担心释放内存,即使some_func中发生异常同样会自动释放内存
}

How to simplify the setting and restoration of waiting light object?

--------------------------------------------------------------------------------

The principle of problem solving is the same as the definition of class:

#pragma warn -bei //关闭枚举变量赋值的警告
class TWaitCursor{//自动处理等待光标,当函数调用栈解体时自动还原光标
private:
TCursor oldc;
public:
TWaitCursor(void) : oldc(Screen->Cursor){ Screen->Cursor = crHourGlass; }
~TWaitCursor(void){ Screen->Cursor = oldc; }
};

Examples of applications are as follows:

void TMainForm::function(void)
{
some_opr(...); //其它操作
TWaitCursor waitit; //变为等待光标
some_func(...); //其它操作
return; //直接返回,光标会自动还原
}

When you create a temporary variable for the Twaitcursor class, the cursor changes to the wait state, and the cursor is automatically restored when the function returns.

Note: When creating an automatic variable, you should use the following method to avoid automatic optimization of the Borland compiler:

Twaitcursor wait;

Instead of being defined as the following:

Twaitcursor wait ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.