C++ entry must learn library function memset

1. Introduction to memset

memset is the string initialization function in string.h of C language, but it is also often used for the initialization of ordinary arrays. Its advantage is that it is simple and easy to use, and the data can be initialized with one line of code. Of course, this can be replaced by for loop assignment of.

Second, the basic usage of memset

Function template:
memset(array first address, initial value, initial size)

The first address of the array : the first address of the array can be directly replaced by the array name

Initial value: The range of the initial value is 0 to 127, because memset assigns the array in units of bytes, a byte is 8 bits, and the expressed value is 0 to 127

Initialization size: The initialization size is represented by an integer, indicating how many bytes need to be initialized, usually use sizeof (the first address of the array) to obtain the size that needs to be initialized

It doesn't matter if it looks a bit difficult to understand, after reading the sample code, it will be clear

1. Initialization of char array

memset is originally prepared for char array initialization

Sample code:

#include<iostream>
#include<cstring>//c语言的头文件是string.h,c++的头文件是cstring 
using namespace std;//命名空间 
int main(){
    
    
	char arr[10]; 
	
	memset(arr,'a',10);//从数组首地址开始,初始化10个字节的值为'a';
	cout<<"memset(arr,'a',10)后,数组的值为:"<<endl; 
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr[i]<<' ';
	}
	cout<<endl;
	cout<<endl;
	
	
	memset(arr+2,'b',4);//从数组首地址+2开始,即arr[2]的地址开始,初始化4个字节的值为'b';
	cout<<"memset(arr+2,'b',4)后,数组的值为:"<<endl; 
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr[i]<<' ';
	}
	cout<<endl;
	cout<<endl;
	
	
	cout<<"sizeof arr的值为"<<(sizeof arr)<<endl;//sizeof可以获取到数组的大小,这个代码中sizeof arr的值就是10  
	memset(arr,'c',sizeof arr);//从数组首地址开始,初始化数组大小个字节的值为'c'
	cout<<"memset(arr,'c',sizeof arr)后,数组的值为:"<<endl; 
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr[i]<<' ';
	}
	
	
}

operation result:

memset(arr,'a',10)后,数组的值为:
a a a a a a a a a a

memset(arr+2,'b',4)后,数组的值为:
a a b b b b a a a a

sizeof arr的值为10
memset(arr,'c',sizeof arr)后,数组的值为:
c c c c c c c c c c

2. Initialization of ordinary arrays

The initialization usage of ordinary arrays is a bit limited, because the size of the int type is 32 bits, the size of the long long type is 64 bits, and memset can only initialize the array in units of 8 bits.

For ordinary arrays, we only need to learn two initial values, one is 0 and the other is 0x3f

0: 0 is the simplest, that is to set all bytes to zero, then both int and long long can be initialized to 0

0x3f: 0x refers to the meaning of hexadecimal, indicating that 3f is a hexadecimal number, and the hexadecimal 0-9 is unchanged. Use a, b, c, d, e, f to represent 10 , 11, 12, 13, 14, 15, then the binary corresponding to 3f means 0011 1111. Generally, when we need to use the maximum value, we will not take the real maximum value, because when the maximum value is added, it will be It will be out of bounds, which is risky, we should take half of the maximum value or less, that is, the frequently used 0x3f3f3f3f, and 0x3f can initialize every 8 bits to this value, if it is int type, then It will be initialized to 0x3f3f3f3f, if it is long long, it will be initialized to 0x3f3f3f3f3f3f3f3f

In general, we use 0 when we need to initialize to 0, and use 0x3f when we need to initialize to the maximum value

Sample code:

#include<iostream>
#include<cstring>//c语言的头文件是string.h,c++的头文件是cstring 
using namespace std;//命名空间 
int main(){
    
    
	cout<<"int型数组"<<endl; 
	int arr1[10];//定义一个int型数组 
	 
	memset(arr1,0,sizeof arr1);//初始化数组为0
	cout<<"memset(arr1,0,sizeof arr1)后,数组的值为" <<endl;
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr1[i]<<' ';
	} 
	cout<<endl;
	 
	
	memset(arr1,0x3f,sizeof arr1);//初始化数组为int型最大值的一半 
	cout<<"memset(arr1,0x3f,sizeof arr1)后,数组的值为" <<endl;
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr1[i]<<' ';
	} 
	cout<<endl;
	cout<<endl;
	
	
	
	
	cout<<"long long型数组"<<endl; 
	
	long long arr2[10];//定义一个long long型数组 
	 
	memset(arr2,0,sizeof arr2);//初始化数组为0
	cout<<"memset(arr2,0,sizeof arr2)后,数组的值为" <<endl;
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr2[i]<<' ';
	} 
	cout<<endl;
	 
	memset(arr2,0x3f,sizeof arr2);//初始化数组为int型最大值的一半 
	cout<<"memset(arr2,0x3f,sizeof arr2)后,数组的值为" <<endl;
	for(int i=0;i<10;i++){
    
    //打印 
		cout<<arr2[i]<<' ';
	} 
	 
}

operation result:

int型数组
memset(arr1,0,sizeof arr1)后,数组的值为
0 0 0 0 0 0 0 0 0 0
memset(arr1,0x3f,sizeof arr1)后,数组的值为
1061109567 1061109567 1061109567 1061109567 1061109567 1061109567 1061109567 1061109567 1061109567 1061109567

long long型数组
memset(arr2,0,sizeof arr2)后,数组的值为
0 0 0 0 0 0 0 0 0 0
memset(arr2,0x3f,sizeof arr2)后,数组的值为
4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399 4557430888798830399

Like it!

Guess you like

Origin blog.csdn.net/weixin_52115456/article/details/127645202