Table of Contents
Operator
Array name and array first address
小 Tips
总结
Home Backend Development Golang What is the difference between go language and c language on pointers?

What is the difference between go language and c language on pointers?

Nov 30, 2022 pm 07:50 PM
go golang c language go language pointer

Difference: 1. Go language can use the new keyword to allocate memory and create pointers of specified types, but C language cannot. 2. The array name arr in the C language represents the address of the first element of the array, which is equivalent to "&arr[0]"; the array name arr in the Go language does not represent the address of the first element of the array, but represents the value of the entire array. 3. Go language does not support pointer arithmetic, but C language supports pointer arithmetic. 4.

What is the difference between go language and c language on pointers?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

C and Go are both languages ​​with pointer concepts. This article mainly uses the similarities and differences between the two to deepen the understanding and use of Go pointers.

Operator

Both C and Go are the same:

  • & Operator takes out the location of the variable The memory address

  • * operator takes out the value in the memory address pointed to by the pointer variable, also called "Dereference"

C language version example:

#include <stdio.h>

int main()
{
    int bar = 1;
    // 声明一个指向 int 类型的值的指针
    int *ptr;
    // 通过 & 取出 bar 变量所在的内存地址并赋值给 ptr 指针
    ptr = &bar;
    // 打印 ptr 的值(为地址),*prt 表示取出指针变量所指向的内存地址里面的值
    printf("%p %d\n", ptr, *ptr);
    return (0);
}

// 输出结果:
// 0x7ffd5471ee54 1
Copy after login

Go language version example:

package main

import "fmt"

func main() {
 bar := 1
 // 声明一个指向 int 类型的值的指针
 var ptr *int
 // 通过 & 取出 bar 变量所在的内存地址并赋值给 ptr 指针
 ptr = &bar
 // 打印 ptr 变量储存的指针地址,*prt 表示取出指针变量所指向的内存地址里面的值
 fmt.Printf("%p %d\n", ptr, *ptr)
}

// 输出结果:
// 0xc000086020 1
Copy after login

Go can also use the new keyword to allocate memory Creates a pointer of the specified type.

 // 声明一个指向 int 类型的值的指针
 // var ptr *int
 ptr := new(int)
 // 通过 & 取出 bar 变量所在的内存地址并赋值给 ptr 指针
 ptr = &bar
Copy after login

Array name and array first address

For an array

// C
int arr[5] = {1, 2, 3, 4, 5};
// Go
// 需要指定长度,否则类型为切片
arr := [5]int{1, 2, 3, 4, 5}
Copy after login

In C, the array name arr represents The address of the first element of the array is equivalent to &arr[0]

and &arr represents the first address of the entire array arr

// C
// arr 数组名代表数组首元素的地址
printf("arr -> %p\n", arr);
// &arr[0] 代表数组首元素的地址
printf("&arr[0] -> %p\n", &arr[0]);
// &arr 代表整个数组 arr 的首地址
printf("&arr -> %p\n", &arr);

// 输出结果:
// arr -> 0061FF0C
// &arr[0] -> 0061FF0C
// &arr -> 0061FF0C
Copy after login

Run the program It can be found that the output values ​​​​of arr and &arr are the same, but their meanings are completely different.

First of all, the array name arr as an identifier is the address of arr[0], viewed from the perspective of &arr[0] It is a pointer to a value of type int.

And &arr is a pointer to a value of type int[5].

You can further verify the pointer offset

// C
// 指针偏移
printf("arr+1 -> %p\n", arr + 1);
printf("&arr+1 -> %p\n", &arr + 1);

// 输出结果:
// arr+1 -> 0061FF10
// &arr+1 -> 0061FF20
Copy after login

This involves the knowledge of offset: the movement of a pointer of type T is based on sizeof(T) is the moving unit.

  • arr 1 : arr is a pointer to a value of type int, so the offset is 1*sizeof(int)

  • ##&arr 1 : &arr is a pointer pointing to int[5], its offset is 1*sizeof(int)*5

I believe you should be able to understand the difference between

arr and &arr in C language. Next, let’s take a look at Go language

// 尝试将数组名 arr 作为地址输出
fmt.Printf("arr -> %p\n", arr)
fmt.Printf("&arr[0] -> %p\n", &arr[0])
fmt.Printf("&arr -> %p\n", &arr)

// 输出结果:
// arr -> %!p([5]int=[1 2 3 4 5])
// &arr[0] -> 0xc00000c300
// &arr -> 0xc00000c300
Copy after login

&arr[0] and &arr are consistent with C language.

But the array name

arr in Go is no longer the address of the first element of the array, it represents the value of the entire array, so the output will prompt %!p([5 ]int=[1 2 3 4 5])

Pointer arithmetic

A pointer is essentially an unsigned integer, representing a memory address.

Pointers and integer values ​​can be added and subtracted, such as the pointer offset example above:

  • Add

    n: A type is The pointer of T moves to the high bit in units of n*sizeof(T).

  • minus

    n: A pointer of type T, in units of n*sizeof(T) Move low.

sizeof(T) represents the bytes occupied by the data type, for example int is 4 bytes in a 32-bit environment , 8 bytes in 64-bit environment

C language example:

#include <stdio.h>

int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    // ptr 是一个指针,为 arr 数组的第一个元素地址
    int *ptr = arr;
    printf("%p %d\n", ptr, *ptr);

    // ptr 指针向高位移动一个单位,移向到 arr 数组第二个元素地址
    ptr++;
    printf("%p %d\n", ptr, *ptr);
    return (0);
}

// 输出结果:
// 0061FF08 1
// 0061FF0C 2
Copy after login
Here

ptr moved from 0061FF08 sizeof( int) = 4 bytes to 0061FF0C, pointing to the address of the next array element

Go language example:

package main

import "fmt"

func main() {
 arr := [5]uint32{1, 2, 3, 4, 5}

 // ptr 是一个指针,为 arr 数组的第一个元素地址
 ptr := &arr[0]
 fmt.Println(ptr, *ptr)

 // ptr 指针向高位移动一个单位,移向到 arr 数组第二个元素地址
 ptr++
 fmt.Println(ptr, *ptr)
}

// 输出结果:
// 编译报错:
// .\main.go:13:5: invalid operation: ptr++ (non-numeric type *uint32)
Copy after login

编译报错 *uint32 非数字类型,不支持运算,说明 Go 是不支持指针运算的。

这个其实在 Go Wiki[1] 中的 Go 从 C++ 过渡文档中有提到过:Go has pointers but not pointer arithmetic.

Go 有指针但不支持指针运算。

另辟蹊径

那还有其他办法吗?答案当然是有的。

在 Go 标准库中提供了一个 unsafe 包用于编译阶段绕过 Go 语言的类型系统,直接操作内存。

我们可以利用 unsafe 包来实现指针运算。

func Alignof(x ArbitraryType) uintptr
func Offsetof(x ArbitraryType) uintptr
func Sizeof(x ArbitraryType) uintptr
type ArbitraryType
func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType
type IntegerType
type Pointer
func Add(ptr Pointer, len IntegerType) Pointer
Copy after login

核心介绍:

  • uintptr : Go 的内置类型。是一个无符号整数,用来存储地址,支持数学运算。常与 unsafe.Pointer 配合做指针运算

  • unsafe.Pointer : 表示指向任意类型的指针,可以和任何类型的指针互相转换(类似 C 语言中的 void* 类型的指针),也可以和 uintptr 互相转换

  • unsafe.Sizeof : 返回操作数在内存中的字节大小,参数可以是任意类型的表达式,例如 fmt.Println(unsafe.Sizeof(uint32(0))) 的结果为 4

  • unsafe.Offsetof : 函数的参数必须是一个字段 x.f,然后返回 f 字段相对于 x 起始地址的偏移量,用于计算结构体成员的偏移量

原理:

Go 的 uintptr 类型存储的是地址,且支持数学运算

*T (任意指针类型) 和 unsafe.Pointer 不能运算,但是 unsafe.Pointer 可以和 *T 、 uintptr 互相转换

因此,将 *T 转换为 unsafe.Pointer 后再转换为 uintptr ,uintptr 进行运算之后重新转换为 unsafe.Pointer => *T 即可

代码实现:

package main

import (
 "fmt"
 "unsafe"
)

func main() {
 arr := [5]uint32{1, 2, 3, 4, 5}

 ptr := &arr[0]

 // ptr(*uint32类型) => one(unsafe.Pointer类型)
 one := unsafe.Pointer(ptr)
 // one(unsafe.Pointer类型) => *uint32
 fmt.Println(one, *(*uint32)(one))

 // one(unsafe.Pointer类型) => one(uintptr类型) 后向高位移动 unsafe.Sizeof(arr[0]) = 4 字节
 // twoUintptr := uintptr(one) + unsafe.Sizeof(arr[0])
 // !!twoUintptr 不能作为临时变量
 // uintptr 类型的临时变量只是一个无符号整数,并不知道它是一个指针地址,可能被 GC
 // 运算完成后应该直接转换回 unsafe.Pointer :
 two := unsafe.Pointer(uintptr(one) + unsafe.Sizeof(arr[0]))
 fmt.Println(two, *(*uint32)(two))
}

// 输出结果:
// 0xc000012150 1
// 0xc000012154 2
Copy after login

甚至还可以更改结构体的私有成员:

// model/model.go

package model

import (
 "fmt"
)

type M struct {
 foo uint32
 bar uint32
}

func (m M) Print() {
 fmt.Println(m.foo, m.bar)
}

// main.go

package main

import (
 "example/model"
 "unsafe"
)

func main() {
 m := model.M{}
 m.Print()

 foo := unsafe.Pointer(&m)
 *(*uint32)(foo) = 1
 bar := unsafe.Pointer(uintptr(foo) + 4)
 *(*uint32)(bar) = 2

 m.Print()
}

// 输出结果:
// 0 0
// 1 2
Copy after login

小 Tips

Go 的底层 slice 切片源码就使用了 unsafe 包

// slice 切片的底层结构
type slice struct {
 // 底层是一个数组指针
 array unsafe.Pointer
 // 长度
 len int
 // 容量
 cap int
}
Copy after login

总结

  • Go 可以使用 & 运算符取地址,也可以使用 new 创建指针

  • Go 的数组名不是首元素地址

  • Go 的指针不支持运算

  • Go 可以使用 unsafe 包打破安全机制来操控指针,但对我们开发者而言,是 "unsafe" 不安全的

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What is the difference between go language and c language on pointers?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C language data structure: data representation and operation of trees and graphs C language data structure: data representation and operation of trees and graphs Apr 04, 2025 am 11:18 AM

C language data structure: The data representation of the tree and graph is a hierarchical data structure consisting of nodes. Each node contains a data element and a pointer to its child nodes. The binary tree is a special type of tree. Each node has at most two child nodes. The data represents structTreeNode{intdata;structTreeNode*left;structTreeNode*right;}; Operation creates a tree traversal tree (predecision, in-order, and later order) search tree insertion node deletes node graph is a collection of data structures, where elements are vertices, and they can be connected together through edges with right or unrighted data representing neighbors.

The truth behind the C language file operation problem The truth behind the C language file operation problem Apr 04, 2025 am 11:24 AM

The truth about file operation problems: file opening failed: insufficient permissions, wrong paths, and file occupied. Data writing failed: the buffer is full, the file is not writable, and the disk space is insufficient. Other FAQs: slow file traversal, incorrect text file encoding, and binary file reading errors.

C language multithreaded programming: a beginner's guide and troubleshooting C language multithreaded programming: a beginner's guide and troubleshooting Apr 04, 2025 am 10:15 AM

C language multithreading programming guide: Creating threads: Use the pthread_create() function to specify thread ID, properties, and thread functions. Thread synchronization: Prevent data competition through mutexes, semaphores, and conditional variables. Practical case: Use multi-threading to calculate the Fibonacci number, assign tasks to multiple threads and synchronize the results. Troubleshooting: Solve problems such as program crashes, thread stop responses, and performance bottlenecks.

How to output a countdown in C language How to output a countdown in C language Apr 04, 2025 am 08:54 AM

How to output a countdown in C? Answer: Use loop statements. Steps: 1. Define the variable n and store the countdown number to output; 2. Use the while loop to continuously print n until n is less than 1; 3. In the loop body, print out the value of n; 4. At the end of the loop, subtract n by 1 to output the next smaller reciprocal.

CS-Week 3 CS-Week 3 Apr 04, 2025 am 06:06 AM

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i

Golang's Purpose: Building Efficient and Scalable Systems Golang's Purpose: Building Efficient and Scalable Systems Apr 09, 2025 pm 05:17 PM

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

C language data structure: the key role of data structures in artificial intelligence C language data structure: the key role of data structures in artificial intelligence Apr 04, 2025 am 10:45 AM

C Language Data Structure: Overview of the Key Role of Data Structure in Artificial Intelligence In the field of artificial intelligence, data structures are crucial to processing large amounts of data. Data structures provide an effective way to organize and manage data, optimize algorithms and improve program efficiency. Common data structures Commonly used data structures in C language include: arrays: a set of consecutively stored data items with the same type. Structure: A data type that organizes different types of data together and gives them a name. Linked List: A linear data structure in which data items are connected together by pointers. Stack: Data structure that follows the last-in first-out (LIFO) principle. Queue: Data structure that follows the first-in first-out (FIFO) principle. Practical case: Adjacent table in graph theory is artificial intelligence

Troubleshooting tips for processing files in C language Troubleshooting tips for processing files in C language Apr 04, 2025 am 11:15 AM

Troubleshooting Tips for C language processing files When processing files in C language, you may encounter various problems. The following are common problems and corresponding solutions: Problem 1: Cannot open the file code: FILE*fp=fopen("myfile.txt","r");if(fp==NULL){//File opening failed} Reason: File path error File does not exist without file read permission Solution: Check the file path to ensure that the file has check file permission problem 2: File reading failed code: charbuffer[100];size_tread_bytes=fread(buffer,1,siz

See all articles