memcpy_s,strcpy_s or strcpy, memcpy? Usage of memset()

Posted by buckit on Thu, 03 Mar 2022 21:25:51 +0100

Choose to use memcpy_s,strcpy_s or strcpy, memcpy? Usage of memset()

memcpy_s,strcpy_ The s function explicitly specifies the size of the target memory, which can clearly expose the problem of memory overflow, while ordinary strcpy and memcpy will not.

In order to ensure sufficient space for memory copy and prevent clerical errors, [try to use memcpy_s instead of memcpy].
(1)memcpy_ Usage of s():

errno_t memcpy_s(
   void *dest,
   size_t numberOfElements,
   const void *src,
   size_t count 
);  // Note that numberofelements > = count, otherwise an exception will occur

memcpy_s copy the count byte of src to dest; If dest or src is a null pointer, or numberOfElements is a buffer that is too small, an exception is thrown.

void Raw2Mat(CameraPublishDecodedStruct src_yuv_img,cv::Mat &frame) {
  std::shared_ptr<hiai::ImageData<u_int8_t>> input_image =
  input_image->size = src_yuv_img.raw_data.size();
  // input_image->data = std::shared_ptr<u_int8_t>(new 
  // 		u_int8_t[src_yuv_img.raw_data.size()]); 
  // memcpy_s(input_image->data.get(), src_yuv_img.raw_data.size(),
  //, src_yuv_img.raw_data.size());
  //The two sentences noted above can be replaced by the following sentence
  input_image->data = std::shared_ptr<u_int8_t>(
      const_cast<u_int8_t *>(, [](u_int8_t *p) {});
  input_image->height = src_yuv_img.height;
  input_image->width = src_yuv_img.width;

  cv::Mat rgb_img(input_image->height, input_image->width, CV_8UC3);
  cv::Mat yuyv_img =
      cv::Mat(input_image->height * 3 / 2, input_image->width, CV_8UC1,
              static_cast<void *>(input_image->data.get()));
  cv::cvtColor(yuyv_img, rgb_img, cv::COLOR_YUV420sp2RGB);
  frame = rgb_img;

(2) Usage of memset() function
Detailed description of memset function
1)void *memset(void *s,int c,size_t n)
General function: set the value of the first n bytes of the opened memory space s to the value c.
2) The memset() function is often used for memory space initialization. For example:

char str[100];

3)memset can easily empty variables or arrays of a structure type. For example:

	struct sample_struct{
	           char csName[16];
	            int iSeq;
	            int iType;
	For variables:
	struct sample_strcut stTest;
	In general, empty stTest How to:
	use memset It is very convenient:
	memset(&stTest,0,sizeof(struct sample_struct));
	If array:
	struct sample_struct TEST[10];
	memset(TEST,0,sizeof(struct sample_struct)*10);
	#include <mem.h>
	void* memset(void* s, int c, size_t n){
	          unsigned char* p = (unsigned char*) s;
	          while (n > 0) {
	                     *p++ = (unsigned char) c;
	          return s;

Memset() function, which can set the entire array to a specified value byte by byte. The memset() function is in MEM H header file declares that it takes the starting address of the array as its first parameter, the second parameter is to set the value of each byte of the array, and the third parameter is the length of the array (number of bytes, not number of elements). Its function prototype is:
void *memset(void*,int,unsigned);
Where void * indicates the address.
For example, the following code passes the array as a parameter to the standard function memset() to set the array to all 0:

void main()
	int ia1[50];
	int ia2[500];

The first argument of memset() is the array name. The array name is used as the parameter, that is, the array is used as the parameter. It is just the starting address of an array.
In the function memset() stack area, the first, second and third parameters are from the return address up. The content in the first parameter is the starting address of the array ia1 defined in the main () function. The second parameter is the value set for the array (0), and the third parameter is the length of the array (50 * 2). When the function returns, all the contents in the array of the main() function are set to 0.

Topics: C++