我是否需要担心分配给TList的整数值无效? [英] Do I have to worry about integer values assigned to TList become invalid?

查看:63
本文介绍了我是否需要担心分配给TList的整数值无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有以下过程,在代码注释中有下列问题,分别标记为Q1,Q2等:

Lets say I have the following procedure, I have the following questions marked as Q1, Q2, and so on in the comments of the code:

var
  winHandle: THandle;
...
// At this point, assume aList is empty.
winHandle := FindWindow(...);
aList.Add(Pointer(winHandle));
// Now THandle(aList[0]) = winHandle.

// After this point, variable `winHandle` will go out of scope thus become invalid, so:
// Q1: Do I have to worry about that the value of THandle(aList[0])
//     will be erased in the memory thus become invalid?
// Q2: If yes, what can I do to keep the validity of the value of THandle(aList[0])?
// Q3: If not, do I have to free the memory of THandle(aList[0]) when freeing aList?

我想我缺乏有关Delphi内存管理的一些知识。

I think I lack some of the knowledge about Delphi's memory management. Thanks.

推荐答案

Q1否。整数是一种值类型。分配整数时,将获取其值的副本。碰巧的是,在这种情况下, TList 接受 Pointer ,但接受 Pointer 只是一个地址,是一个值类型,在概念上,出于赋值目的,它被视为整数值类型。

Q1 No. An integer is a value type. When you assign an integer, you take a copy of its value. As it happens, in this case, TList accepts Pointer, but Pointer is simply an address, a value type, and conceptually treated identically as an integer value type for the purpose of assignment.

Q3您不必释放任何内存,因为整数是值而不是引用。

Q3 You do not have to free any memory because the integers are values rather than references.

这篇关于我是否需要担心分配给TList的整数值无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆