C++ learning difficulties record

1. Pointer and address

Debugging in VS, the comments in the code are the results of debugging.

#include <iostream>
using namespace std;

int main()
{
    
    
	int a = 10;//a 的地址0x00D7FBD0  a的值是10;
	int* b = &a; //b 的地址0x00D7FBC4, b的值是a的地址
	return 0;
}

In the memory, the variable arepresents the address of a certain block, and the variable is stored inside. &Take the address symbol, you can take the address of the lvalue, declare a pointer variable, you can store the obtained address value, of course, you can also know the address of the pointer, but in many cases it is not necessary.
Image example
2. The first address of the array and the address of the array name

Insert picture description here
We check that a, &a, &a[0] are the same value.

  • In fact, the results of a and &a are both the first address of the array. But their types are different.

  • a means &a[0], that is, take the address of the first element of the array. a+1, which is &a[1].

  • &a Although the value is the address of the first element of the array. But the type is: type(*)[number of array elements], so the size of &a+1 is: first address+sizeof(a).
    Take the above code as an example, we can check &a+1, the content pointed to is the first address after a[4].
    Insert picture description here

When you understand that the name of the array is the first address of the array, you should also know that the name of the array is just "equivalent" to a pointer, not really a pointer. The
array name is just a constant (a value for the first element of the array) Address constant), so ++ or-operations cannot be performed. The constant cannot take the address, and the reason for the &a, in fact, the meaning of a here is no longer the original array name, it represents the entire array at this time.

#include<stdio.h>
#include <iostream>

using namespace std;

int main()
{
    
    
    int a[5]={
    
    0x11121314,0x21222324,0x31323334,0x41424344,0x51525354};
    printf("%p\n%p\n",a,&a); //数值相等
//输出为  00AFFA5C  00AFFA5C
    cout<<sizeof(a)<<endl; // a是复合类型 int[5] 
// 输出20
    cout<<sizeof(&a)<<endl; // &a是int(*)[5]
// 输出4

    cout<<sizeof(a+1)<<endl; // a+1 变成指向数组第二个元素的指针,其类型是 int *    
//输出4
    cout<<sizeof(&a+1)<<endl; // &a+1 还是 int(*)[5]。类型还是指针   
//输出4

    //a+1 由于 a 指向int(但不是普通指针),加1移动int个大小
    //&a+1 由于 &a 指向 int[5],加1移动 int[5] 个大小
    printf("%p\n%p\n",a+1,&a+1);
//00AFFA60(00AFFA5C+4)   00AFFA70(00AFFA5C+5*4)
}

To record, from the above we know that array names can be accessed as pointers to a certain extent, so there are some doubts, then what is the difference between the names of classes, structures, and common data types.

#include <iostream>
#include <stdio.h>
using namespace std;

struct stu{
    
    
    int age;
    char b;
};
class A{
    
    
public:
    int aa;
    int ss;
    A(int a, int s):aa(a),ss(s){
    
    }
    void set(){
    
    
        cout << "hello world" << endl;
    }
};

int main()
{
    
    
    A aaa(7,3);
    A* ccc = &aaa;
    printf("%p\n", aaa);
    printf("%p\n", &aaa);
    printf("%p\n", ccc);

    cout << endl;
    int f = 7;
    printf("%p\n", f);
    printf("%p\n", &f);
    
    cout << endl;
    stu s;
    s.age = 7;
    s.b = 'a';
    printf("%p\n", s);
    printf("%p\n", &s);
    stu* sz = &s;
    printf("%p\n", sz);

    cout << endl;
    int a[10] = {
    
    0};
    int* b = a;
    printf("%p\n", a);
    printf("%p\n", &a);
    printf("%p\n", b);
    return 0;
}

Insert picture description here
From the above results, for the time being, we can regard classes, structures, and common data types as one look, where the names of classes and structures can take their addresses, and the address is the corresponding first address. The array name has a certain effect and can be used as a pointer.
When c++ writes the linked list, do the nodes generated by new need to be released
at the end of the program ? Not at the end of the program, but when a node is no longer needed.
For example, deleting a node not only removes it from the linked list, but also deletes it.
The reason for this is because if you don't manually delete it, you know that it will continue to occupy memory before the program ends. If it is a large program, it may run out of memory after running for a long time.
There is no need to release it when the program ends, because the system will reclaim the memory.

Guess you like

Origin blog.csdn.net/JACKSONMHLK/article/details/113527839