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?













0















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].










share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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? 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





    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 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















0















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].










share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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? 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





    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 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













0












0








0








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].










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
If this question can be reworded to fit the rules in the help center, please edit the question.







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
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 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? 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





    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 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












  • 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? 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





    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 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







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












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?