Returning local variable's address in C

In this video, starting from 13:35, Alex Crichton illustrates the borrowing concept in Rust with what he claims to be "a common mistake in C" (suggesting an advantage of Rust over C): returning the address of a local variable.

The example code he gives, in Rust, is as follow:

In Rust, the code above returns an error message, as explained in the video.

This code is equivalent in C to:

Compiled with:

correctly returns the following warning:

One may argue it's a warning against an error. I would reply, as well as you're free to ignore the warnings in C you're free to get rid of the errors in Rust using unsafe blocks.
I'm using Ubuntu and gcc version 9.4.0. gcc version 4.7.4, which was available at the time the video was published, already reported the warning. On a MacBook, clang version 11.0.3 also reports the warning.

The borrowing concept in Rust is definitely interesting. But if it gives an advantage over C, it's definitely not concerning the avoidance of return of address of local variables. C compilers already did it as well as Rust.

2021-08-16
in Debunked,
80 views
Copyright 2021-2024 Baillehache Pascal