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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript