0

I just wanted to déclare a pointer to a struct in a crate shared by several component of my project but using the same process. I m meaning the aim is to get it initilized only once.

type Box = [u64; 64];
pub static mut mmaped: &mut Box;

which on compile time generate this error.

free static item without body

where mmaped is later assigned a value in the following way only one time from the top crate and it s value used from the multiple crates it depends on.

mmaped = unsafe { std::mem::transmute(addr) };

So how to provide a definition mmaped without having to mmap it more than one time?

This question isn t a duplicate of this one as it doesn t talk about exporting the singleton outside the crate and I m getting a compiler error specifically for doing it.

user2284570
  • 2,686
  • 3
  • 21
  • 67
  • @pretzelhammer I edited my question, but this doesn t appears to apply exactly to my specific case as it doesn t explain how to get it exported outside the crate. – user2284570 Dec 14 '20 at 16:48
  • Please [edit] your question to contain a [mre] that either (1) demonstrates the error you're currently getting or (2) compiles but does not have the desired behavior (and explain what the desired behavior is). In particular, it's not clear what you mean by "without having to mmap it more than one time". `mmap` is a [specific thing](https://en.wikipedia.org/wiki/Mmap); if you're not referring to that you should probably try to find different words to describe the behavior you want. – trent Dec 14 '20 at 17:22
  • @trentcl `type Box = [u64; 64]; pub static mut mmaped: &mut Box;` is the minimal example trigerring the error. – user2284570 Dec 14 '20 at 17:24
  • @trentcl https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=eb9a09e43bf6495eb950fcea1a967ae6 – user2284570 Dec 14 '20 at 17:33
  • Um... ok... I must be missing something. You can't declare an uninitialized `static`, that's what the error is telling you. It's not valid code because it doesn't *mean* anything. So... what do you want it to mean? Maybe you want to create a static `Option`? (``pub static mut MMAPPED: Option = None;``) – trent Dec 14 '20 at 17:34
  • @trentcl but then, it s value will be reinitilized to `None` each time my crate is loaded by a new thread? – user2284570 Dec 14 '20 at 17:38
  • No, spawning a new thread does not re-initialize static variables, which are shared between threads. If you wanted that behavior you would need to use `thread_local`. (But realistically you probably want to use `lazy_static` instead of `static mut`, which is prone to UB if you are not extremely careful) – trent Dec 14 '20 at 17:40
  • @trentcl this doesn t seems to be the case as show by this playground https://stackoverflow.com/a/65270812/2284570. – user2284570 Dec 14 '20 at 17:42
  • There's no `static` in that example, just two local variables initialized separately. – trent Dec 14 '20 at 17:44
  • @trentcl I m meaning this since the thread will load the crate one more time. – user2284570 Dec 14 '20 at 18:13
  • 1
    @user2284570 I realized that [your question](https://stackoverflow.com/questions/65270748/when-implementing-the-default-trait-for-a-struct-and-intializing-its-members-us/65294420#65294420) was misunderstood, so I added an answer. – Ibraheem Ahmed Dec 14 '20 at 18:32

0 Answers0