Analysis of the relationship between C++ pointers, references, and stack transfers

Table of contents

foreword

C++

heap pointer

stack pointer

constant pointer

pointer constant

quote 

 constant reference

Summarize


foreword

At present, I have done a lot of projects and come into contact with various languages. Basically, I learn what I use, and the boundaries of the language will be very blurred. In fact, the design of the language is similar, but the language has its own characteristics and differences. One of the important concepts is the pointer, which is a popular technology in C language. It is actually displayed or hidden in various languages, and it shows essentially the same phenomenon in the call of function or method stack. Relatively speaking, Golang and Java are similar in design, and there are so many C++ concepts.

C++

C++ pointers, pointers are extended to C. In fact, by default, C++ pointers are similar to go or Java references, but there are special definitions, such as constant pointers, pointer constants, and references. The demo is as follows.

heap pointer

C++ has the concept of stack object and heap object memory allocation, and the pointer of the heap object needs to be manually recovered.


#include <iostream>

using namespace std;

class Person{
    int age;
    string name;

public:
    Person(){

    }
    Person(string name, int age){
        this->name = name;
        this->age = age;
    }
    string sayHello(){
        return name + " : " + to_string(age);
    };
};

void setPerson(Person* person){
    delete person;
    new Person("tom", 35);//省略写法,默认就会对person赋值
//    Person p;
//    person = &p;
};

int main(){
    Person* person = new Person("JIM", 33);
    setPerson(person);
    string result = person->sayHello();

    cout << result << endl;

    delete person;

    return 0;
}

Write a class at will, use pointer access, of course, you can also create a stack object, take the address, the result is as follows

TOM : 35

In the case of pointers, even assignment in the method stack will affect the change of data . This does not exist in Golang and Java. Of course, Golang is similar to C language and can use pointer pointers to simulate this situation. This is why it is said that Golang It is the closest to the C language, and the C language does the same. The author also wrote other articles: The process of go function or method parameter call_fenglllle's blog-CSDN blog

stack pointer

If you use stack objects, you don’t need to manually reclaim memory, and the reference counting method will automatically reclaim


#include <iostream>

using namespace std;

class Person{
    int age;
    string name;

public:
    Person(){

    }
    Person(string name, int age){
        this->name = name;
        this->age = age;
    }
    string sayHello(){
        return name + " : " + to_string(age);
    };
};

void setPerson(Person* person){
    //delete person;
//    new Person("tom", 35);
    Person p("TOM", 35);
    person = &p;
};

int main(){
    Person person = Person("JIM", 33);
    setPerson(&person);
    string result = person.sayHello();

    cout << result << endl;

    //delete person;

    return 0;
}

The result is the same as the Java pattern

JIM : 33

If you directly modify the value of the address, it can be modified

#include <iostream>

using namespace std;

class Person{
    int age;
    string name;

public:
    Person(){

    }
    Person(string name, int age){
        this->name = name;
        this->age = age;
    }
    void setPerson(string name, int age){
        this->name = name;
        this->age = age;
    }
    string sayHello(){
        return name + " : " + to_string(age);
    };
};

void setPerson(Person* person){
    //delete person;
//    new Person("tom", 35);
//    Person p("TOM", 35);
//    person = &p;
    person->setPerson("TOM", 35);
};

int main(){
    Person person = Person("JIM", 33);
    setPerson(&person);
    string result = person.sayHello();

    cout << result << endl;

    //delete person;

    return 0;
}

The result is as follows. This is often mentioned in Java stereotypes. It is actually the use of pointers, but Java objects are allocated to the heap (because of GC, it has developed many generations)

TOM : 35

constant pointer

In fact, it is a pointer. A pointer to a constant is similar to the difference between a function pointer and a pointer function. It is a pointer, which means that the memory address can be modified. The constant pointed to means that the value of the memory address cannot be modified.

const int* is parsed from first to last, indicating that it is a pointer, using a constant modifier pointer (opposite to Golang)

This is different from Java and Golang. In Java and Golang, there is no constant pointer design, or there is no restriction that pointers can only point to constants.

pointer constant

As the name implies: int* const represents a pointer to modify a constant, and the pointer is a constant, which means that the memory address pointed to by the pointer cannot be modified, but the value of the memory address pointed to by the pointer can be modified. In Java, such as final List<String> list, in Golang, the use of pointer types is restricted to constant decoration.

The error is just the opposite of the above, the value can be modified, but the address cannot be modified.

quote 

The essence of a reference is a pointer constant, which is the simplified way of writing above.

 constant reference

 This is similar to Golang, constants are not allowed to modify the value.

Summarize

In fact, there is a lot of linguistics, and the design is similar, but because of different abilities, the functions have additional differences, so there will be a saying that xx language is suitable for xx development. In actual use, you can learn while doing projects. . For example, the author recently wants to understand the underlying implementation of JDK and some ebpf content of K8S, so he needs to learn C++, but C++ has so many keywords, and the grammar is very detailed, far exceeding other languages. For example, pointers may be because the C language does not refine pointers, but C++ is specific.

Guess you like

Origin blog.csdn.net/fenglllle/article/details/129111000