Hello, I’m Liang Tang.

This is part 17 of the EasyC++ series, and we continue to talk about Pointers in C++.

For those who want a better reading experience, visit the Github repository.

A Preliminary Study on Pointers (3)

The previous Pointers were created as single variables, and in this case the advantage of using Pointers is not obvious. Many programmers still choose to declare variables, but Pointers can be useful when we need to dynamically create large data such as arrays.

The array we create declaratively is already allocated memory at compile time, even if we don’t use it at all. This compile-time allocation of memory to an array is called a static binding, meaning that the array is added to the program at compile time.

Arrays created with new are created at run time, and as we mentioned earlier, the big difference is that one is stack memory and the other is heap memory. We can programmatically control whether and under what circumstances it is created, and the length of the array. Therefore, such arrays are called dynamic arrays, and this method of creation is called dynamic binding.

Create an array

Creating an array with new is easy, just like declaring it, by specifying the type of the array and the number of elements.

int *arr = new int[10];
Copy the code

The new operator claims a block of 10 ints and returns the address of the first element to the pointer ARr.

We can also delete an array created with new, but we need to specify that it is an array when we delete it:

delete [] arr;
Copy the code

Without square brackets, delete simply frees the element to which the pointer points, which is the first position in the array. So when we use new and delete, we must be careful that if we use square brackets to apply the array, then we need to add square brackets to delete. Mismatches can lead to uncertain consequences, so be careful.

C++ Primer compiles a few rules:

  • Don’t usedeleteRelease is notnewAllocated memory
  • Don’t usedeleteFree the same block of memory twice
  • If you are usingnew[]To allocate memory for arrays, it should also be useddelete[]To release the
  • If you are usingnewAllocate memory for non-arrays, should be useddeleteTo release the
  • The null pointerdeleteIs safe

Using dynamic arrays

Having talked about the use of DELETE, let’s look at the use of dynamic arrays.

The problem here is that what we create with new is an array, but it’s in the form of a pointer, so how do we use it?

It’s actually quite simple, let’s just use it like an array:

int *p = new int[10];

p[2] = 5;
cout << p[2] << endl;
Copy the code

Because arrays are essentially constant Pointers, they are essentially equivalent. The only difference is that arrays are constant Pointers, so we can’t assign to array names. Pointers can:

p = p + 1;
++p;
Copy the code

Yes, Pointers can be arithmetically manipulated, and adding and subtracting Pointers is the same thing as moving Pointers. For example, a pointer plus one moves the element it points to back one bit.

P points to 0, but after p++, it points to 1. Because arrays are contiguous chunks of memory, different types of variables have different lengths. Therefore, in the underlying operation, the length of movement of the pointer is not actually 1, but the number of bytes occupied by a variable type.

Similarly, a pointer can be subtracted, and the difference is an integer. This operation makes sense only if both Pointers point to the same array, representing the number of elements spaced between the two Pointers.