Unsafe Pointers in Rust: Common Pitfalls and How to Avoid Them

Raw Pointers

In Rust, pointers are used to reference a location in memory. They are a way of borrowing data stored at a particular address rather than copying it. Pointers are an important feature in Rust, but they can also be a source of bugs if used incorrectly. In this article, we will discuss some common pitfalls when using pointers in Rust and how to avoid them.

One of the main issues with pointers is that they can be null, meaning they do not point to a valid location in memory. In Rust, the null pointer is represented by the null keyword.

Using a null pointer can lead to a null reference error, which occurs when you try to access data through a null pointer. This can cause the program to crash or produce undefined behaviour.

To prevent null reference errors, it is important to check for null pointers before dereferencing them. This can be done with an if statement or the .as_ref() method

Another issue with pointers is that they can be dangling, meaning they point to a location in memory that has been deallocated. This can occur when a pointer is created to an object that goes out of scope, and the memory it occupied is then freed.

Using a dangling pointer can lead to data races or segmentation faults, as the memory location it points to may have been reused for something else.

To prevent dangling pointers, it is important to ensure that the data a pointer points to remains valid for the lifetime of the pointer. In Rust, this can be achieved through the use of reference counting or by using lifetimes.

In the example above, the vec_ptr pointer is created to the first element of the vec vector. However, when the vector is cleared, the memory occupied by its elements is freed, making vec_ptr a dangling pointer

To fix this issue, we can use reference counting by changing the vec vector to a Rc<Vec<i32>>. This will ensure that the memory occupied by the vector is not freed until all references to it have gone out of scope

Another way to fix this issue is to use lifetimes. Lifetimes are a way of specifying the lifetime of a reference in Rust. They are used to ensure that references are valid for a certain period of time.

Here is an example of how lifetimes can be used to ensure that a pointer remains valid:

In this example, the vec_ptr pointer is created to the first element of the vec vector. The lifetime of the reference is specified with the 'static keyword, which means that the reference is valid for the entire duration of the program. As the vec vector is not deallocated, the vec_ptr pointer remains valid and can be safely dereferenced.

On the other hand, if we try to create a pointer to an object that goes out of scope, we will get a compile-time error:

In this example, the vec_ptr pointer is created to the x reference, which points to the first element of the vec vector. However, x goes out of scope at the end of the inner block, and the vec_ptr pointer becomes a dangling pointer. The Rust compiler will detect this and prevent the program from compiling

Lifetimes can also be used in conjunction with structs and functions to ensure that references remain valid.

In this example, the MyStruct struct has a reference field with the lifetime 'a, and the my_function function has a reference parameter with the same lifetime. This means that the references stored in the struct and returned by the function will remain valid for the same lifetime 'a. In this case, 'a is the lifetime of the x variable, so the references will remain valid as long as x remains in scope.

Overall, lifetimes are a useful tool for ensuring the validity of references in Rust, particularly when working with pointers. By specifying the lifetimes of references, we can prevent dangling pointers and ensure that our program is safe and correct.

I share my learning on daily basis on twitter and LinkedIn:

For More such interesting tips follow me on twitter

https://twitter.com/nceevij

and LinkedIn

https://www.linkedin.com/in/vijendr/

--

--

Software Architect @CordNetwork @Dhiway Building Digital Trust Ecosystems for Public Services

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vijay Gaur

Software Architect @CordNetwork @Dhiway Building Digital Trust Ecosystems for Public Services