C++ memory management copy memcpy, allocate malloc and release free

1. Memory copy memcpy

  • C library function void *memcpy(void *str1, const void *str2, size_t n) copies n bytes from storage area str2 to storage area str1.
#声明:
void *memcpy(void *str1, const void *str2, size_t n)

#**********************
str1 -- 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。
str2 -- 指向要复制的数据源,类型强制转换为 void* 指针。
n -- 要被复制的字节数。
- 该函数返回一个指向目标存储区 str1 的指针。

1.1 Copy character data

// 将字符串复制到数组 dest 中
#include <stdio.h>
#include <string.h>
 
int main ()
{
    
    
   const char src[50] = "http://www.runoob.com";
   char dest[50];
 
   memcpy(dest, src, strlen(src)+1);
   printf("dest = %s\n", dest);
   
   return(0);
}

#******************
output:
dest = http://www.runoob.com
  • Copy 6 consecutive characters starting from the 11th character in s to d:
#include <stdio.h>
#include<string.h>
 
int main()
 
{
    
    
  char *s="http://www.runoob.com";
  char d[20];
  memcpy(d, s+11, 6);// 从第 11 个字符(r)开始复制,连续复制 6 个字符(runoob)
  // 或者 memcpy(d, s+11*sizeof(char), 6*sizeof(char));
  d[6]='\0';
  printf("%s", d);
  return 0;
}

#********************
output:
runoob

1.2 Copy image data

  • C++_ cache camera image data to memory
#相机原始输出的图像数据是Byer格式,可以塞进内存中的图像数组

unsigned char* imgData[150] ;//创建图像数据数组
 
//初始化时申请缓存150张图像的内存空间
for (int i = 0; i < 150; i++)
{
    
    
	imgData[i] = new unsigned char[1280 * 1024];
}
 
//采集到图像后将图像数据拷贝到数组中,pBuffer为相机图像帧数据的内存地址
memcpy(imgData[Pic_num], pBuffer, 1280 * 1024);
 
//完成操作后释放申请的内存空间
for (int i = 0; i < 150; i++)
{
    
    
	delete[] imgData[i];
}
  • If there are 1000 grayscale images of 100*100, copy the data:
//*****************将若干份图像数据拷贝到指定目标中************************************
// 申请需要开辟的目标缓冲区的大小
const int ImageSize = 100 * 100;
const int ImageNums = 1000;
 
char* pData = new char[ImageNums * ImageSize ];  // 1000张100*100的灰度图像
int index = 0;
for(int i = 0; i < ImageNums ; i++)
{
    
    
    // 假设图像的数据从文件中读取
    CString strImagePath;
    strImagePath.Format("imageData%d",i);
    Mat src = imread(strImagePath,0);  // 灰度图像加载 大小为100*100
    // 将图像数据拷贝到指定的缓冲区中
    memcpy(pData + index,src.data,ImageSize);
    index += ImageSize;
}
 
//*********************将指定目标中的数据读取出来,拆分为一张张图像*****************
// 上述将1000张图像存入指定的缓存中,那么我们如何将图像取出来呢
index = 0;
for(int i = 0; i < ImageNums ; i++)
{
    
    
    Mat dst(100,100,CV_8UC1); // 创建图像
    memcpy(dst.data,pData + index,ImageSize);
    index += ImageSize;
 
    // 已经将图像数据拷贝到指定图像中了
    // 接下来对图像做相关的处理
}
 
// 释放指针
delete[] pData;
pData = NULL;
  • Convert char data to float:
    When passing parameters, convert the pointer of float type to pointer of char type, but a float occupies four bytes, and a char occupies one byte, so the problem lies in char data, data[0 ] occupies one byte, data[1] occupies one byte, and two floats are passed, that is, eight bytes, but we only use the first two bits of eight bytes, namely data[0], data[1].
#include <iostream>
using namespace std;
float calc(char *data)
{
    
    
    //目标数据为float型指针,内存为2*4 = 8 个字节
	float fdata[2];
	//将拷贝内存大小设为目标数据内存大小
	memcpy((char*)fdata,data,sizeof(float)*2);
	return fdata[0] * fdata[1];
}
 
int main()
{
    
    
	float data[2] = {
    
    1,2};
	float ret = calc((char*)data);
	cout << ret << endl;
}
double* featureData=new double[nSamples * nFeatures];
//featureData赋值代码省略
Mat trainFeature(nSamples, nFeatures, CV_64FC1, Scalar(0));
memcpy(trainFeature.data, featureData, (size_t)nSamples * nFeatures * sizeof(double));
  • Example of the efficiency of copying data
    insert image description here

insert image description here

2. Memory allocation malloc

  • The C library function void *malloc(size_t size) allocates the required memory space and returns a pointer to it.
//声明:
//size -- 内存块的大小,以字节为单位。
//返回值:该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NULL。
void *malloc(size_t size)
  • use
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
    
    
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %u\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
 
   free(str);
 
   return(0);
}

output:

String = runoob,  Address = 3662685808
String = runoob.com,  Address = 3662685808

3. Memory release free

  • The C library function void free(void *ptr) frees the memory space allocated by calling calloc, malloc or realloc before.
//ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果传递的参数是一个空指针,则不会执行任何动作。
//返回值该函数不返回任何值。
void free(void *ptr)
  • Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    
    
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 释放已分配的内存 */
   free(str);
 
   return(0);
}

output:

String = runoob,  Address = 0x7fe4e4c02b10
String = runoob.com,  Address = 0x7fe4e4c02b10

reference:

1. C++ how to copy multiple copies of data of the same size and copy out multiple copies of data of the same size (usage of memcpy)
2. Time-consuming test using the memcpy function (copying different sizes of data takes different time)

Guess you like

Origin blog.csdn.net/yohnyang/article/details/130367802