C++ dynamic allocation new & delete delete & initialize memset

1. Dynamically allocate new

1 Introduction

The advantage of using new to create an array: Since the object created by new is established at runtime, it has the advantage of specific analysis of specific situations, so what is specific analysis of specific situations?

To give a very apt example:

For example, you are on vacation and have made a daily visit plan, but suddenly one day the weather is bad or you are in a bad mood. At this time, you don’t want to visit. If you are in the compilation state at this time, the system does not allow it. You have to visit according to the plan, but the runtime state is allowed by the system. At this time, you can stay in the hotel and play to your heart's content. Creating an array with new also has this advantage, that is, the length of the array can be determined according to the situation.


2. Implementation of new

use new newThe new operator must know the data type, and it will apply for enough storage space in the system heap area. If the application is successful, it will return the first address of the memory block; if the application is unsuccessful, it will return a null pointer NULLNULLNULL

n e w new The new operator returns a pointer. The created variable or object is indirectly operated through the pointer, and the dynamically created object itself has no identifier name.


n e w newnew syntax : _

  1. pointer variable name = new newn e w type identifier;
  2. pointer variable name = new newn e w type identifier (initial value);
  3. pointer variable name = new newn e w type identifier [number of memory units];

① For a single variable:

The first definition way:

int *a=new int;

Indicates opening up a storage space for storing integers and returning an address pointing to the storage space, int *a = new intthat is, converting an int intThe address of int type is assigned to the integer pointeraaa


For the second definition:

int *a=new int(6)
cout<< *a <<endl; // 6

It also opens up an integer space and defines a pointer aa pointing to this spacea , but at the same time point to the integer space∗ a *a The value of a is assigned to 6 66


②For array space:

The third way of definition:

int n;
cin>>n;
int* a=new int[n];

For array development, use square brackets [ ] [\ ][ ]  means to open up a continuous segment containingnnn types of memory spaces


2. delete delete

For the memory space that has been opened, we also need to release it manually

delete deleted e l e t e syntax:

  1. delete deleted e l e t e pointer name of dynamically allocated memory
  2. delete[] delete[\]d e l e t e [ ]  The pointer name of the dynamically allocated memory

① For a single variable:

int* a=new int;
delete a;

int* b=new int(6);
delete a;

②For array space:

int* a=new int[n];
delete[] a;

Note:

  1. freed pointer aaa must bepointer to dynamically allocated memory space, otherwise an error will be reported;
  2. If an array is dynamically allocated, but delete pit is released in a way, it is useless [ ] [\ ][ ]  , then there is no problem at compile time, and generally no errors will occur at run time, but it will actually causeDynamically allocated arrays are not fully freed

3. Initialize memset

m e m s e t memset m e m se t : Initialize or clear the space for large space structures or large arrays

①The function
is to set all the contents of a block of memory to the specified value. This function usually initializes the newly applied memory, and is the fastest way to clear a large structure or array.


②Syntax
memset memsetm e m se t syntax:void* memset(void *s,int val,int len)

means to ssReplace the current position and the following len bytes in s with val and return sss


③ Use attention
memset memsetThe m e m se t function initializes the memory block by byte, so it cannot be used asint intint array initialized to0 and −1 0 and -10 andother than 1

This is because memset memsetm e m se t uses theAssign by byte, that is, assign the same value to each byte, so that the 4 bytes that make up the int type will be assigned the same value, and because 0 0The two's complement of 0 is all 0 00 − 1 -1 The two's complement of 1 is all 1 11 , not easily mistaken


For −1 -11 , its binary value is1 11 's complement+ 1 +1+1,即为 1 1 1 的补码

图解:

在这里插入图片描述


第一种情况:

针对字符串数组

char s[4];
memset(s,'1',4);

结果为:由上述可知,因为是对字节进行修改, c h a r char char 数组 s s s,一共有 4 4 4 字节空间,因此我们将全部的字符数组成员,都改为 ′ 1 ′ '1' 1

s [ 0 ] = s [ 1 ] = s [ 2 ] = s [ 3 ] = ′ 1 ′ s[0]=s[1]=s[2]=s[3]='1' s[0]=s[1]=s[2]=s[3]=1


第二种情况:

针对整型数组

char a[4];
memset(a,1,4);

结果为:仅仅将数组 a a a 的前四个字节,也就是第一个元素 a [ 0 ] a[0] a[0] 4 4 4 个字节中的每一个字节都变成了1,而后三个元素还是未分配内容的状态

即将这个 i n t int int元素的每个字节都变成了0000 0001,再合起来数组得到数组 a 的第一个元素也就是:
a [ 0 ] = 00000001   00000001   00000001   00000001 ( 2 ) = 16843009 a[0]=00000001\ 00000001\ 00000001\ 00000001(2)=16843009 a[0]=00000001 00000001 00000001 00000001(2)=16843009

Guess you like

Origin blog.csdn.net/qq_73450915/article/details/130970827