二级指针使用注意点

 设:unsigned char* buffer->bitmap = NULL;

 

//初始化:

buffer->bitmap = (unsigned char**)malloc(HIGHT * sizeof(unsigned char*));  //注意点:务必是unsigned char*,不能是unsigned char
 for (int y = 0; y < HIGHT; y++)
 {
  buffer->bitmap[y] = (unsigned char*)malloc(WIDTH * sizeof(unsigned char));
  memset(buffer->bitmap[y], 0, WIDTH*sizeof(unsigned char));
 }

//release

 

为什么要用unsigned char*呢,这是因为bitmap 是一个二级指针,指向一个unsigned char*指针(即地址),并以该unsigned char*指针为起始地址,分配了HIGHT个大小为sizeof(unsigned char*)的内存空间,用来存放HIGHT个指针。每一个指针,指向有WIDTH个unsigned char类型数据的区域。

 

你可能感兴趣的:(null)