directory

  • preface

  • The body of the

  • What is a pointer?

  • How to use a pointer?

  • Significant differences

  • conclusion

preface

Not only C++ has Pointers, Golang also has Pointers! What are the differences? Let’s take a look today.

The body of the

What is a pointer? A pointer is a type variable that points to the memory address of any variable. The memory address of the variable it points to takes up 4 or 8 bytes, respectively, on 32-bit and 64-bit machines, regardless of the size of the value it points to.

Golang

Let’s look at an example of how to get the address of a variable in Golang.

Example code:

package main

import (
    "fmt"
)

func main(a) {
    var a int = 1 // An integer variable a
    var b string = "abcdefg" // A string variable b
    fmt.Printf("%p %p", &a, &b) // Output the addresses of a and B respectively
}

Copy the code

The result of running the above code:

0xc00007e020 0xc00005e1e0

It is worth noting that Golang can declare variables using Chinese characters (which is really more obvious than C++).

For example, the following code form can also be compiled and run correctly, used to C++ development friends, if you see it, it must be obsessive compulsive.

“Where’s my Italian cannon?”

package main

import (
    "fmt"
)

func main(a) {
    varAn integer variableint = 1
    varA string variablestring = "abcdefg"
    fmt.Printf("%p %p", & an integer variable, & a string variable)}Copy the code

The result of running the above code:

0xc000016060 0xc000010210

C++

Let’s look at an example of how C++ gets the address of a variable.

Example code:

#include <iostream>
using namespace std;

int main(a) {
    int a  = 1; // An integer variable a
    string b  = "abcdefg"; // A string variable b
    cout<< &a <<""<< &b <<endl; // Output the addresses of a and B respectively
    return 0;
}
Copy the code

The result of running the above code:

0x7ffea42614bc 0x7ffea42614c0

To be honest, there is not much difference between C++ and Golang compared to the above two parts of the code, and it is probably more the difference in syntax.

How to use a pointer?

A pointer can be used to get the address of a variable. It can also be used to get the value of a variable. Using Pointers correctly, whether in Golang or C++, is a fun and efficient thing to do. To become a good Golang developer or C++ developer, it is necessary to learn how to use Pointers.

Golang

Let’s look at an example of how a pointer in Golang gets the address of a variable and its value.

Example code:

package main

import (
    "fmt"
)

func main(a) {

    // Prepare a string type
    var a = "Hello, world"

    // Get the address of the string. The PTR type is *string
    ptr := &a

    // Print the type of the PTR
    fmt.Printf("ptr type: %T\n", ptr)

    // Print the PTR address
    fmt.Printf("address: %p\n", ptr)

    // Take the value of the pointer
    value := *ptr

    // Print the type of value
    fmt.Printf("value type: %T\n", value)

    // Print the value of value
    fmt.Printf("value: %s\n", value)

}
Copy the code

The result of running the above code:

ptr type: *string

address: 0xc000010210

value type: string

value: Hello, world

PTR is a pointer to a string whose address is 0xC000010210 and whose value is Hello, world.

C++

A piece of C++ code with the same logic

#include <iostream>
using namespace std;

int main(a) {

    // Prepare a string type
    string a = "Hello, world";

    // Get the address of the string. The PTR type is *string
    string* ptr = &a;

    // Print the PTR address
    cout<<"address: "<< ptr <<endl;

    // Take the value of the pointer
    string value = *ptr;

    // Print the value of value
    cout<<"value: " << value <<endl;
    
    return 0;
}
Copy the code

The result of running the above code:

address: 0x7ffe0b9c8860

value: Hello, world

Overall, there is little difference between Golang and C++.

Significant differences

From the above comparison, we can see that Golang and C++ are similar in many ways. Is there a clear difference between the two?

The answer is yes!

You can do arithmetic on Pointers directly in C++ (+, -, ++, –), but not in Golang.

Let’s look at two code examples:

Golang

package main

import (
    "fmt"
)

func main(a) {

    // Prepare a string type
    var a = "Hello, world"

    // Get the address of the string. The PTR type is *string
    ptr := &a

    // Print the address of ++ PTR
    fmt.Printf("address: %p\n", ++ptr)
	
    // Print the address of PTR +1
    fmt.Printf("address: %p\n", ptr+1)}Copy the code

Lines 16 and 19 of the above code will both report an error, but the same operation is fine in C++.

C++

A piece of C++ code with the same logic

#include <iostream>
using namespace std;

int main(a) {

    // Prepare a string type
    string a = "Hello, world";

    // Get the address of the string. The PTR type is *string
    string* ptr = &a;

    // Print the address of ++ PTR
    cout<<"address1: "<< ptr+1 <<endl;

    // Print the address of PTR +1
    cout<<"address2: "<< ++ptr <<endl;
    
    return 0;
}
Copy the code

The result of running the above code:

address1: 0x7fffe8bc9b80

address2: 0x7fffe8bc9b80

By comparing the results, we can see that Golang does not support arithmetic on Pointers, which is not restricted in C++.

conclusion

Today is just a very basic introduction to the use of Pointers in Golang and C++, in the actual development will involve many other types of pointer variables, such as array pointer, pointer array, object pointer, and so on. This article is a good introduction to learn more about C++ and Golang, please comment! The public account “play audio and video”, sharing super practical audio and video skills!