在编程中有时需要使用二维数组作为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;