当前位置:网站首页>Rust short note: raw pointer
Rust short note: raw pointer
2022-07-21 21:02:00 【biakia0610】
as everyone knows ,Rust The pointer of language is a safe pointer , It will follow certain rules , such as ownership The rules , Will ensure that there is no dangling pointer . But when we need to write some underlying frameworks , It is often necessary to bypass these rules , Free control pointer , At this time, we can use the original pointer
1、 Define raw pointer
We can define the original pointer in the following way :
fn main() {
let mut num = 5;
let r1 = &num as *const i32;
let r2 = &mut num as *mut i32;
}
adopt &num What you get is Rust Cannot be modified (immutable) The pointer , And then through as Make a cast , Convert to *const i32, Indicates that it cannot be modified (immutable) Of i32 Original pointer to type .
adopt &mut What you get is Rust Can be modified (mutable) The pointer , And then through as Make a cast , Convert to *mut i32, Means modifiable (immutable) Of i32 Original pointer to type .
Notice the * Not a separate operator , It's about putting *const Connected as a type , Empathy *mut It is also connected as a separate type .
Now try to pass *const Type pointer for content modification :
fn main() {
let mut num = 5;
let r1 = &num as *const i32;
let r2 = &mut num as *mut i32;
unsafe {
*r1 = 1;
}
}
Errors will be found after compilation :
error[E0594]: cannot assign to `*r1`, which is behind a `*const` pointer
--> src/main.rs:9:10 |
6 | let r1 = &num as *const i32;
| ---- help: consider changing this to be a mutable pointer: `&mut num`
...
9 | *r1 = 1;
| ^^^^^^^ `r1` is a `*const` pointer, so the data it refers to cannot be written
Here the compiler tells us ,r1 It's a *const Original pointer to type , Therefore, the data it points to cannot be modified .
Next, try to use the modifiable original pointer to modify the data :
fn main() {
let mut num = 5;
let r1 = &num as *const i32;
let r2 = &mut num as *mut i32;
unsafe {
*r2 = 1;
}
println!("num={:?}",num);
}
The result is
num=1
Modification successful , explain *mut The original pointer of type can modify data .
2、 Get the data pointed to by the original pointer
We can use the operator * To get the data pointed to by the original pointer :
fn main() {
let mut num = 5;
let r1 = &num as *const i32;
let r2 = &mut num as *mut i32;
unsafe {
println!("r1 is: {}", *r1);
println!("r2 is: {}", *r2);
}
}
The operation results are as follows :
r1 is: 5
r2 is: 5
What needs to be noted here is , The original pointer can be defined directly in Rust The statement defines , To obtain the data pointed to by the original pointer, you need to unsafe Used in code blocks , Otherwise the compilation will report an error .
边栏推荐
- How is redis different from memcached
- Construction de l'environnement PHP (panneau de pagode recommandé)
- [intranet penetration] the intranet does not go out of the network, and the machine bounces back to the shell and CS goes online
- Thinkphp5.1 send email with phpmailer
- 【逆向分析】基础入门-查找程序main函数修改程序
- [intranet penetration] intranet penetration of vulnstack II
- Verify with JWT under thinkphp5.1
- 【内网渗透】内网渗透红日靶场(vulnstack)二
- MetaForce原力元宇宙之我見,教你迅速搞懂滑落機制
- SushiToken的投票实现
猜你喜欢
[intranet penetration] the intranet does not go out of the network, and the machine bounces back to the shell and CS goes online
Extract a subset from a point cloud
BUUCTF(misc)
Web security -- File Inclusion (local inclusion, remote inclusion)
TP5 docking visa free FM payment interface
Uniswap计算过程推演
MetaForce原力元宇宙之我見,教你迅速搞懂滑落機制
[intranet penetration] MSF rebound traffic encryption session
写一个Solana版的Sushi Masterchef
Buuctf n1book [Chapter 2 advanced web] file upload
随机推荐
解决uniapp编译后vendor.js文件过大(官方处理方案)
Nodejs waits for a period of time
PHP three lines of code to write test interface
Uniswap计算过程推演
NVM, NRM tutorial
[intranet penetration] information collection in the domain (manual +adfind tool)
PHP环境搭建(推荐宝塔面板)
易语言学习笔记(四)--js解密,图形验证码,滑块,鱼刺多线程
php百度人脸检测api测颜值评分(源码直接可用)
[PHP code audit] Introduction analysis of vulnerabilities in Pikachu shooting range
Boundary layer integral equation and Marangoni effect
【权限提升】 MSSQL提权方法
The text file is transferred to the external server through the web proxy server and returned after modification
【逆向分析】恶意代码静态分析
Attack and defense World Web Zone difficulty level: 3 (ics-05, MFW, easytornado)
攻防世界web区 难度等级:3(ics-05,MFW,easytornado)
A label download and window location. Herf Download
From going to IOE to cipu, cloud computing in China should go its own way
Use the nodemon tool to restart the nodejs server automatically
Verify with JWT under thinkphp5.1