C 中的指针与句柄(这些术语用于表达不同的事物吗?)
最近,我读到一个人写的一篇白皮书,其中将指向结构的指针称为句柄。作者显然是以前在windows平台上写过C代码的人。谷歌搜索表明 Windows 程序员通过句柄与系统组件交互。我想知道 Windows 程序员将所有结构指针称为句柄是否是常见做法?或者术语“句柄”是否意味着传达除结构指针之外的其他内容?我作为一个linux C 程序员来问。
我指的白皮书是: 达夫、赫鲁和波索。稀疏基本线性代数子程序概述:BLAS 技术论坛的新标准。 ACM 数学软件汇刊,第 28 卷,第 2 期,2002 年 6 月,第 239-267 页。
Recently, I read a white paper by an individual who refers to a pointer to a struct as a handle. The author was clearly someone who had written C code on the windows platform previously. Googling indicates that windows programmers interact with system components via handles. I am wondering if it is common practice for windows programmers to refer to all struct pointers as handles? Or is the term handle meant to convey something beyond pointer to struct? I am asking as a linux C programmer.
The white paper I am referring to is:
Duff, Heroux, and Pozo. An Overview of the Sparse Basic Linear Algebra Subprograms: The New Standard from the BLAS Technical Forum. ACM Transactions on Mathematical Software, Vol 28, No. 2, June 2002, Pages 239-267.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
术语“句柄”通常表示一些不透明的值,仅对生成它的 API 有意义。在 Win32 中,HANDLE 类型要么是内核内存中的指针(应用程序无论如何都无法访问),要么是某个内核内部数组的索引。
The term handle generally means some opaque value that has meaning only to the API which produced it. In Win32, the HANDLE type is either a pointer in kernel memory (which applications cannot access anyway) or an index into some kernel-internal array.
手柄是一个古老而受人尊敬的概念。
cookie 也是一样的东西。或者一个 GUID。或者从停车场取回汽车的票,或者从高档餐厅取回外套等。
它具有任何独特的价值,当提交给发行人时,可以用来追踪到所提到的实际事物,无论是通过什么方式发行人想要的不透明机制。您可能确切地知道也可能不知道有关该过程的任何信息,也不知道底层的东西是什么(仅在概念上)。
它被 Windows 大量使用,但它肯定不是 Windows 所独有的。
通常不会使用“句柄”来表示“指向结构的指针”。句柄更像是“令牌”而不是“指针”。它指的是某些东西 - 文件、系统资源、内存、状态快照等。但是它到底是什么是基于句柄本身的上下文(即谁发布了句柄)。
句柄在 K&R C 的早期文件系统编程中也大量使用。
A handle is an old and revered concept.
A cookie is much the same thing. Or a GUID. Or a ticket to retrieve your car from a car park, or your coat from a fancy restaurant, etc.
Its any unique value that when presented back to the issuer can be used to track back to the actual thing referred, by whatever opaque mechanism the issuer wants. You may or may not know anything about that process, nor what the underlying thing is, exactly (only conceptually).
It was heavily used by Windows, but it is certainly not unique to Windows.
You would not normally use "handle" to mean "pointer to struct." Handle is more like "token" than like "pointer." It refers to something - file, system resource, memory, state-snapshot, etc. But what-exactly-it-is is based on the context of the handle itself (i.e. who issued the handle).
Handles were also used heavily in early filesystem programming in K&R C.
我使用“句柄”一词来表示指向代表资源(通常是操作系统资源)的“对象”的指针,而指针仅指向某些内存。如果您有某个对象的句柄,则不应尝试直接读取和写入字节,而应通过提供的方法对其进行操作。
通常句柄被实现为不透明的
void *
,这进一步鼓励不要尝试直接取消引用它。I use the word handle to mean a pointer that points to an "object" that represents a resource - often an OS resource, whereas a pointer just points to some memory. If you have a handle to something, you shouldn't try to read and write bytes into it directly, but manipulate it through provided methods.
Often handles are implemented as an opaque
void *
, which is further encouragement not to try to directly dereference it.由于您提到句柄被用作指向结构的指针(如 Windows 程序员所使用的那样),因此我将在该上下文中回答。请注意,显然有许多不同类型的“句柄”,因为它是计算环境中广泛使用的通用概念。当然,您会熟悉文件句柄的概念; Windows 还提供窗把手和许多其他类型的把手。话虽如此:
“内存句柄”(类似于指向结构的指针)是来自 16 位 Windows 编程领域的概念,其中 CPU 中没有内存管理器,所有内存管理都必须完成在软件中。本质上,“句柄”是一种指针,但操作系统可以自由地在句柄引用的内存中移动。您无法使用常规指针来做到这一点,但句柄具有获取和释放实际内存地址的函数。
随着 Win32 的引入(其中 CPU 具有硬件内存管理器),内存句柄的概念变得过时了。其他类型的句柄(例如文件句柄和窗口句柄)仍然存在于 Win32 中,但不是指向结构的指针。
Since you refer to handles being used as a pointer to a structure, as used by a Windows programmer, I'll answer within that context. Please note that there are clearly many different kinds of "handles", as it is a generic concept widely used within the computing environment. Certainly you will be familiar with the concept of a file handle; Windows also offers window handles and many other kinds of handles. Having said that:
A "memory handle" (that is similar to a pointer to a struct) is a concept from the land of 16-bit Windows programming, where there was no memory manager in the CPU and all memory management had to be done in software. Essentially, a "handle" was sort of a pointer, but the OS would be free to move around the memory that the handle referred to. You can't do that with a regular pointer, but the handle had functions that would get and release the actual memory address.
With the introduction of Win32, where the CPU had a hardware memory manager, the concept of the memory handle became obsolete. Other types of handles such as file handles and window handles still exist in Win32, but are not pointers to structs.
在 MacOS 编程的旧时代,早在 OSX 之前,句柄是指向指针的指针。这使得操作系统可以在不使用户指针无效的情况下移动事物。关于何时我们可以假设所指向的对象不会移动有一些规则,我不记得了。
In the old days of MacOS programming, back before OSX, a handle was a pointer to a pointer. That allowed the OS to move things around without invalidating the user's pointers. There were rules on when we could assume the pointed-to object wouldn't move, which I don't remember.
术语句柄用于表示允许您访问另一个对象的任何技术。句柄可以是指针、引用、指向指针的指针等。但它肯定与类、对象等相关。因此句柄不一定总是指向结构的指针。
-广告。
The term handle is used to mean any technique that lets you access to another object. A handle can be a pointer, a reference, a pointer to a pointer, etc.. But sure its related to classes, objects etc. So handle need not always be a pointer to structure.
-AD.
“句柄”一词并非起源于 Windows,尽管它在 Windows 程序员中广泛使用。
在 C 标准库 (stdio.h) 中,文件句柄是指向 C 库使用的数据结构的指针。
纯 Unix 编程使用文件描述符,文件描述符是内核数据结构的索引,但指针在 Unix 中用作句柄已有 30 多年了。
The term "handle" didn't orignate on Windows, though it became widespread among Windows programmers.
In the C standard library (stdio.h), file handles are pointers to a data structure used by the C library.
Pure Unix programming uses file descriptors, which are indexes into a kernel data structure, but pointers have been used as handles in Unix for over 30 years.
“句柄”是一个逻辑术语,而不是物理术语。它意味着作为物理对象的代理,以便对该对象有更深入了解的代码。指向结构的指针就是这样的代理之一,但还有许多其他可能性。
"Handle" is a logical term, not a physical one. It's meant as a proxy to a physical object to code that has more intimate knowledge of the object. A pointer to a struct is one such proxy, but there are many other possibilites.
我可能比大多数受访者年龄都大,我靠在早期(80 年代末)的 Macintosh 以及 16 位和 32 位 Windows 上用 C 编写代码为生。在古代(IBM 大型机可能只有 256k 内存),句柄始终是指向内存指针的指针(或表偏移量)。
正如前面的受访者提到的,这允许操作系统管理指向内存块的指针表,而不会使程序员使用的“句柄”无效。不幸的是,我不记得我们如何保证在使用句柄时分配的块不会被移动。
I'm probably older than most of the respondents, having earned a living coding in C on both the early (late 80s) Macintosh and both 16 and 32-bit Windows. In those ancient times (when an IBM mainframe might have only 256k of memory) a handle was always a pointer (or table offset) to a memory pointer.
As a previous respondent mentioned, that allowed tables of pointers to memory blocks to be managed by the OS without invalidating the "handles" used by the programmer. Unfortunately, I do not remember how we guaranteed that an allocated block would not be moved while we using the handle.
不,在 Windows 程序员中将指针称为句柄并不特别常见,但这样做也没有错。术语“句柄”通常用于描述用于访问某些内容的对象,从这个意义上说,所有指针都是句柄(但并非所有句柄都是指针)。 Win32 的句柄通常不是指针,而是内部操作系统表的索引 - 但这在 Windows 的未来版本中可能会改变。
No, it is not particularly common amongst Windows programmers to refer to pointers as handles, but doing so isn't WRONG either. The term "handle" is usually used to describe something you use to access something through, and in that sense are all pointers handles (but not all handles are pointers). Win32's handles are AFAIK usually not pointers, but instead indices to internal OS tables - but this might change in future versions of Windows.
句柄是对象引用(并非特指 C++ 引用)的通用术语。
指针是句柄的子集,因为它指向对象。
数据库中的外键也是一个句柄,因为它指向其他表中的记录;并且它不是一个指针。
在Windows API环境中,他们使用抽象术语句柄,这样他们就可以在表中使用整数、指针或其他方法,而不会干扰客户端; IOW,定义一个抽象接口。
总之,句柄可以是指针以外的东西,例如整数索引或包含有关该对象的更多详细信息的对象(例如智能指针)。
A handle is a generic term for a reference (not specifically a C++ reference) to an object.
A pointer is a subset of handle, since it points to objects.
A foreign key in a database is also a handle, since it points to records in other tables; and it is not a pointer.
In the Windows API environment, they used the abstract term handle so they could use an integer into a table, a pointer, or other methods, without interfering with the client; IOW, defining an abstract interface.
In summary, a handle can be something other than a pointer, such as an integer index or an object containing more details about the object (such as a smart pointer).
Windows 定义了很多东西的句柄。它们根本不一定是指针——有些是指针,但另一些则是特定表的偏移量之类的东西。有一些是故意混淆的。从窗口到设备上下文再到位图等等,所有东西都有句柄。
无论如何,句柄通常被设计为不透明的数据类型——即您不应该知道有关其值的任何信息,而只知道一组可以使用它来完成各种任务的预定义操作。我相信 C++/CLI 也有一个类似指针的对象,称为句柄。我相信它应该更接近于不透明的数据类型——如果内存允许的话,你不允许对它们进行任何指针算术。
Windows defines handles for many things. They're not necessarily pointers at all -- some are, but others are things like offsets into particular tables. A few are intentionally obfuscated. There are handles for everything from windows to device contexts to bitmaps, and so on.
In any case, a handle is normally intended as an opaque data type -- i.e. you're not supposed to know anything about its value, only a set of predefined operations that can use it to accomplish various tasks. I believe C++/CLI also has a pointer-like object that's called a handle. I believe it's supposed to be closer to an opaque data type though -- if memory serves, you're not allowed to do any pointer arithmetic on them.
句柄通常是不需要直接取消引用的指针。相反,您将它们传递给对底层结构进行操作的 API 调用。
从历史上看,在 Windows 上,句柄不是指针。您可以在使用之前锁定句柄以获取指针,并在使用完毕后将其解锁(并且指针将变得无效)。在分页内存出现之前,老式 Windows 通过交换仅由句柄引用的资源并在锁定时将它们交换回来来进行自己的内存管理。实际上,这使内存管理成为一场噩梦,但允许 Windows 在没有硬件支持的系统上模拟虚拟内存。
Handles are generally pointers that you don't directly need to dereference. Rather you pass them to API calls which operate on the underlying structs.
Historically on Windows, handles were not pointers. You would lock the handle to get a pointer before using it, and unlock it when you were done (and the pointer would become invalid). In the days before paged memory, old-school Windows did it's own memory management by swapping out resources only referenced by handles and swap them back in when they got locked. In practice, this made memory management a nightmare, but allowed Windows to simulate virtual memory on systems without hardware support for it.
指针与句柄绝对不同。指针是内存中未指定的地址。指向结构的指针可以称为“句柄”(通常通过使用“typedef”)。
句柄是编写 Windows 操作系统时使用的概念。
指针是C语言的一部分。
A pointer is definitely different than a handle. A pointer is an address of something unspecified in memory. A pointer to a structure can be called a "handle" (usually by using 'typedef').
A handle is a concept used in writing the windows operating system.
A pointer is a part of the C language.
实际上,指针是一个包含另一个变量地址的变量,但句柄是一个指向指针的指针,即包含另一个指针地址的指针
例如:
Actually a pointer is a variable which contains the address of another variable,but a handle is a pointer to a pointer i.e a pointer which contains the address of another pointer
FOR EX:
关键区别或者如果您更喜欢将其称为句柄的“优点”,则可以尝试推断句柄是否有效,或者如果您更喜欢术语“悬空”。
只要可行,我都会使用句柄。 这是一篇关于优点和实施实践的好文章 。
The key difference or if you prefer to call it the "advantage" of handles is one can try to deduce if the handle is valid or if you prefer the term, "dangling".
I do use handles whenever feasible. Here is a good article on advantages and implementation practices.