临时变量的清除,是不是一定要进行?

natfit 2011-07-07 03:09:59
刚学C++,对基本的还是不清楚,网上也没有多少有用的东西!(胡乱拼了个成形的,结果debug没有问题,只是内存泄漏,但release就会有问题!所以先弄清一些基本的先)

请各位指教!

CString dAESHelper::encrypt( CString text )
{
USES_CONVERSION;
char * pText = W2A(text);
int maxLen = getChipherLen(strlen(pText));
char * pTmp = new char[maxLen];
int tmpLen = encrypt(pText, strlen(pText), pTmp);

maxLen = maxLen*2+2; //其实本来只需要maxlen*2就够了,但AtlHexEncode方法体里需要大于maxlen*2+1
char *hexData = new char[maxLen];
AtlHexEncode((LPBYTE)pTmp, tmpLen, hexData, &maxLen);
hexData[maxLen] = '\0';

CString rst;
rst.Format(L"%s", A2W(hexData));

delete[] pTmp;
delete[] hexData;
return rst;
}

以上的方法中:
1、char * pText 是不是不能delete
2、pTmp和hexData是不是还要加=NULL
3、如果我还有个临时变量:
char * aaa;
aaa=text.AllocSysString();

这个aaa是不是在定义在栈上,函数执行完后自动就清除了,不用我手工delete?
4、如果我还在里面定义了Cstring bbbb;bbbb=text;
bbbb是不是根本就不用去管它,它会自己析构?
...全文
274 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-07-07
  • 打赏
  • 举报
回复
使用MSDN中的例子pwalk查看内存分配详情。
natfit 2011-07-07
  • 打赏
  • 举报
回复
多谢各位!
luciferisnotsatan 2011-07-07
  • 打赏
  • 举报
回复
Example
The following example demonstrates the use of CStringT::AllocSysString.

Copy Code
//typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString;

CAtlString str("This is a test string!");
BSTR bstr = str.AllocSysString();

// bstr now contains "This is a test string!", and can be
// passed to any OLE function requiring a BSTR.
// Normally, if you pass the BSTR, you will
// need to free the string after returning from the function call.

luciferisnotsatan 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 natfit 的回复:]

3、如果我还有个临时变量:
CString的BSTR AllocSysString() const;
返回类型是BSTR,之后用
VOID SysFreeString(
BSTR bstr
);
释放


那是不用我释放的了?
[/Quote]


Allocates an Automation-compatible string of the type BSTR and copies the contents of the CStringT object into it, including the terminating null character.


BSTR AllocSysString() const;


Return Value
The newly allocated string.

Remarks
A CMemoryException Class is thrown if insufficient memory exists. This function is normally used to return strings for Automation.

Commonly, if this string is passed to a COM function as an [in] parameter, then this requires the caller to free the string. This can be done by using SysFreeString, as described in the Platform SDK. For more information, see Allocating and Releasing Memory for a BSTR.

For more information about OLE allocation functions in Windows, see SysAllocString in the Platform SDK.

hai040 2011-07-07
  • 打赏
  • 举报
回复
hexData[maxLen] = '\0';
越界了
一叶之舟 2011-07-07
  • 打赏
  • 举报
回复
使用了new一定要delete
northcan 2011-07-07
  • 打赏
  • 举报
回复
char * pText 是不是不能delete?
不能,new出来的指针才需要delete
至善者善之敌 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 luciferisnotsatan 的回复:]
1、char * pText 是不是不能delete
W2A是个宏,里面用了_alloca,在栈上分配。不用你free

Allocates memory on the stack. This function is deprecated because a more secure version is available; see _malloca.


void *_alloca……
[/Quote]

+++1 ,解释的很详细了
natfit 2011-07-07
  • 打赏
  • 举报
回复
3、如果我还有个临时变量:
CString的BSTR AllocSysString() const;
返回类型是BSTR,之后用
VOID SysFreeString(
BSTR bstr
);
释放


那是不用我释放的了?
luciferisnotsatan 2011-07-07
  • 打赏
  • 举报
回复
Allocates memory on the stack. This function is deprecated because a more secure version is available; see _malloca.


void *_alloca(
size_t size
);



Parameters
[in] size
Bytes to be allocated from the stack.

Return Value
The _alloca routine returns a void pointer to the allocated space, which is guaranteed to be suitably aligned for storage of any type of object. If size is 0, _alloca allocates a zero-length item and returns a valid pointer to that item.

A stack overflow exception is generated if the space cannot be allocated. The stack overflow exception is not a C++ exception; it is a structured exception. Instead of using C++ exception handling, you must use Structured Exception Handling (SEH).

Remarks
_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits (not when the allocation merely passes out of scope). Therefore, do not pass the pointer value returned by _alloca as an argument to free.

There are restrictions to explicitly calling _alloca in an exception handler (EH). EH routines that run on x86-class processors operate in their own memory frame: They perform their tasks in memory space that is not based on the current location of the stack pointer of the enclosing function. The most common implementations include Windows NT structured exception handling (SEH) and C++ catch clause expressions. Therefore, explicitly calling _alloca in any of the following scenarios results in program failure during the return to the calling EH routine:

Windows NT SEH exception filter expression: __except (_alloca () )

Windows NT SEH final exception handler: __finally {_alloca () }

C++ EH catch clause expression

However, _alloca can be called directly from within an EH routine or from an application-supplied callback that gets invoked by one of the EH scenarios previously listed.

luciferisnotsatan 2011-07-07
  • 打赏
  • 举报
回复
1、char * pText 是不是不能delete
W2A是个宏,里面用了_alloca,在栈上分配。不用你free

Allocates memory on the stack. This function is deprecated because a more secure version is available; see _malloca.


void *_alloca(
size_t size
);

2、pTmp和hexData是不是还要加=NULL
加NULL是防止野指针,在这代码里,不加也可以

3、如果我还有个临时变量:
CString的BSTR AllocSysString() const;
返回类型是BSTR,之后用
VOID SysFreeString(
BSTR bstr
);
释放

4、如果我还在里面定义了Cstring bbbb;bbbb=text;
是的

ryfdizuo 2011-07-07
  • 打赏
  • 举报
回复
delete和new对应:
char * pTmp = new char[maxLen];
char *hexData = new char[maxLen];
只有两个地方new,对应delete[] pTmp; delete[] hexData;
CString的构造函数和赋值操作符 会进行深拷贝。

natfit 2011-07-07
  • 打赏
  • 举报
回复
要用啊,用了半年时间,改变了个一个很大的工程,但也是仅仅能运行而已,现在要优化它!
bdmh 2011-07-07
  • 打赏
  • 举报
回复
没有new的,就不要delete,delete指针后,最好设置为NULL,aaa你不用管,不过要看你AllocSysString是怎样分配的
healer_kx 2011-07-07
  • 打赏
  • 举报
回复
对你来说确实很复杂了。。。
luciferisnotsatan 2011-07-07
  • 打赏
  • 举报
回复
刚学C++,写这么复杂的东西干嘛??

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧