词条 | Dope vector |
释义 |
In computer programming, a dope vector is a data structure used to hold information about a data object,[1] especially its memory layout. PurposeDope vectors are most commonly used to describe arrays, which commonly store multiple instances of a particular datatype as a contiguous block of memory. For example, an array containing 100 elements, each of which occupies 32 bytes, requires 100*32 bytes. By itself, such a memory block has no place to keep track of how large the array (or other object) is overall, how large each element within it is, or how many elements it contains. A dope vector is a place to store such information. Dope vectors can also describe structures which may contain arrays or variable elements. If such an array is stored contiguously, beginning at memory location M, then it ends at location M+3199. A major advantage of this arrangement is that locating item N is easy: it begins at location M + (N*32). Of course, the value 32 must be known (this value is commonly called the "stride" of the array or the "width" of the array's elements). Navigating an array data structure using an index is called dead reckoning. This arrangement, however (without adding dope vectors) means that having the location of item N is not enough to discover the index N itself; or the stride; or whether there are elements at N-1 or N+1. For example, a function or method may iterate over all the items in an array and pass each one to another function or method, which does not know the item is part of an array at all, much less where or how large the array is. Without a dope vector, even knowing the address of the entire array does not tell you how big it is. This is important because writing to the N+1 element in an array that only contains N elements, will likely destroy some other data. Because many programming languages treat character strings as a kind of array, this leads directly to the infamous Buffer overflow problem. A dope vector reduces these problems by storing a small amount of metadata along with an array (or other object). With dope vectors, a compiler can easily (and optionally) insert code that prevents accidentally writing beyond the end of an array or other object. Alternatively, the programmer can access the dope vector when desired, for safety or other purposes. DescriptionThe exact set of metadata included in a dope vector varies from one language and/or operating system to another, but a dope vector for an array might contain:
A program then can refer to the array (or other dope-vector-using object) by referring to the dope vector. This is commonly automatic in high level languages. Getting to an element of the array costs a tiny bit more (commonly one instruction, which fetches the pointer to the actual data from out of the dope vector). On the other hand, doing many other common operations are easier and/or faster:
Even with a dope vector, having (only) a pointer to a particular member of an array does not enable finding the position in the array, or the location of the array or the dope vector itself. If that is desired, such information can be added to each element within the array. Such per-element information can be useful, but is not part of the dope vector. Dope vectors can be a general facility, shared across multiple datatypes (not just arrays and/or strings)[2] See also
References1. ^Pratt T. and M. Zelkowitz, Programming Languages: Design and Implementation (Third Edition), Prentice Hall, Upper Saddle River, NJ, (1996) pp 114 {{DEFAULTSORT:Dope Vector}}{{compu-prog-stub}}2. ^The design of a template structure for a generalized data structure definition facility. Billy G. Claybrook. ICSE '76: Proceedings of the 2nd international conference on Software engineering. San Francisco, California, USA — October 13 - 15, 1976. Pages 408-413. IEEE Computer Society Press. http://dl.acm.org/citation.cfm?id=807713&CFID=799276124&CFTOKEN=88822581 1 : Arrays |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。