Why one static var has two addr?

here is my log:

B9572701-D0C3-4E6C-A162-E6A4F091160F

and here is my code:

in mod global:

pub static mut TEST:TestStruct=TestStruct{ id: 1 };

#[derive(Debug)]
pub struct TestStruct{
    pub id:i32
}

in mod magic:

fn test2(){
	unsafe {
		info!("test>>>>> {:p} | {:?}",&TEST,TEST);
	}
}

in mod store:

fn test1(){
	unsafe {
		let a=&mut TEST;
		*a=TestStruct{ id: 22 };
	}
	unsafe {
		info!("ptr》》》》》》: {:p} | {:?}",&TEST,TEST);
	}
}

Please post a complete example. There are mistakes you could make in code you didn't show.

2 Likes

This looks to be something in code you haven't shared. I've turned what you shared into a Rust Playground, and I see:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.23s
     Running `target/debug/playground`
[2023-11-24T11:27:46Z INFO  playground::store] ptr》》》》》》: 0x563af7c66008 | TestStruct { id: 22 }
[2023-11-24T11:27:46Z INFO  playground::magic] test>>>>> 0x563af7c66008 | TestStruct { id: 22 }

It'd be helpful if you could share a complete reproducer of the problem.

4 Likes

From the reference:

Static items

Syntax
StaticItem :
static mut ? IDENTIFIER : Type ( = Expression )? ;

A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.