Why get address of variable is unsafe ? #6182
-
When I use operator for get address(&) compiler shows necessary using unsafe context. But if we just get address and never use it as pointer we are always in safe context. I suggest making changes. So that this requirement applies only if pointers are used for read\write operations. Below I give the code that should work in a safe context. static void Test()
{
var adr = 0;
var threadId = (long)&adr >> 16;
} Edit: |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 54 replies
-
I made two benchmarks: With ManagedThreadId. class, bench
|
Beta Was this translation helpful? Give feedback.
-
Can you explain what do you want with that pointer? |
Beta Was this translation helpful? Give feedback.
-
I don't think this is sufficient justification to change the language, which is something that requires fairly significant effort, even for a small change like this one. This change would bring a tiny benefit (not requiring to use And your use case still feels fairly dangerous to me: what guarantees that stacks of two threads can't occupy the same 64 kB block of memory? So I'm not sure not requiring |
Beta Was this translation helpful? Give feedback.
-
What's the value in getting the address of something that you don't then intend to read/write. I don't understand the use case. |
Beta Was this translation helpful? Give feedback.
-
So? Language changes need to make sense for every existing platform, not just your narrow special case.
This sounds like an incredibly narrow special case. What's the value in the language changing for ALL users just to accommodate your esoteric application? I'm not denying the value to you in your situation - but languages changes aren't made to suit a single case; they're tremendously expensive to implement and incur an ongoing testing and maintenance burden, so changes need to make sense for a wider audience. |
Beta Was this translation helpful? Give feedback.
-
@MBurtsev another way to get the same 'random' distribution is to have something like
|
Beta Was this translation helpful? Give feedback.
@MBurtsev another way to get the same 'random' distribution is to have something like