在程式設計中有時需要使用二維數組作為buffer,下面是我寫的一個二維數組的demo。
這裡面涉及到幾個記憶體操作的知識點,一個一個的來講。
1. 建立兩個1維數組,用于給二維數組填充值的時候使用。
/*2行27列,一維數組長度為27*/
#define BUFF_SIZE_ROW 2
#define BUFF_SIZE_COL 27
int main()
{
/*初始化兩個原子資料,全1和全2*/
unsigned char data[BUFF_SIZE_COL];
unsigned char data1[BUFF_SIZE_COL];
/*memset整體為數組指派某個值*/
memset(data, 1, BUFF_SIZE_COL);
memset(data1, 2, BUFF_SIZE_COL);
這一段包含兩個知識點,一個是一位數字的建立,和一維數組的一個整體一次性指派。用到的重點函數的memset。
2.動态建立二維數組,用法如下
unsigned char **buffer_a = (unsigned char **)malloc(BUFF_SIZE_ROW * sizeof(unsigned char *));
for(int i = 0; i < BUFF_SIZE_ROW; i++)
{
buffer_a[i] = (unsigned char *)malloc(BUFF_SIZE_COL * sizeof(unsigned char));
}
3.将原子一維數組填充到動态建立的二維數組中
/*将原子資料指派給buffer_a*/
memcpy(buffer_a[0], data1, BUFF_SIZE_COL);
memcpy(buffer_a[1], data1, BUFF_SIZE_COL);
這部分用到的知識點是memcpy,這裡一定要注意copy的記憶體大小。
4.将一個動态建立的二維數組指派給另外一個動态建立的二維數組
/*将buffer_a循環指派給buffer_b*/
for (size_t i = 0; i < BUFF_SIZE_ROW; i++)
{
memcpy(buffer_b[i], buffer_a[i], BUFF_SIZE_COL);
}
這裡注意buffer_b的建立方式與buffer_a一緻。
5.釋放二維數組
/*釋放buffer,先釋放行,再釋放列*/
for(int i = 0; i < BUFF_SIZE_ROW; i++)
free(buffer_a[i]);
free(buffer_a);
buffer_a = NULL;
這裡注意要先free掉列,再釋放行,與建立時相反
下面是完整的測試代碼:
#include <stdio.h>
#include <windows.h>
/*2行27列,一維數組長度為27*/
#define BUFF_SIZE_ROW 2
#define BUFF_SIZE_COL 27
int main()
{
/*初始化兩個原子資料,全1和全2*/
unsigned char data[BUFF_SIZE_COL];
unsigned char data1[BUFF_SIZE_COL];
/*memset整體為數組指派某個值*/
memset(data, 1, BUFF_SIZE_COL);
memset(data1, 2, BUFF_SIZE_COL);
/*建立二維動态數組buffer_a和buffer_b*/
unsigned char **buffer_a = (unsigned char **)malloc(BUFF_SIZE_ROW * sizeof(unsigned char *));
for(int i = 0; i < BUFF_SIZE_ROW; i++)
{
buffer_a[i] = (unsigned char *)malloc(BUFF_SIZE_COL * sizeof(unsigned char));
}
unsigned char **buffer_b = (unsigned char **)malloc(BUFF_SIZE_ROW * sizeof(unsigned char *));
for(int i = 0; i < BUFF_SIZE_ROW; i++)
{
buffer_b[i] = (unsigned char *)malloc(BUFF_SIZE_COL * sizeof(unsigned char));
}
/*将原子資料指派給buffer_a*/
memcpy(buffer_a[0], data1, BUFF_SIZE_COL);
memcpy(buffer_a[1], data1, BUFF_SIZE_COL);
/*将buffer_a循環指派給buffer_b*/
for (size_t i = 0; i < BUFF_SIZE_ROW; i++)
{
memcpy(buffer_b[i], buffer_a[i], BUFF_SIZE_COL);
}
/*輸出A B的第一行資料,驗證是否等于data*/
for (size_t i = 0; i < BUFF_SIZE_COL; i++)
{
printf("a %d, %d\r\n", i, buffer_a[0][i]);
}
for (size_t i = 0; i < BUFF_SIZE_COL; i++)
{
printf("b %d, %d\r\n", i, buffer_b[0][i]);
}
/*釋放buffer,先釋放列,再釋放行*/
for(int i = 0; i < BUFF_SIZE_ROW; i++)
free(buffer_a[i]);
free(buffer_a);
buffer_a = NULL;
for(int i = 0; i < BUFF_SIZE_ROW; i++)
free(buffer_b[i]);
free(buffer_b);
buffer_b = NULL;
system("pause");
return 0;
}
linux kernel
int i, j;
uint8_t **buffer_a = (uint8_t **)kmalloc(BUFF_SIZE * sizeof(uint8_t *), GFP_KERNEL);
uint8_t **buffer_b = (uint8_t **)kmalloc(BUFF_SIZE * sizeof(uint8_t *), GFP_KERNEL);
uint8_t data[SAMP_SIZE];
memset(data, 0xff, SAMP_SIZE);
for (i = 0; i < BUFF_SIZE; i++)
{
buffer_a[i] = (uint8_t *)kmalloc(SAMP_SIZE * sizeof(uint8_t), GFP_KERNEL);
}
for (i = 0; i < BUFF_SIZE; i++)
{
buffer_b[i] = (uint8_t *)kmalloc(SAMP_SIZE * sizeof(uint8_t), GFP_KERNEL);
}
calltime = ktime_get();
for (i = 0; i < BUFF_SIZE; i++)
{
memcpy(buffer_a[i], data, SAMP_SIZE);
}
for (j = 0; j < 100; j++)
{
/* code */
for (i = 0; i < BUFF_SIZE; i++)
{
memcpy(buffer_b[i], buffer_a[i], SAMP_SIZE);
}
}
rettime = ktime_get();
delta = ktime_sub(rettime, calltime);
duration = (unsigned long long )ktime_to_ns(delta) >> 10;
printk("buffer copy use %lld us\r\n", duration);
/*free buffer*/
for (i = 0; i < BUFF_SIZE; i++)
{
kfree(buffer_a[i]);
}
kfree(buffer_a);
buffer_a = NULL;
for(i = 0; i < BUFF_SIZE; i++)
{
kfree(buffer_b[i]);
}
kfree(buffer_b);
buffer_b = NULL;