解释内存静态堆分配

时间:2019-04-05 18:55:06

标签: c

void Heap_Init(void){ int i;
int32_t *pt;
  pt = FreePt = &Heap[0];
  for(i=1; i<NUM; i++){
    *pt = (long)(pt+SIZE);// the first word of a free block points to another free block
    pt = pt + SIZE;       // value of pointer pt should increase by 4*SIZE
  }
  *(long*)pt = NULL;      // the last free block points to NULL
}

我想了解这行:

*pt = (long)(pt+SIZE);// the first word of a free block points to another free block

为下一个pt + size分配指针的值* p将如何工作? 有人可以想像一下吗?

#define SIZE 4  // number of 32-bit words in each block
#define NUM 5   // number of blocks
#define NULL 0  // definition of empty pointer
int32_t *FreePt;   // points to the first free block
int32_t Heap[SIZE*NUM];

该功能也如何工作?

void Heap_Release(int32_t *pt){
int32_t *oldFreePt;
  oldFreePt = FreePt;
  FreePt = pt;            // newly freed block is first
  *pt = (int32_t)oldFreePt;  // newly freed block points to previous first
}

0 个答案:

没有答案