C#: how to do: fixed (byte* ptr = (byte*)this) [on hold]How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?What are the correct version numbers for C#?How do you convert a byte array to a hexadecimal string, and vice versa?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?What is a NullReferenceException, and how do I fix it?
A known event to a history junkie
Can I Retrieve Email Addresses from BCC?
Proof of Lemma: Every integer can be written as a product of primes
Lifted its hind leg on or lifted its hind leg towards?
A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?
Do all polymers contain either carbon or silicon?
How can I successfully establish a nationwide combat training program for a large country?
Why isn't KTEX's runway designation 10/28 instead of 9/27?
Does "Dominei" mean something?
Organic chemistry Iodoform Reaction
Could solar power be utilized and substitute coal in the 19th century?
My boss asked me to take a one-day class, then signs it up as a day off
I2C signal and power over long range (10meter cable)
How to color a zone in Tikz
Giant Toughroad SLR 2 for 200 miles in two days, will it make it?
Simple recursive Sudoku solver
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Can a malicious addon access internet history and such in chrome/firefox?
How do I rename a LINUX host without needing to reboot for the rename to take effect?
Greatest common substring
What does 사자 in this picture means?
No idea how to draw this using tikz
What is the term when two people sing in harmony, but they aren't singing the same notes?
Is exact Kanji stroke length important?
C#: how to do: fixed (byte* ptr = (byte*)this) [on hold]
How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?What are the correct version numbers for C#?How do you convert a byte array to a hexadecimal string, and vice versa?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?What is a NullReferenceException, and how do I fix it?
I have a struct array in unmanaged memory.
In my struct code, marked as 'unsafe', I need to do something like:
void myMethod()
fixed ( byte* ptr = this )
MyStruct* pMyStruct = (MyStruct*)ptr;
//-- from here on I can refer to the fields in the struct
pMyStruct->Field1 = ...
...
Unfortunately, I am unable to cast/convert 'this' into a memory pointer.
My structs are all in unmanaged memory.
To get around the problem I defined all my struct methods as static and pass in the pointer to the start of the struct. Workable but not elegant.
Any ideas how to convert the 'this' to a 'byte*' or even a 'void*' ?
[For those who might want to question why I need this: for all its worth, the structs contain variable length fields. Each field is really a byte offset (from the start of the struct) to the actual value. The only things that have a a fixed offset into the structure are the offsets to the values. This means that where in memory the value of any field changes across struct instances - i.e., the location of the values are data dependent].
c# memory
put on hold as off-topic by Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere Mar 21 at 17:23
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere
|
show 5 more comments
I have a struct array in unmanaged memory.
In my struct code, marked as 'unsafe', I need to do something like:
void myMethod()
fixed ( byte* ptr = this )
MyStruct* pMyStruct = (MyStruct*)ptr;
//-- from here on I can refer to the fields in the struct
pMyStruct->Field1 = ...
...
Unfortunately, I am unable to cast/convert 'this' into a memory pointer.
My structs are all in unmanaged memory.
To get around the problem I defined all my struct methods as static and pass in the pointer to the start of the struct. Workable but not elegant.
Any ideas how to convert the 'this' to a 'byte*' or even a 'void*' ?
[For those who might want to question why I need this: for all its worth, the structs contain variable length fields. Each field is really a byte offset (from the start of the struct) to the actual value. The only things that have a a fixed offset into the structure are the offsets to the values. This means that where in memory the value of any field changes across struct instances - i.e., the location of the values are data dependent].
c# memory
put on hold as off-topic by Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere Mar 21 at 17:23
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere
1
What is the error message you are getting?
– Robert Harvey♦
Mar 21 at 14:53
1
You can use Marshal object to copy any un-magnaged pointer object to managed object (or byte[]) array as long as you know the start location (the pointer) and the length of the object. Once you get it into managed memory you can use BitConverter to get the values.
– jdweng
Mar 21 at 14:56
1
What is the type ofthis
? AssumingmyMethod()
is a member ofMyStruct
, why are you trying to cast tobyte*
first and then back toMyStruct*
?
– kalimag
Mar 21 at 14:56
2
Theunsafe
keyword is not a "now I can anything I want" passport. The C# compiler can already correctly detect that your assumptions are nonsensical.
– Hans Passant
Mar 21 at 15:13
2
this
is not a pointer but a reference, even when referring to astruct
.fixed ( MyStruct* ptr = &this) byte* b = (byte*) ptr;
is legal. Whether your scenario makes sense is another matter altogether -- I strongly suspect it does not, so you should not take the indication that this compiles as confirmation that it will actually work correctly.
– Jeroen Mostert
Mar 21 at 16:18
|
show 5 more comments
I have a struct array in unmanaged memory.
In my struct code, marked as 'unsafe', I need to do something like:
void myMethod()
fixed ( byte* ptr = this )
MyStruct* pMyStruct = (MyStruct*)ptr;
//-- from here on I can refer to the fields in the struct
pMyStruct->Field1 = ...
...
Unfortunately, I am unable to cast/convert 'this' into a memory pointer.
My structs are all in unmanaged memory.
To get around the problem I defined all my struct methods as static and pass in the pointer to the start of the struct. Workable but not elegant.
Any ideas how to convert the 'this' to a 'byte*' or even a 'void*' ?
[For those who might want to question why I need this: for all its worth, the structs contain variable length fields. Each field is really a byte offset (from the start of the struct) to the actual value. The only things that have a a fixed offset into the structure are the offsets to the values. This means that where in memory the value of any field changes across struct instances - i.e., the location of the values are data dependent].
c# memory
I have a struct array in unmanaged memory.
In my struct code, marked as 'unsafe', I need to do something like:
void myMethod()
fixed ( byte* ptr = this )
MyStruct* pMyStruct = (MyStruct*)ptr;
//-- from here on I can refer to the fields in the struct
pMyStruct->Field1 = ...
...
Unfortunately, I am unable to cast/convert 'this' into a memory pointer.
My structs are all in unmanaged memory.
To get around the problem I defined all my struct methods as static and pass in the pointer to the start of the struct. Workable but not elegant.
Any ideas how to convert the 'this' to a 'byte*' or even a 'void*' ?
[For those who might want to question why I need this: for all its worth, the structs contain variable length fields. Each field is really a byte offset (from the start of the struct) to the actual value. The only things that have a a fixed offset into the structure are the offsets to the values. This means that where in memory the value of any field changes across struct instances - i.e., the location of the values are data dependent].
c# memory
c# memory
asked Mar 21 at 14:48
david marcusdavid marcus
726
726
put on hold as off-topic by Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere Mar 21 at 17:23
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere
put on hold as off-topic by Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere Mar 21 at 17:23
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hans Passant, Sterling Archer, thehennyy, wp78de, thewaywewere
1
What is the error message you are getting?
– Robert Harvey♦
Mar 21 at 14:53
1
You can use Marshal object to copy any un-magnaged pointer object to managed object (or byte[]) array as long as you know the start location (the pointer) and the length of the object. Once you get it into managed memory you can use BitConverter to get the values.
– jdweng
Mar 21 at 14:56
1
What is the type ofthis
? AssumingmyMethod()
is a member ofMyStruct
, why are you trying to cast tobyte*
first and then back toMyStruct*
?
– kalimag
Mar 21 at 14:56
2
Theunsafe
keyword is not a "now I can anything I want" passport. The C# compiler can already correctly detect that your assumptions are nonsensical.
– Hans Passant
Mar 21 at 15:13
2
this
is not a pointer but a reference, even when referring to astruct
.fixed ( MyStruct* ptr = &this) byte* b = (byte*) ptr;
is legal. Whether your scenario makes sense is another matter altogether -- I strongly suspect it does not, so you should not take the indication that this compiles as confirmation that it will actually work correctly.
– Jeroen Mostert
Mar 21 at 16:18
|
show 5 more comments
1
What is the error message you are getting?
– Robert Harvey♦
Mar 21 at 14:53
1
You can use Marshal object to copy any un-magnaged pointer object to managed object (or byte[]) array as long as you know the start location (the pointer) and the length of the object. Once you get it into managed memory you can use BitConverter to get the values.
– jdweng
Mar 21 at 14:56
1
What is the type ofthis
? AssumingmyMethod()
is a member ofMyStruct
, why are you trying to cast tobyte*
first and then back toMyStruct*
?
– kalimag
Mar 21 at 14:56
2
Theunsafe
keyword is not a "now I can anything I want" passport. The C# compiler can already correctly detect that your assumptions are nonsensical.
– Hans Passant
Mar 21 at 15:13
2
this
is not a pointer but a reference, even when referring to astruct
.fixed ( MyStruct* ptr = &this) byte* b = (byte*) ptr;
is legal. Whether your scenario makes sense is another matter altogether -- I strongly suspect it does not, so you should not take the indication that this compiles as confirmation that it will actually work correctly.
– Jeroen Mostert
Mar 21 at 16:18
1
1
What is the error message you are getting?
– Robert Harvey♦
Mar 21 at 14:53
What is the error message you are getting?
– Robert Harvey♦
Mar 21 at 14:53
1
1
You can use Marshal object to copy any un-magnaged pointer object to managed object (or byte[]) array as long as you know the start location (the pointer) and the length of the object. Once you get it into managed memory you can use BitConverter to get the values.
– jdweng
Mar 21 at 14:56
You can use Marshal object to copy any un-magnaged pointer object to managed object (or byte[]) array as long as you know the start location (the pointer) and the length of the object. Once you get it into managed memory you can use BitConverter to get the values.
– jdweng
Mar 21 at 14:56
1
1
What is the type of
this
? Assuming myMethod()
is a member of MyStruct
, why are you trying to cast to byte*
first and then back to MyStruct*
?– kalimag
Mar 21 at 14:56
What is the type of
this
? Assuming myMethod()
is a member of MyStruct
, why are you trying to cast to byte*
first and then back to MyStruct*
?– kalimag
Mar 21 at 14:56
2
2
The
unsafe
keyword is not a "now I can anything I want" passport. The C# compiler can already correctly detect that your assumptions are nonsensical.– Hans Passant
Mar 21 at 15:13
The
unsafe
keyword is not a "now I can anything I want" passport. The C# compiler can already correctly detect that your assumptions are nonsensical.– Hans Passant
Mar 21 at 15:13
2
2
this
is not a pointer but a reference, even when referring to a struct
. fixed ( MyStruct* ptr = &this) byte* b = (byte*) ptr;
is legal. Whether your scenario makes sense is another matter altogether -- I strongly suspect it does not, so you should not take the indication that this compiles as confirmation that it will actually work correctly.– Jeroen Mostert
Mar 21 at 16:18
this
is not a pointer but a reference, even when referring to a struct
. fixed ( MyStruct* ptr = &this) byte* b = (byte*) ptr;
is legal. Whether your scenario makes sense is another matter altogether -- I strongly suspect it does not, so you should not take the indication that this compiles as confirmation that it will actually work correctly.– Jeroen Mostert
Mar 21 at 16:18
|
show 5 more comments
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
1
What is the error message you are getting?
– Robert Harvey♦
Mar 21 at 14:53
1
You can use Marshal object to copy any un-magnaged pointer object to managed object (or byte[]) array as long as you know the start location (the pointer) and the length of the object. Once you get it into managed memory you can use BitConverter to get the values.
– jdweng
Mar 21 at 14:56
1
What is the type of
this
? AssumingmyMethod()
is a member ofMyStruct
, why are you trying to cast tobyte*
first and then back toMyStruct*
?– kalimag
Mar 21 at 14:56
2
The
unsafe
keyword is not a "now I can anything I want" passport. The C# compiler can already correctly detect that your assumptions are nonsensical.– Hans Passant
Mar 21 at 15:13
2
this
is not a pointer but a reference, even when referring to astruct
.fixed ( MyStruct* ptr = &this) byte* b = (byte*) ptr;
is legal. Whether your scenario makes sense is another matter altogether -- I strongly suspect it does not, so you should not take the indication that this compiles as confirmation that it will actually work correctly.– Jeroen Mostert
Mar 21 at 16:18