Table of contents
What is an address?
In memory(SeeWikipedia)In the memory, the smallest addressable unit is the byte. In order to operate memory efficiently, we introduce a unique address for each byte. Usually expressed in hexadecimal, such as 0x000001 (hexadecimal is usually expressed in0x
At the beginning, used to distinguish, seehttps://stackoverflow.com/questions/2670639/why-are-hexadecimal-numbers-prefixed-with-0x)
(For security reasons, in most cases, the address is not the real address in physical memory, but a virtual address assigned by the operating system)
For example, you have a 4GiB memory stick with 232Bytes (4 × 210 × 210 × 210), so its address starts from 0 and goes to 232-1. A 32-bit computer uses 32 bits for addressing, so a 32-bit computer can only support up to 4 GiB of memory; while a 64-bit computer uses 64-bit addresses, so its maximum memory is 264Byte = 224TiB = 214PiB = 24EiB = 17179869184 GiB
Explanation of storage units: We use two different measurement methods, one is based on decimal, such as GB (Gigabyte) MB (Megabyte), the conversion method is 1GB = 1000MB; the other is based on binary, such as GiB (Gibibyte) MiB (Mebibyte), the conversion method is 1GiB = 210MiB = 1024MiB. GB displayed in Windows is actually GiB. Seehttps://en.wikipedia.org/wiki/Gigabyte
pointer
Pointers are similar to variables, but they store addresses instead of specific values.
Operators
&
Reference: used to obtain an address.
*
Dereference: Get the contents.
for example:
int myAge = 43; // int变量
int* ptr = &myAge; // ptr是一个指针变量,存储了myAge的地址
// 输出age的值 (43)
printf("%d\n", myAge);
// 输出myage的地址 (0x7ffe5367e044)
printf("%p\n", &myAge);
// 输出ptr存储的地址 (0x7ffe5367e044)
printf("%p\n", ptr);
How do pointers work?
int main(){
int num = 10;
int *p1, *p2;
p1 = #
p2 = #
*p1 = 20;
*p2 = 30;
cout << &num << p1 << p2 << &p1 << &p2;
}
Output: 0x61fe1c, 0x61fe1c, 0x61fe10, 0x61fe08
It can be seen from this thatPointers are also variables, the pointer also has its own address.
Pointers in arrays
Previously we used [] to access elements in an array. In fact, an array variable is a pointer to the first element of the array.
int a[5] = {1, 2, 3, 4, 5};
cout << a << endl; // 输出:0x31e6fffc80(数组首位的地址)
cout << *a << endl; // 输出:1(数组首位的值)
cout << a + 1 << endl; // 输出:0x31e6fffc84
cout << *(a + 1) << endl; // 输出:2
Pointers can also perform operations, for example, a+1 represents the second element of a, as shown above
The offset caused by pointer operation (a+1 followed by address+4) is the size of the data type. Here a is of type int, so each array element occupies 4 bytes, and the offset is also 4 bytes.
Difference between pointer and array
The array is a constant pointer, which means it cannot be modified (reassigned)
int numbers[4] = {0, 1, 2, 3};
int *p = numbers; // 合法
p = p + 1; // 合法
numbers = p; // 错误: array names cannot be reassigned
Use the sizeof() function to return the size of the array (occupied space) in Bytes
int a[5] = {1, 2, 3, 4, 5};
cout << sizeof(a) << endl; // 输出: 20 (4Byte × 5)
Memory Operation
In the previous C, we used malloc to allocate memory space and free to release memory space. This method can also be used in C++.
In C++, there are two new operators: new and delete, which have some new features
int * p1 = new int; //allocate an int, default initializer (do nothing)
int * p2 = new int(); //allocate an int, initialized to 0
int * p3 = new int(5); //allocate an int, initialized to 5
int * p4 = new int{};//C++11 allocate an int, initialized to 0
int * p5 = new int {5};//C++11 allocate an int, initialized to 5
Student * ps1 = new Student; //allocate a Student object, default initializer
//allocate a Student object, initialize the members
Student * ps2 = new Student {"Yu", 2020, 1}; //C++11
For arrays:
//allocate 16 int, default initializer (do nothing)
int * pa1 = new int[16];
//allocate 16 int, zero initialized
int * pa2 = new int[16]();
//allocate 16 int, zero initialized
int * pa3 = new int[16]{}; //C++11
//allocate 16 int, the first 3 element are initialized to 1,2,3, the rest 0
int * pa4 = new int[16]{1,2,3}; //C++11
//allocate memory for 16 Student objects, default initializer
Student * psa1 = new Student[16];
//allocate memory for 16 Student objects, the first two are explicitly initialized
Student * psa2 = new Student[16]{{"Li", 2000,1}, {"Yu", 2001,1}}; //C++11
Use the delete keyword to release memory. For a single variable, usedelete
; For arrays, usedelete[]
//deallocate memory
delete p1;
//deallocate memory
delete ps1;
//deallocate the memory of the array
delete []pa2;
//deallocate the memory of the array, and call the destructors of all the elements
delete []psa2;
Two-dimensional array
Since memory is linear, in C++ we use the following method (row-major layout) to store a two-dimensional array.
Row-major order means that multidimensional arrays (such as two-dimensional arrays) are stored in memory row-first. That is, the elements of the array are stored in row order: all elements of the first row are stored first, then all elements of the second row, and so on.

Definition: int A[3][3];
If d has N rows and M columns, then d[i][j] is synonymous with ptr[i*M+j].
We can also use A[1] to access the address of the first element in each row and use it as a one-dimensional array.
int a[2][2] = {{1,2},{3,4}};
cout << a[0] << endl; // 输出0x5f33dff750
cout << a[1] << endl; // 输出0x5f33dff758
Command Line Parameters
In practical usage, sometimes we need to pass parameters to a program. For example, when using the MinGW compiler, we add the -o parameter to specify the name of the executable file.
int main(int argc, char *argv[]) {
cout << argc << endl;
for (int i = 0; i < argc; i++) {
cout << argv[i] << endl;
}
return 0;
}
After compilation, run the following command in the command line:
PS C:\Users\sy\Desktop\test> ./a.exe arg1 arg2 arg3
4
C:\Users\sy\Desktop\test\a.exe
arg1
arg2
arg3
It can be seen that the parameters entered in the command line are passed as arguments to the main function. The first parameter is the count, and the second parameter is a C-style string array. The first element represents the execution location, and the subsequent elements store the input parameters.
Data Structures
Todo
Comments NOTHING