← Back to Rust Course | Chapter 13: Smart Pointers & Unsafe | Lesson 7 of 7

Raw Pointers क्या हैं

Raw pointers memory में bare-bones addresses हैं जिन्हें Rust आपको उपयोग करने देता है, लेकिन सिर्फ unsafe के अंदर, क्योंकि वे usual safety checks skip करते हैं।
Syntax
rust
let p = &value as *const Type;
let p = &mut value as *mut Type;
unsafe {
    let v = *p;
}

Creating Raw Pointers

Raw pointers as *const T या as *mut T उपयोग करके references से बनाए जा सकते हैं। खुद एक raw pointer बनाना पूरी तरह safe है -- सिर्फ इसे dereference करने के लिए unsafe चाहिए।

उदाहरण: Creating Raw Pointers

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `value`, set to `100`
    let value = 100;
    // Declare an immutable variable `const_ptr`, set to `&value as *const i32`
    let const_ptr = &value as *const i32;
    // Print "Pointer created (not yet dereferenced)" to the console (with a trailing newline)
    println!("Pointer created (not yet dereferenced)");
    unsafe {
        // Print "Value: {}" to the console, substituting in `*const_ptr` (with a trailing newline)
        println!("Value: {}", *const_ptr);
    }
}

Mutable Raw Pointers

*mut T pointer के ज़रिए pointed-to data modify करने देता है, लेकिन सभी raw pointer dereferences की तरह, ऐसा करने के लिए एक unsafe block चाहिए।

उदाहरण: Mutable Raw Pointers

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `value`, initialized to `5`
    let mut value = 5;
    // Declare an immutable variable `mut_ptr`, set to `&mut value as *mut i32`
    let mut_ptr = &mut value as *mut i32;
    unsafe {
        *mut_ptr += 1;
    }
    // Print "value = {}" to the console, substituting in `value` (with a trailing newline)
    println!("value = {}", value);
}

Raw Pointers Skip Borrow Checking

References के विपरीत, आपके पास उसी data के एक साथ कई raw pointers (यहां तक कि mutable वाले भी) हो सकते हैं, क्योंकि borrow checker raw pointers track नहीं करता -- यही बिल्कुल वजह है कि इन्हें dereference करना unsafe है।

उदाहरण: Raw Pointers Skip Borrow Checking

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `number`, initialized to `10`
    let mut number = 10;
    // Declare an immutable variable `p1`, set to `&mut number as *mut i32`
    let p1 = &mut number as *mut i32;
    // Declare an immutable variable `p2`, set to `&mut number as *mut i32`
    let p2 = &mut number as *mut i32;
    unsafe {
        *p1 += 1;
        *p2 += 1;
    }
    // Print "number = {}" to the console, substituting in `number` (with a trailing newline)
    println!("number = {}", number);
}

Why Raw Pointers Exist

Raw pointers foreign function interfaces (C code call करना) जैसे low-level tasks के लिए और internally safe abstractions बनाने के लिए ज़रूरी हैं, जहां implementer manually उन safety invariants की गारंटी देता है जिन्हें compiler जांच नहीं सकता।

उदाहरण: Why Raw Pointers Exist

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `array`, set to `[1, 2, 3, 4]`
    let array = [1, 2, 3, 4];
    // Declare an immutable variable `ptr`, set to `array.as_ptr()`
    let ptr = array.as_ptr();
    unsafe {
        // Print "First element via raw pointer: {}" to the console, substituting in `*ptr` (with a trailing newline)
        println!("First element via raw pointer: {}", *ptr);
        // Print "Second element via offset: {}" to the console, substituting in `*ptr.offset(1)` (with a trailing newline)
        println!("Second element via offset: {}", *ptr.offset(1));
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक unsafe block के बाहर एक raw pointer dereference करना, जो एक compile error है।
  2. ऐसे data के लिए एक raw pointer बनाना जो पहले ही scope से बाहर जा चुका है, एक dangling pointer में परिणत होते हुए जिसे बनाना legal है लेकिन dereference करना unsafe है।
  3. *const T (immutable raw pointer) को *mut T (mutable raw pointer) के साथ confuse करना -- उनकी अलग-अलग capabilities हैं।
चैप्टर सारांश
  • *const T और *mut T raw pointers हैं जो Rust के normal borrowing rules bypass करते हैं।
  • Raw pointer बनाना safe है; एक को dereference करने के लिए एक unsafe block चाहिए।
  • Raw pointers null, dangling, या unaligned हो सकते हैं -- Rust उनकी validity की गारंटी नहीं देता जैसे यह references के लिए देता है।
  • Raw pointers मुख्य रूप से low-level interoperability (जैसे FFI) और custom safe abstractions बनाने के लिए उपयोग होते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.