[英]How does this segment of C code for a priority queue work?
我对这条线有些不知所措:
Entry** newHeap = (Entry**)malloc(sizeof(Entry*) * newHeapLength);
在此代码中:
/**
* Expands the heap array of the given priority queue by
* replacing it with another that is double its size.
*
* @param pq the priority queue whose heap is to be doubled in size
* return 1 for successful expansion or an error code:
*/
int expandHeap (PriorityQueue *pq)
{
int returnCode = 1;
int newHeapLength = pq->heapLength * 2;
Entry** newHeap = (Entry**)malloc(sizeof(Entry*) * newHeapLength);
if (newHeap != NULL)
{
int index;
for (index = 0; index < pq->heapLength; index++)
{
newHeap[index] = pq->heap[index];
}
free(pq->heap);
pq->heap = newHeap;
pq->heapLength = newHeapLength;
}
else
{
returnCode = -1; // TODO: make meaningful error codes
}
return returnCode;
}
Entry** newHeap = (Entry**)malloc(sizeof(Entry*) * newHeapLength);
| |
newHeap is a malloc allocates a chunk in memory that is the size of
pointer to a a pointer to an Entry times newHeapLength
pointer to an Entry
它只是在运行时为您分配一个数组。 通常,数组的大小必须在编译时指定。 但是这里是在运行时指定的,它是newHeapLength
。 该数组中的每个条目(“单元格”)必须能够在其中存储Entry*
类型的值。 在C中,数组是连续的,因此数组的总大小(以字节为单位)只是两个数字的乘积: sizeof(Entry*) * newHeapLength
。 现在,可以使用newHeap
以通常的方式来寻址此数组:例如newHeap[8]
。 当然,如果8 >= newHeapLength
,这将是访问过分配的区域,这是不好的 。
对于存储10个int
的数组, int ia[10];
, ia
的类型为int *
( 更正:几乎。但是,出于解释的目的,我们可以假装为 )。 在这里,类似地,对于存储类型为Entry*
值的数组,类型为(Entry*)*
。 简单。 :)
当然,您必须将malloc
的返回值转换为您的类型,以便能够使用该地址寻址该数组。 malloc
本身返回一个地址为void*
。 含义。 它所指向的存储单元的大小被称为未知 。 当我们说ia
的类型为int*
,实际上是说它所指向的存储单元的大小为sizeof(int)
。 因此,当我们编写ia[3]
,它实际上被转换为*(ia+3)
,实际上是*(int*)(void*)( (unsigned int)(void*)ia + 3*sizeof(int) )
。 换句话说,编译器仅将sizeof(int)
添加到起始地址三倍,从而“跳过”三个sizeof(int)
宽的内存单元。 对于newHeap[8]
,它将仅“跳”过8个sizeof(Entry*)
宽的存储单元,以获取该数组中第9个条目的地址(从1开始计数)。
另外,请参见哈希数组树 ,以了解几何扩展的替代方法,即代码正在执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.