Tutorial by Examples: deref

This is an example of dereferencing a NULL pointer, causing undefined behavior. int * pointer = NULL; int value = *pointer; /* Dereferencing happens here */ A NULL pointer is guaranteed by the C standard to compare unequal to any pointer to a valid object, and dereferencing it invokes undefined...
int* foo(int bar) { int baz = 6; baz += bar; return &baz; /* (&baz) copied to new memory location outside of foo. */ } /* (1) The lifetime of baz and bar end here as they have automatic storage * duration (local variables), thus the returned pointer is not valid! */ ...
int a = 1; int *a_pointer = &a; To dereference a_pointer and change the value of a, we use the following operation *a_pointer = 2; This can be verified using the following print statements. printf("%d\n", a); /* Prints 2 */ printf("%d\n", *a_pointer); /* Also prints...
Let's say we have the following structure: struct MY_STRUCT { int my_int; float my_float; }; We can define MY_STRUCT to omit the struct keyword so we don't have to type struct MY_STRUCT each time we use it. This, however, is optional. typedef struct MY_STRUCT MY_STRUCT; If we th...
The std::ops::Deref and std::ops::DerefMut traits are used for overloading the dereference operator, *x. For types A and B, impl Deref<Target=B> for A indicates that dereferencing a binding of &A will yield a &B and, impl DerefMut for A indicates that dereferencing a binding of...
Dereferencing happens with the . operator: Object obj = new Object(); String text = obj.toString(); // 'obj' is dereferenced. Dereferencing follows the memory address stored in a reference, to the place in memory where the actual object resides. When an object has been found, the requested meth...
Given two types T and U, &T will coerce (implicitly convert) to &U if and only if T implements Deref<Target=U> This allows us to do things like this: fn foo(a: &[i32]) { // code } fn bar(s: &str) { // code } let v = vec![1, 2, 3]; foo(&v); // &Vec&l...
In some cases the behavior of a command depends on whether it is given branch name, tag name, or an arbitrary revision. You can use "de-referencing" syntax if you need the latter. A suffix ^ followed by an object type name (tag, commit, tree, blob) enclosed in brace pair (for example v0.9...
For functions that need to take a collection of objects, slices are usually a good choice: fn work_on_bytes(slice: &[u8]) {} Because Vec<T> and arrays [T; N] implement Deref<Target=[T]>, they can be easily coerced to a slice: let vec = Vec::new(); work_on_bytes(&vec); le...
Pointers can be dereferenced by adding an asterisk * before a pointer. package main import ( "fmt" ) type Person struct { Name string } func main() { c := new(Person) // returns pointer c.Name = "Catherine" fmt.Println(c.Name) // prints: Cathe...
use std::ops::Deref; use std::fmt::Debug; #[derive(Debug)] struct RichOption<T>(Option<T>); // wrapper struct impl<T> Deref for RichOption<T> { type Target = Option<T>; // Our wrapper struct will coerce into Option fn deref(&self) -> &Option...
Deref has a simple rule: if you have a type T and it implements Deref<Target=F>, then &T coerces to &F, compiler will repeat this as many times as needed to get F, for example: fn f(x: &str) -> &str { x } fn main() { // Compiler will coerce &&&&&&a...
Just like in C, Rust raw pointers can point to other raw pointers (which in turn may point to further raw pointers). // Take a regular string slice let planet: &str = "Earth"; // Create a constant pointer pointing to our string slice let planet_ptr: *const &str = &planet ...

Page 1 of 1